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

how to use dcc.Store() for dictionary of dataframe

我已经挣扎了一天了。谁能帮帮我?

我有一本字典可以存储很多数据帧,即。 dict1={'key1':df1,'key2':df2,'key3':df3}

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

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

那么如何将数据框字典存储到 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)

错误 TypeError: Object of type DataFrame is not JSON serializable 意味着您需要将数据框转换为 JSON-compatible 的内容(例如字典)。然后,您将需要向 dcc.Store() 传递一个元组或该 json-serializable 对象的列表(例如字典列表)。一旦您想在另一个回调中读取该数据框,只需将 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)