Pandas Dataframe (read file stored in database)

Ok try this:

#put this at the top with the imports:
import io


@anvil.server.callable
def read_csv():
  for row in app_tables.table_1.search():
    file = row['file_object']
    f = io.BytesIO(file.get_bytes())
    df = pd.read_excel(f)
    
    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
      print(d)
      pass
    
    dft = pd.DataFrame.from_dict(df) #New line for read the file
    print(dft)

This works because io.BytesIO() takes a bytes string from the media object and turns it into an object that looks like a file to pandas. (It loads a file-like object in memory instead of the disk)

3 Likes