How to abort callbacks?

Hi everyone,

I am developing a multi-page app in which I have a homepage and from there you can go to few different pages.
When I move to one of these pages I have a callback that is fired and shows some plots. In all these pages I also have a link to go back to the homepage.

Now consider I have entered in one page and I immediately click on the home button/link. In this case I do not want the plots to be generated and I want to abort the “plotting callback”. However, I do not know how to do that.

I am thinking about a callback for the home link that when is clicked it does something, but I am not sure what it should do. How do I say “abort all the callbacks currently running?”.

Any help on this?

Thank you all!

2 Likes

I have same issue too!
There are too many unnecessary requests and it occurs server loads.

Since I don’t see the code, I will assume that you have a callback in which the input is the path to the pages.

Since you have the pathname as the input to the call back for plotting of the graph, you can check when the pathname is home and you can use PreventUpdate to prevent the callback from firing. E.g like below

from dash.exceptions import PreventUpdate

@app.callback(
    Output("page-content", "children"),
    [Input("url", "pathname")]
)

def plot_graph(pathname):
     if pathname == "/":
        raise PreventUpdate

Read more on that here Advanced Callbacks | Dash for Python Documentation | Plotly