Black Lives Matter. Please consider donating to Black Girls Code today.

How to store uploaded file as dataframe and used in multiple callbacks?

Hello!

How to write a method that accept uploaded csv/excel file and return a dataframe that can be used in multiple callback functions?

Thanks!

I am interested in a better answer if someone has one.
Personally I have to load the dataset into a JSON file (https://dash.plot.ly/sharing-data-between-callbacks)
For large datasets, if I want to live plot a series for example, I update the series on a different process and save it on a csv. My dash then picks up the csv every second for example and display the chart. JSON is slow for loading large dataframes while pandas read_csv is acceptable

@yaochen, have you tried the dcc.Upload component

Thank you for these suggestions. I have seen and try to change these tutorial codes. But the “parse_content” method directly return a generated table. Instead, I want this method to just return a dataframe. In the other tutorial, there’s a code that puts the online datasets into cashe. I just don’t know how to link that with a file drop box upload from local file. Also, my file wouldn’t be so big that I have to put it into a cache.

Thank you and please provide more insights!

I see. I actually never tried out the dcc.Upload so I couldn’t tell you. When I saw you question, I immediately though there must be solution using the flask server instance, but then learned that there was a dash_core_component already created for this.

You could also try out using flask under the hood. Here is how you can upload in flask. After uploading you can then do a pd.read_excel() or any other pandas I/O method.

Note, to access the flask server instance, you can just use:

app = dash.Dash(__name__)
server = app.server

Let me know how it goes.

I have figure it out, with the help of tutorial dcc.Upload component. Very little changing is needed. If you need source code. Happy to send it.

Now I have one more question:

If I have a csv file with unknown length, and I want to generate a scatter plot with each several rows of data. So I have to generate many scatter plot. How can I use a for loop in the app.Layout() to do that? Can I for loop callback as well?

I am looking to create a dashboard that pre populates with a dataframe, but has the option to use the dcc.upload function to use user defined data. Is that the solution you created Yaochen? I would be interested in your code if so.

Hello Yaochen,

Can you please share your code. I am attempting to do something similar.

Thank you!

1 Like

Hi @yaochen, would it be possible for you to post your solution? It seems like this is a pretty straight-forward use case for the File Upload component and the community could really benefit from the example. Thanks!

hey yaochan,
can you please share your code?