Hi Paul,
The filesystem in Server modules is ephemeral – you can store data there for a short while, but it might not be there on your next server call. It’s fine for caching (eg making a copy of a file you already have in Drive or Data Tables), but we don’t recommend storing data there that you want to be persistent.
with anvil.media.TempFile(media)
will write the Media object to a temporary file, which is removed as soon as the with
block finishes executing. For example, if you’re uploading a file that’s being used with Pandas, you can do the following:
# On the client:
def file_loader_1_change(self, file, **event_args):
anvil.server.call('process_csv', file)
# On the server:
@anvil.server.callable
def process_csv(csv_media):
with anvil.media.TempFile(csv_media) as temp_filename:
df = pd.read_csv(temp_filename)
# ... and now do what we like with that data frame ...
Does this make sense?
You can, of course, copy that tempfile to somewhere on the server filesystem, so you can load it from there (rather than pulling it from Google) – but you should check each time whether the file exists, because it will periodically disappear as your server environment gets refreshed.
Something like:
import os
def get_data_frame():
if not os.path.isfile('/tmp/data.csv'):
with open('/tmp/data.csv', 'w') as f:
f.write(app_tables.my_file.get_bytes())
return pd.read_csv('/tmp/data.csv')
Hope that helps!