Data from file in dash - upload component

If I want to use data from a file to generate a set of plots, how can I do this?

My initial expectation was that I would have an initial page for the file upload and that the successful upload would trigger the loading of another page with the graphs/dashboard. I heard that there’s multi-app support in Dash in master at the moment but I am not sure I need to go that far.

Any suggestions on how to get something like this done?

Kind regards,

Paulo Matos

1 Like

Right now, you would need to preload this data and offer a UI for selecting different (available) data files. If you want the viewers of your dash app to supply their own data files (e.g. upload a CSV), then you’ll have to wait for an official dash_core_components.Upload button (or build your own or contract us to build one).

@chriddyp Is there an rough timeframe by when the official upload component will be released?

Likely sometime this fall, unless expedited by a corporate sponsor.

Thanks for the update

Update: Adding support for this in https://github.com/plotly/dash-core-components/pull/73

7 Likes

Update: this is now available in dash-core-components==0.14.0. View the documentation here: https://plot.ly/dash/dash-core-components/upload

7 Likes

2 posts were split to a new topic: Dash Upload Component - Decoding Large Files

A post was split to a new topic: Dash Upload - Uploading a Text File

By the way, I was trying to import sas xpt file by adding the following line:

elif 'xpt' in filename:
    # Assume that the user uploaded an xpt file
    df = pd.read_sas(io.BytesIO(decoded), format='xport')

Unfortunately, I am getting error TypeError: Object of type 'bytes' is not JSON serializable
Instead of Bytes, I may use the absolute path of the file, however, is there anyway I can use the same method as used for csv and xlsx?

1 Like

when the file is uploaded, where is it stored? like a local variable?
Because I need to use it further.

2 Likes

I’m really interested in the question of prasadovhal, someone has an answer?

Also super interested in @prasadovhal 's question. The dash app I’m building originally takes in a hard-coded csv, but I’ve recently discovered the upload component and I would love for my users to be able to upload csv’s themselves. Is there any way to store the uploaded file for it to be used later in other functions?

I’ve been referring to https://dash.plot.ly/dash-core-components/upload (specifically the parse_contents function) and looking for a workaround but I’m having trouble finding a solution. I know using global variables is not recommended, so I’m curious what other solutions are out there.

For now I’ve been trying to adapt the already written code to use the uploaded file, but it’s starting to get very messy… Any help or suggestions would be greatly appreciated!

2 Likes

It’s stored in the “front-end” as a base64 encoded string and transferred to your Python code via the dash callbacks via the contents property. Once its in your python callback, you can do whatever you want with it: save it to a file (which is one way for it to be reused in other callbacks: you just have to be careful about file naming), read it into a dataframe, etc.

For file naming, you could come up with your own user session ID and use that as a filename and reference that session ID across callbacks. That way, multiple users can access the app at once without overriding (or accessing) each other’s data files. See " Example 4 - User-Based Session Data on the Server" in Part 4. Sharing Data Between Callbacks | Dash for Python Documentation | Plotly . In that example, data is implicitly saved to the file system via flask-caching, but you could use the session ID input yourself as a filename.

2 Likes

Instead of storing data in hidden division, I have employed SQL query to solve this issue. Which solved all of mine data handling problems.

@prasadovhal Hi, I wonder how much does that call take? Doesn’t that hinder UX?

I’ve a similar problem and tried out to upload/import an spss file (sav). I tried out two options but it didn’t work.

elif ‘sav’ in filename:
df = pd.read_spss(io.StringIO(decoded.decode(‘utf-8’)))
or

elif ‘sav’ in filename:
df = pd.read_spss(io.BytesIO(decoded))

Did anybody find a solution for SAS/STATS7SPSS upload?

hi, is there any way to display the name of loaded file in Upload Box , instead of “upload” string?