Black Lives Matter. Please consider donating to Black Girls Code today.
Learn how to use COVID-19 data in open source Dash apps. Register for the Sept 23rd webinar with IQT!

DCC items reset to default value on interval

I have a dash app built that relies on some dcc.Input and dcc.RadioItems. I also have interval components for updating a table. Every time the interval component fires, it resets the other dcc components back to their original values I set in the layout. How can I stop the resetting on interval?

You could probably add a RadioItem that toggles the dcc.Interval's interval attribute on and off (or at least, short and long)

Here’s a simple example :slight_smile:
toggle-interval

import dash_core_components as dcc
import dash_html_components as html
import dash
import datetime

app = dash.Dash()

app.layout = html.Div([
    dcc.Interval(id='my-interval'),
    dcc.RadioItems(id='set-time',
        value=1000,
        options=[
            {'label': 'Every second', 'value': 1000},
            {'label': 'Every 5 seconds', 'value': 5000},
            {'label': 'Off', 'value': 60*60*1000} # or just every hour
        ]),
    html.Div(id='display-time')
])


@app.callback(
    dash.dependencies.Output('display-time', 'children'),
    events=[dash.dependencies.Event('my-interval', 'interval')])
def display_time():
    return str(datetime.datetime.now())


@app.callback(
    dash.dependencies.Output('my-interval', 'interval'),
    [dash.dependencies.Input('set-time', 'value')])
def update_interval(value):
    return value

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