dcc.Store

The dcc.Store component is used to store JSON data in the browser.

For more information and examples, refer to Part 4 of the Dash tutorial on Sharing Data Between Callbacks.

Examples

Share Data Between Callbacks

```python
from dash import Dash, html, dcc, Input, Output, callback, no_update
import dash_ag_grid as dag
import plotly.express as px
import pandas as pd

app = Dash()

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv')

app.layout = html.Div([
    dcc.Store(id='memory-output'),
    dcc.Dropdown(
        df.country.unique(),
        ['Canada', 'United States'],
        multi=True,
        id='memory-countries',
    ),
    dcc.Dropdown(
        {'lifeExp': 'Life expectancy', 'gdpPercap': 'GDP per capita'},
        'lifeExp',
        id='memory-field'
    ),
    dcc.Graph(id='memory-graph'),
    dag.AgGrid(
        id='memory-table',
        columnDefs=[{'field': i} for i in df.columns],
        dashGridOptions={"pagination": True}
    )
])


@callback(Output('memory-output', 'data'), Input('memory-countries', 'value'))
def filter_countries(countries_selected):
    if not countries_selected:
        return df.to_dict('records')
    dff = df[df['country'].isin(countries_selected)]
    return dff.to_dict('records')


@callback(Output('memory-table', 'rowData'), Input('memory-output', 'data'))
def update_table(data):
    if data is None:
        return no_update
    return data


@callback(Output('memory-graph', 'figure'), Input('memory-output', 'data'), Input('memory-field', 'value'))
def update_graph(data, field):
    if data is None:
        return no_update
    dff = pd.DataFrame(data)
    fig = px.line(
        dff,
        x='year',
        y=field,
        color='country',
        markers=True,
        title=f'{field} over time'
    )
    return fig


if __name__ == '__main__':
    app.run(debug=True)
```

Store Clicks

```python

from dash import Dash, html, dcc, Output, Input, State, callback, MATCH, no_update

# This example uses pattern matching callbacks since it needs 2 callbacks for each button.
# For more info see https://dash.plotly.com/pattern-matching-callbacks

app = Dash(__name__)

app.layout = html.Div([
    html.Div("Click to store in:"),

    # The memory store reverts to the default on every page refresh
    dcc.Store(id={'type': 'storage', 'index': 'memory'}),

    # The local store will take the initial data
    # only the first time the page is loaded
    # and keep it until it is cleared.
    dcc.Store(id={'type': 'storage', 'index': 'local'}, storage_type='local'),

    # Same as the local store but will lose the data
    # when the browser/tab closes.
    dcc.Store(id={'type': 'storage', 'index': 'session'}, storage_type='session'),

    html.Button('Memory Storage', id={'type': 'button-storage', 'index': 'memory'}),
    html.Button('Local Storage', id={'type': 'button-storage', 'index': 'local'}),
    html.Button('Session Storage', id={'type': 'button-storage', 'index': 'session'}),
    html.Hr(),

    html.Div([html.Span(0, id={'type': 'output-storage', 'index': 'memory'}), " Memory Clicks"]),
    html.Div([html.Span(0, id={'type': 'output-storage', 'index': 'local'}), " Local Clicks"]),
    html.Div([html.Span(0, id={'type': 'output-storage', 'index': 'session'}), " Session Clicks"]),
])


@callback(
    Output({'type': 'storage', 'index': MATCH}, 'data'),
    Input({'type': 'button-storage', 'index': MATCH}, 'n_clicks'),
    State({'type': 'storage', 'index': MATCH}, 'data'))
def on_click(n_clicks, data):
    if n_clicks is None:
        return no_update

    # Give a default data dict with 0 clicks if there's no data.
    data = data or {'clicks': 0}

    data['clicks'] = data['clicks'] + 1
    return data

# Since we use the data prop in an output, we cannot get the initial data on load with the data prop.
# Instead, we use the modified_timestamp as Input and the data as State.
@callback(
    Output({'type': 'output-storage', 'index': MATCH}, 'children'),
    Input({'type': 'storage', 'index': MATCH}, 'modified_timestamp'),
    State({'type': 'storage', 'index': MATCH}, 'data'))
def on_data(ts, data):
    if ts is None:
        return no_update
    data = data or {}
    return data.get('clicks', 0)


if __name__ == '__main__':
    app.run(debug=True)
```

Retrieving the Initial Store Data

If you use the data prop as an output, you cannot get the
initial data on load with the data prop. To counter this,
you can use the modified_timestamp as Input and the data as State.

Storage Limitations


Store Properties

Access this documentation in your Python terminal with:
```python

help(dash.dcc.Store)
```

Our recommended IDE for writing Dash apps is Dash Enterprise’s
Data Science Workspaces,
which has typeahead support for Dash Component Properties.
Find out if your company is using
Dash Enterprise
.

id (string; required):
The ID of this component, used to identify dash components in
callbacks. The ID needs to be unique across all of the components in
an app.

storage_type (a value equal to: ‘local’, ‘session’ or ‘memory’; default 'memory'):
The type of the web storage. memory: only kept in memory, reset on
page refresh. local: window.localStorage, data is kept after the
browser quit. session: window.sessionStorage, data is cleared once the
browser quit.

data (dict | list | number | string | boolean; optional):
The stored data for the id.

clear_data (boolean; default False):
Set to True to remove the data contained in data_key.

modified_timestamp (number; default -1):
The last time the storage was modified.