Storing a DataFrame in dcc.Store

@AnnMarieW Suppose I have a DataFrame df and Dictionary dict-obj, can I store them in data property of dcc.Store like below?

from dash.dependencies import Input, Output, State
import pandas as pd
import dash
import dash_table
import dash_html_components as html
import dash_core_components as dcc
import dash_bootstrap_components as dbc
from file1 import func_call


layout = html.Div([

                # Plot map
                dcc.Graph(id="map-graph"),

                dash_table.DataTable(id="table"),

                dcc.Input(id="space"),

                dcc.Input(id="type"),
  
                dbc.Button(id="btn"), 

                dcc.Store(id="c-store")

       ])


@application.callback(Output("space","value"),
                               [ 
                                    Input("type","value")
                               ]
                               )
def generate_df(type):

      # Function returns a Pandas DataFrame Object 
      df = func_call(type)

      space_val = df[df["col1"] == "type"]] 

      return space_val
 


# Store map-graph and data-table in dcc.Store component
@application.callback(Output("c-store","data"),
                     [
                          Input("map-graph","figure"),
                          Input("table","data"),

                          # Pandas DataFrame -- useful for stats calculation
                          Input("df","data"),
 
                          # Store Dictionary Object
                          Input("dict_obj","data"),


                          Input("space","value"),
                          Input("type","value"),
                          Input("button","n_clicks")
                     ]
                    )
def c_store(map, table, df, dict_obj, space, type, n_clicks):

     print("DataFrame", df)


    # Store map-graph and data-table as dictionary.
    if n_clicks:

        return {
                "map": map,
                "table": table,
                "info" : [{"type": type, "space": space}],
                "data-frame": df,
                "dict-obj": dict_obj
               }

Hi @keval

The data in dcc.Store needs to be in JSON format for the browser. Here is one common way to do this:

return {
     "data-frame": df.to_dict("records")
}

Then when you use it in a callback you can turn it back to a DataFrame - per your example with your variable names above it would be something like:

dff = pd.DataFrame(c_store["data-frame"])
1 Like

@AnnMarieW

This approach makes sense. However, for some reason, my callback to store the elements in data property of dcc.Store in tab1.py is not being triggered.

Based on the contents of the file above, any thoughts on why this could happening? c_store is not populated with any data. No errors are logged as well.

@keval It’s hard to know exactly why the callback isn’t triggering based on what you posted. But there is some information in this topic that might be helpful: Need urgent help - app.callback function doesn't get triggered at all. What should I do? - #2 by AnnMarieW

How do I pass the dataframe to dcc.Store callback? The dataframe and dict objects are created in a separate callback. It’s not a of type dash Input which what is typically passed to the callback function.

The trigger issue is related to how I was passing the df to function callback. Below two lines don’t work, as these are not elements that are defined in the layout.

# Pandas DataFrame -- useful for stats calculation
                          Input("df","data"),
 
                          # Store Dictionary Object
                          Input("dict-obj","data")

Hmm, sorry, I can’t tell based on what you posted. Making a minimal working example can really help here. This post has more info on how to do that: How to Get your Questions Answered on the Plotly Forum

@AnnMarieW

I have added dummy df and dict elements and additional code for MWE. Hopefully, that helps explain the issue.

@keval - There is no need to include the df as an Input in the callback since it’s a global variable.

1 Like

@AnnMarieW

I modified the code a bit. The DataFrame object is not a global variable. It’s created within a separate callback through a function call. Can you please check the callback function: def generate_df(type): in the code.

I’d like to pass df to c_store callback.