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?
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).
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?
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!
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.