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

How to use dcc.Upload to upload pickle file


This is a newbie question. I am working on a dashboard to build linear regression and general linear regression model using StatsModel library. I want to be able to save the model after I build them as a pickle file, and then reload it later using the dashboard. It’s my understanding that the dcc.Upload gives a base64 encoded file, so my question is how to decode such a file.

Thanks in advance for any help.


Hi Mike,

In your call back with content as input from your dcc.Upload, you can retrieve the content of the pickle by decoding the base64 stream. Then, if say need to feed the decoded content as a file to some function (e.g. pandas read_pickle to retrieve a panda dataframe that you pickled), you can use ìo.BytesIO`as a pickle is a byte stream.

content_type, content_string = content.split(',')
decoded = base64.b64decode(content_string)
df = pd.read_pickle(io.BytesIO(decoded))

You need to import base64 and io

Hope this helps