Ulises Bussi
Ulises Bussi

Reputation: 1725

python dash persistence and callback issue

i'm trying to link in a dash app an RangeSlider with 2 inputs. Also i Would like to have persistence in those components. When I add persistence the components works well but the values are not saved. Here's a minimal example:

from dash import (Dash, html, dcc, Input, callback, ctx,
                  Output,State,no_update,callback_context)
import dash_bootstrap_components as dbc
import numpy as np

app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])

vals = np.arange(35.7, 42.2, 0.1)

slider = dcc.RangeSlider(min=0, max=100,
                         marks={vals[3*i]: f"{i:.2f}" for i in range(5)},
                         value = [36,40], id='slider',
                         persistence='Session')

inp1 = dbc.Input(id='inp1', value=36, persistence='Session')
inp2 = dbc.Input(id='inp2', value=40, persistence='Session')

app.layout = dbc.Container([
                    html.Div("", id='text' ),dbc.Row([
                    dbc.Col([ slider,inp1,inp2], ),
        ])
])

@callback(Output('inp1','value'),
          Output('inp2','value'),
          Output('slider','value'),
          [Input('inp1','value'),
          Input('inp2','value'),
          Input('slider','value'),
            ],
          prevent_initial_call=True
)
def update_vals(in1_val,in2_val,sl_val):
    trigger = ctx.triggered_id
    if trigger == 'slider':
        datadic = {'inp1':sl_val[0],'inp2':sl_val[1],'slider':sl_val}
        return sl_val[0],sl_val[1], sl_val
    return no_update,no_update,no_update



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

Can't understand why the callback and the persistance are colliding and i'm wondering if there's a solution for that.

Thanks!

Edit:


the version of libraries used:

-Python 3.10.11

-Dash 2.11.1

-Numpy 1.25.0 (nothe that numpy only creates an array unused)

-Dash_bootstrap_components 1.4.1

Upvotes: 1

Views: 69

Answers (1)

fgoudra
fgoudra

Reputation: 861

According to this thread on plotly forum, it seems it is not possible to make persistence work when the value of the component is set by a callback. Perhaps it is possible to circumvent this with a dcc.Store component.

Edit: there is an issue on the dash github repo related to this behaviour.

Upvotes: 1

Related Questions