Download all table contents to a .csv

I very much like the ability to go to the data tables and export/download an entire table to a .csv. Is there a component method to accomplish the same thing from a button? Best I can tell I have to write server code to basically course through a table and write each row out to a file on disk.

app_tables.my_table.search().to_csv()
will get you the csv
though if you need to process the csv then you’ll need to iterate through the rows and write each row as you’ve already suggested.

You can create the csv on the server and send a media object to the client.
But there’s no builtin component that will accomplish this.

So you’d probably end up doing something like

def button_click(self, **event_args):
  csv = anvil.server.call('get_csv')
  download(csv)

3 Likes

You could use Pandas to process the CSV file, but keep in mind loading Pandas and processing will add seconds to the processing time. Very powerful though.

1 Like

Just click the button and the table will be saved as csv

I ended up writing some brief code that compiles each row and exports to a .csv just fine, and it doesn’t take very long. I do love Pandas but I was trying to do all of this on the client side. Thanks for the resposnes.

1 Like

Hi @dconnell - coming late to this as I just had the same requirement.

I don’t know if I’m missing the point but I don’t think you need such an elaborate solution. My client side solution was as follows:

Create a button in the Design view as normal, then create a function for the click event:

image

  def button_1_click(self, **event_args):
      """Download contents of repeating panel (visible and invisible values) or
      entire data table to CSV.  Comment out the relevant line as applicable.
      Requires client search permission to be set in my_table"""
      csv_file = self.repeating_panel_1.items.to_csv()
      # Or...
      csv_file = app_tables.my_table.search().to_csv()
      anvil.media.download(csv_file)

No need for Pandas. No need for server function (unless for security etc you want to run app.tables.my_table.search() on the server. I believe anvil.media.download() can only be called from the client though, so if you do want a server side solution, you’d need to call the server function from the client, return the LazyMedia object, and then finish the job in the client.

Hope that helps.
Pete

1 Like

Would it be possible to share a snippet regarding this ?
I have returned my pandas dataframe from serever as csv then directly called it for download using
download (csv)
But I am encountering the following error:
Error message : AttributeError: ‘str’ object has no attribute ‘get_url’

Try this:

if you have a string (Im guessing you do from the error: AttributeError: ‘str’ object… ) you will want to convert it it a media object so you can call it like this:

import anvil.media

text_file = anvil.BlobMedia('text/plain', b'Hello, world', name='hello.txt')
anvil.media.download(text_file)

In the example above b’Hello, world’ would be replaced by your string object like my_string_object.encode(), and you might want a different mime type other than text/plain but it will probably work for a csv file either way.

1 Like