Set custom slider value from backend on initialization

Hello I have a simple Dash app with a slider component

        dcc.Slider(
            id='temp-slider',
            min=18,
            max=25,
            step=0.5,
            value=DEFAULT_TEMP,
            marks={i: '{}°C'.format(i) for i in range(18, 26)},
            persistence=True
        ),

and a callback that writes the current slider position to a file which is read by a backend

@app.callback(Output('temp-slider', 'value'),
              [Input('temp-slider', 'value')])
def update_slider(value):
    json.dump({'setpoint': value}, open('setpoint.json', 'w'))
    return value

The problem is whenever I open a new instance (e.g. new tab) to my server the slider will reinitialize to DEFAULT_TEMP.

Can anyone suggest a simple fix to have the slider value set from the file on the app launch for every user?

Thanks

I’m not sure if this is helpful but I would look into persistence

persistence (boolean | string | number ; optional): Used to allow user interactions in this component to be persisted when the component - or the page - is refreshed. If persisted is truthy and hasn’t changed from its previous value, a value that the user has changed while using the app will keep that change, as long as the new value also matches what was given originally. Used in conjunction with persistence_type .

1 Like