Plotly Dash dcc.Interval Disabled Boolean Documentation

Viewed 1503

Trying to determine if this dcc.interval disabled boolean property is the only way to disable the callback function. If so, do I simply add it to the html.div and a button appears? The documentation doesn't seem very clear. I don't see an example anywhere of its' usage.

Is it this easy:

app.layout = html.Div(
    html.Div([
        #html.H4('my dash/plotly graph'),
        dcc.Graph(id='live-update-graph'),
        dcc.Interval(
            id='interval-component',
            interval=1*1000, # in milliseconds
            n_intervals=0,
       -->  disabled=False   <--
        )
    ])
)

And the a button appears, or is it more involved? Like setting a button, setting your inputs/ouputs in the callback decorator and so on?

If so, it seems overly involved for just wanting to stop the callback.

1 Answers

Edit at bottom

Whatever callback function you hook up to listen to the interval component will fire as often as you set the interval for. If you want to stop that callback, you need a way to set the interval's disabled prop to False. You could do that by creating a button with a callback to update that prop, and every time the user clicks it, the function will switch whether the prop is True or False.

And the a button appears, or is it more involved? Like setting a button, setting your inputs/ouputs in the callback decorator and so on? The latter. You'd have to rig everything up yourself.

Under what conditions would you want to stop a callback? Do you actually want an interval that will run forever, updating every X seconds? Would your app work better if you manually triggered the callback instead? That could be less to set up.

Example callback to disable interval:

With something like this in your layout:

html.Button(id='start-stop-button', label='Start/Stop')


@app.callback(
    Output('interval-component', 'disabled'),
    [Input('start-stop-button', 'n_clicks')],
    [State('interval-component', 'disabled')],
)
def callback_func_start_stop_interval(button_clicks, disabled_state):
    if button_clicks is not None and button_clicks > 0:
        return not disabled_state
    else:
        return disabled_state
Related