Create dataframe from file in data table

I am trying to get csv and excel files out of a data table and into data frames. I don’t think I understand the type that is returned from the tables.

My code as it stands is this:

data_file_row = app_tables.data_table.get(owner=anvil.users.get_user())
data_file = data_file_row['file']
df = pd.read_excel(data_file, sheet_name='Sheet_1',)

and this for the csv file:

data_row = app_tables.reference_table.get(owner="owner”)
data_file = data_file_row['file']
data_df = pd.read_csv(file_name)

The error returned is this in the case of the Excel file:
ValueError: Invalid file path or buffer object type: <class 'anvil._server.LiveObjectProxy'>

How should I be passing the objects?

Thanks

Hello @whills

Does this post help?


As an aside, you can format code on the forum by wrapping it in backticks with the word Python.

For example,

```python

print('this will be syntax highlighted')

```

Hi there - you’re really close! You’re getting this error because you’re passing the Media object directly into pd.read_excel(), which doesn’t understand Media objects. But you can write the Media out to a temporary file, and load it from there:

import anvil.media

with anvil.media.TempFile(data_file_row['file']) as filename:
  df = pd.read_excel(filename, sheet_name='Sheet_1')
4 Likes

@alcampopiano. @meredydd, Thanks!
Both of those will be useful in due course. I am using

 m=app_tables.reference_files.get(name="foo")["file"]
  cofr_df=pd.read_csv(io.BytesIO(m.get_bytes()), index_col=0)  ```

and it seems to be doing the trick.
1 Like

That’ll work too! Glad you got it sorted.

we will write the name of our data table in which that file is stored or the actual name of the file which we uploaded in our data table in place of ‘file’
kindly help me, which things do i have to alter