Create pandas dataframe to csv to media object to download from server module

Objective: Getting a pandas dataframe generated from server side data to a csv file to a media object to be able to download the csv file in the server module.

I am generating small dataframes of data based on a few parameters on the server module and after creating the data want the user to be able to download the csv files. I do not need to save the csv files as I can generate the data from the parameters and would merely waste memory.

My thought process is generate data, create df, save to csv local temp folder load csv file as media object and call download on button click.

I could generate the data save a csv and send a media object remotely but my goal is to do this using the server module server side as this code is small and fast.

Here is demo code of server module, after consulting all documention have exhausted options.

@anvil.server.callable
def make_data(**kwargs):
  x_arr = np.array([[1,2,3]]) #placeholder data
  X = pd.DataFrame(x_arr)
  X.to_csv('/tmp/X.csv')
  X_media = anvil.media.from_file('/tmp/X.csv', 'csv', 'X')
  return X_media

In the Home class upon button click, am of course calling this function with download.

  def button_1_click(self, **event_args):
    """This method is called when the button is clicked"""
    x_media =anvil.server.call("make_data")
    download(x_media)

I am recieving from the server module
AttributeError: module ‘anvil’ has no attribute ‘media’

Despite code being in documentation @
https://anvil.works/docs/media

Is there an equivilant updated module for this call? I could not find it.

1 Like

You’re nearly there! You just need to import anvil.media at the top of your server module, and the code you’ve written should work :slight_smile:

1 Like

Thanks that worked,
I was doing import anvil, and assumed the method did not exist, will keep that in mind for future imports.

Great! Can you mark my post as a successful answer to the question, then? It’s the checkbox (you might need to click the “…” under the post to see it):

image

Hi ,

I had to do exactly what you are trying to do, but in my case the file is downloading as a txt file and not a csv file ? what do I do

Does the file have a CSV file extension? For example, '/tmp/X.csv'.

If this does not help, please share a simple clone that can reproduce your issue if you are able (or show the relevant code).

yes it does have a csv extension

the relevant code is exactly the same as mentioned above
series.to_csv(‘series.csv’) // i use this to same a temp file in another function which is then returned in case the function below is called

def get_data_1():
ser = anvil.media.from_file(‘series.csv’, ‘csv’, ‘Country 1’)
return ser

finally the frame code is:
def button_3_click(self, **event_args):
da = anvil.server.call(‘get_data_1’)
anvil.media.download(da)

Hmm, what if you use "Country 1.csv" as the filename?

That appears to work for me (i.e., it is a CSV file that wants to open in a spreadsheet program rather than a text editor).

https://anvil.works/build#clone:HMPNT63MKSX6TCKB=OEYXKJAVXPQSDGUI4QNWU2PN

Just a suggestion but you can format your code nicely on the forum by wrapping it in backticks with the word “python”.

Yup,

It worked.
Thank You soooo much

1 Like

A post was split to a new topic: Returning media object in background task