Importing existing data into Data Table

Hi Chris,

Anvil can return a lot from a server function, but custom classes like Pandas data frame rows will fox it. If you want to return data like this, I’d suggest turning it into a dict (Pandas’s to_dict() method will help you here).

In fact, if you’re using the uplink, you don’t even need to return this data from a server function - you can add it to the data table directly! You can call add_row() on a data table from Uplink code, same as server code.

Something like this, perhaps:

import anvil.server
from tables import app_tables

anvil.server.connect("[key]")
df = pd.read_excel(...)
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.my_table.add_row(**d)

Does that do what you want?

1 Like