Can I disable an interval if there is an error

I am using dcc.Interval to dynamically update graph. This, correctly, causes the webpage to reload periodically and display new data.

What I want to accomplish is that the periodic reload of the webpage gets automatically disabled if there is an error. Normally, it just keeps reloading even if Python app is terminated.

Hey @voidtrance.

Iḿ not sure how to do that. What would be the use case? I mean if there is an error and the app stopped working why would you keep the browser open?

Hello @voidtrance,

Yse, you can use the on_error for the callback to set_props on the interval to a different value, or stop it entirely.

This is very flexible to what you want to do. :slight_smile: You can even have the entire app alert you when there are errors. :smiley:

1 Like

Thanks, this was very helpful.

I have a follow up question about dcc.Interval.

The documentation states that n_intervals will be updated every interval milliseconds. But it does not talk about how often the callbacks are being called. Is there some documentation that describes the frequency of calling callbacks and how that can be controlled?

I would like to come back to the on_error situation.

I also want the webpage to stop continuously reloading. Is there a way to make that happen?

Yes, you can check out here: Interval | Dash for Python Documentation | Plotly

Should be interval. Default is 1000 or 1 second.

Not sure what you mean on this one.

You could disable debug mode to keep from hot_reloading. If the app is encountering an error, this is the default process to reload the failed state.

Hmm, I have an interval set to 50 milliseconds. I also added a print() statement in my callback and while I generally see time differences of about 0.05 seconds between calls, there are also some huge gaps.

Here is how my app is setup:

...
    app.layout = html.Div([
        html.Div([
            html.Button('Stop Refresh', id='refresh-control', n_clicks=0)
        ], title="Refresh Control"),
        html.Div([
            dcc.Graph(id="travel-plot", figure=fig),
            dcc.Interval(id="interval-component", interval=50, n_intervals=0)
        ]),
        html.Div([
            dcc.Graph(id="heater-temps", figure=tfig),
            dcc.Interval(id="temp-interval-component", interval=1000, n_intervals=0)
        ])
    ])

    start_time = time.time()

    @app.callback(Output('travel-plot', 'extendData'),
                  Input('interval-component', 'n_intervals'))
    def update_path_data(n):
        print(time.time())
        pos, temps = get_data(connection, toolhead, *heaters)
        data = {'x':[[pos[0]]], 'y': [[pos[1]]], 'z': [[pos[2]]]}
        return data
...

And here is the output that I get:

1741198379.4000185
1741198379.4341614
1741198379.5649786
1741198379.585141
1741198379.642623
1741198379.697914
1741198379.8117445
1741198384.5249014

In the last call, the gap was 5 seconds.

I also want to be able to call the callback with a higher frequency but I found that any interval value of less than 50 results in the scatter plot now showing at all.

The further you get away from the source, the longer the time between the calls will be.

I am not sure what you mean by that. The app and the browser are running on the same system. There should be hardly any latency in getting the response.

The latency, if you were to host it, you should keep in mind.

By default, dash / flask is a sync environment, meaning completion time will block other aspects of the code. You can get around this when you host, but its still in a blocking nature.

Then you also have the rendering on the clientside for the data.

Anyways, requests will keep piling up from the browser to the server since it is not async.

As far as stopping an interval:

image