'how to use dcc.Store() for dictionary of dataframe
I have been struggling for one day already. Can anyone help me?
I have a dictionary to store lots of dataframes, ie. dict1={'key1':df1,'key2':df2,'key3':df3}
Now I am using dcc.Store() to share these dictionary data between callbacks.
firstly, I store the dictionary into dcc.Store() using json.dumps(dict1), then I got an error of TypeError: Object of type DataFrame is not JSON serializable.
so how do I store dictionary of dataframe into dcc.Store()? do I have to convert all the dataframe before putting them into the dictionary? It will re-work all my codes. Is it possible to do it without converting each dataframe to json and just work on the dictionary?
Thank you so much for your help. I really appreciate it.
@app.callback(
[
Output('stored-shared-data-time-dict','data'),
[
Input('load-area-data','n_clicks'),
],
prevent_initial_call=True, # disable output in the first load
)
def change_area_data(n_clicks): #radio_value,range_slider_values
s1=json.dumps(dict1)
return s1
then, I am loading the dictionary from dcc.Store().
@app.callback(
[
Output("timeplot-graph", "figure"),
Output("timeplot-markdown", "children"),
],
[
Input("plot-well-prod", "n_clicks"),
],
[
State('stored-shared-data-time-dict','data'),
],
prevent_initial_call=True, # disable output in the first load
)
def change_well_time_graphs(n_clicks,well_cell, well_data,json_dict):
df = json.loads(json_dict)
(fig,markdown_text)=make_graph(df)
print('Done Creating Graphes')
return (fig,markdown_text)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
