Read file with Pandas Dataframe

Hello,

I would like to know how I can read a file stored in a table with Pandas Dataframe.

This is an example:

image

Thanks in advance.

This sounds awfully familiar. Have you searched this forum for existing solutions?

1 Like

Yes, but I didn’t find what I was looking for. In the end, I found a solution to read the file without having to dump it into a database. I leave here the code in case it is useful to someone else.

@anvil.server.callable
def store_data(file):
  with anvil.media.TempFile(file) as file_name:
    if file.content_type == 'text/csv':
      df = pd.read_csv(file_name)
    else:
      df = pd.read_excel(file_name)
    for d in df.to_dict(orient="records"):
      # d is now a dict of {columnname -> value} for this row
      # We use Python's **kwargs syntax to pass the whole dict as
      # keyword arguments
      # app_tables.data.add_row(**d) This Outside
  dft = pandas.DataFrame.from_dict(df) #New line for read the file
  print(dft)
2 Likes

Ah cool use of a TempFile!

1 Like