如何使用 dcc.Store() 作为数据帧字典

问题描述 投票:0回答:1

我有一本字典来存储大量数据帧,即。 dict1={'key1':df1,'key2':df2,'key3':df3}

现在我使用 dcc.Store() 在回调之间共享这些字典数据。

首先,我使用 json.dumps(dict1) 将字典存储到 dcc.Store() 中,然后出现 TypeError: Object of type DataFrame is not JSON Serialized 错误。

那么如何将数据帧字典存储到 dcc.Store() 中?在将它们放入字典之前我是否必须转换所有数据帧?它将重新工作我的所有代码。是否可以在不将每个数据帧转换为 json 的情况下只对字典进行操作?

非常感谢您的帮助。我真的很感激。


@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 

然后,我从 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)

plotly-dash
1个回答
4
投票

错误

TypeError: Object of type DataFrame is not JSON serializable
意味着您需要将数据帧转换为与 JSON 兼容的内容(例如字典)。然后,您需要向
dcc.Store()
传递一个元组或该 json 可序列化对象的列表(例如字典列表)。一旦您想在另一个回调中读取该数据帧,只需将
dcc.Store
对象转换回数据帧即可。以下是如何在代码中执行此操作的示例:


@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 = [df.to_dict()]
    return s1 


@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 = pd.DataFrame.from_dict(json_dict, orient='index')   
    (fig,markdown_text)=make_graph(df)
    print('Done Creating Graphes')
        
    return (fig,markdown_text)

编辑: 如果您希望将数据帧字典存储在

dcc.Store
中,则适用相同的方法,但对字典中的每个数据帧执行此操作,然后将字典发送到存储,然后迭代中的每个字典回调并将其转换回数据帧,这是您的代码的示例:

@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
    temp_dict = dict1
    for key, df in temp_dict.items():
        temp_dict[key] = df.to_dict()
    return [temp_dict]


@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_list = []
    for _, dic in json_dict.items():
        df_list.append(pd.DataFrame.from_dict(dic, orient='index')) 
    # the rest of your logic using the list of df's "df_list"
    .
    .
    .
    return (fig,markdown_text)
© www.soinside.com 2019 - 2024. All rights reserved.