Dash callback Output value clears the persistence of the dcc components

Hi everyone,

I’m trying to retain the last selected value in dcc.Checklist even after refreshing the page. However, I’ve encountered an issue with persistence: when I set the checklist value through a callback, the persistence is cleared, and after the page refresh, I can’t get the latest selected value.

Here’s a minimal example:

from dash import Dash, dcc, html, Input, Output

app = Dash(__name__)

app.layout = html.Div([
    html.Button("Select All", id="select_all_button"),
    dcc.Checklist(
        id='checkbox',
        options=[1, 2, 3],
        value=[],
        persistence=True,
        persistence_type='session'
    ),
])


@app.callback(
    Output('checkbox', 'value'),
    Input('select_all_button', 'n_clicks'),
    prevent_initial_call=True
)
def select_all_options(n_clicks):
    return [1, 2, 3]


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

dash_persistence_cleared

Is there any way to programmatically manage _dash_persistence storage from within a Dash callback?
I’ve tried working with both clientside_callback and set_props, but neither approach seems to affect the persistence storage as expected.

1 Like

Don’t know whether you saw these before? It looks to me like this is a long-standing bug - check @tcbegley’s response in particular.

Thanks for linking similar questions @antony.milne.

One of the comments in the first thread you sent seems to pinpoint the core issue in the source code.

I wonder if Dash sees this as a bug? If so, is there a plan to fix it, and would any assistance be helpful?

I hunted around a bit more and found these:

Key quote:

That’s as intended: if a value is set via callback, then the thing to be persisted should be the user input that led to that value, and that callback-set value will flow from the persisted user input. But I’m curious what use case you had in mind for callback outputs to be persisted?

ie if we need a single rule for this, I think the current behavior is correct, but there’s clearly an appetite for more flexibility so if we can understand it we can certainly think about an extension of the feature.

Given that [BUG]Component properties set through dynamic callbacks cannot be persisted. · Issue #2678 · plotly/dash · GitHub was closed only recently in the great stale issue cleanse and that Alex Johnson seemed open to thinking about changing this behaviour (or enabling some option that would switch between the current behaviour and our desired one), let’s post on that issue and try to get it re-opened.

1 Like