Downloading a csv file

I am trying to download a csv file by using the exact method in the post below


However I am using a full path here as my csv file path, and after I run the function, it brings up an error and said there is no such file. Is this because of the full path I used? If I want to use relative path, then which directory should I start with?
Thanks in advance

Hello,

It is usually better to provide a link to another post rather than a screenshot. This way folks can click on it and look at the post in full detail.

Here is the link from above:

Have you tried '/tmp/your_file.csv'?

why doing /tmp/your_file.csv?
what I usually do when exporting csv using pandas is put the relative path or full path on my local device

It actually worked with /tmp/your_file.csv, However can you explain more in detail why do I need to put this here rather than a relative path?, also why not put ./tmp/your_file.csv instead?

also by doing this, why did I get a .txt file rather than a .csv file?

1 Like

The /tmp directory is on the Anvil servers. They let you store temporary files there (which, by the way, are not guaranteed to exist beyond the server call).

You could move the whole server side code to your own server using the uplink functionality and then you can use whatever path you like, but you are restricted to absolute path /tmp on the Anvil servers.

3 Likes

but why did it download to a txt file rather than a csv?

Thanks @david.wylie for your helpful description of the tmp directory.

@aren can you share the code you are using to return your media file after you saved it to tmp?

I assume that if the extension and content type are set to csv, then it should indeed be a csv.

For example (please see the docs):

my_media=anvil.media.from_file('/tmp/X.csv', 'csv', 'X.csv')

clone:
https://anvil.works/build#clone:6HZSMN3Q4EAQGOWR=HR47IXKXEXTTP27E6IQS54CW

yes this is exactly what I did
In the server code, I have:

@anvil.server.callable
def make_data():
  csv_1 = app_tables.csv_1.search()
  dicts = [{'site':r['site'],
            'actual_nw':r['actual_nw'],
            'ea_model':r['ea_model'],
            'ea_model_difference':r['ea_model_difference']}
            for r in csv_1]
  X = pd.DataFrame.from_dict(dicts)
  X.to_csv("/tmp/X.csv") 
  X_media = anvil.media.from_file('/tmp/X.csv', 'csv', 'X')
  return X_media

In the user part, I have:

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

To help others more easily read your code please format code by wrapping it in backticks with the word Python.

For example,

```python

print('this will be syntax highlighted')

```

Try adding ‘.csv’ extension to the “name” argument. I updated my previous answer and I added a clone.

@shaun just letting you know that this may need to be updated/fixed or mentioned in the docs

1 Like

yes sir it worked, however where can I find a more detailed documentation for future reference?

I know this is an old post, but I ran into it and the clone almost worked. I am sure anvil made an update. so for people in the future, this one line update gave me a .csv instead of a .txt:

goes to

my_media=anvil.media.from_file('/tmp/X.csv', 'text/csv', 'X.csv')
1 Like