Interrupt execution of queued callbacks

Hi, I have a question about the execution of callbacks.

I have a dash app with a scatter plot and a line plot. When hovering over the markers in the scatter plot, a callback is triggered that updates the line plot. I feel like when I quickly hover over many scatter markers and the callback that updates the line plot is triggered many times, the executions of this callback queue up.
I would like to interrupt the execution of the callback whenever the same callback is triggered again. This means that, when hovering over many scatter markers, only the callback for the scatter marker that was hovered over most recently is executed and all the previous callbacks that might still be running are interrupted. Is this possible?

I hope that I could make clear what I am trying to achieve.

Thank you :slightly_smiling_face:

HI @christoph.b

Something like this might work, but I’m not sure if this makes your chart more responsive.

import json
from dash import Dash, DiskcacheManager, dcc, Input, Output, html, callback
import diskcache
import plotly.graph_objects as go
import numpy as np

cache = diskcache.Cache("./cache")
background_callback_manager = DiskcacheManager(cache)

app = Dash(__name__, background_callback_manager=background_callback_manager)

app.layout = html.Div(
    [
        dcc.Graph(
            id='graph',
            figure=go.Figure(
                go.Scatter(
                    x=np.arange(50),
                    y=np.random.randint(0, 1000, 50),
                    mode='markers'
                )
            )
        ),
        html.Pre(id='out')
    ]
)


@callback(
    output=Output("out", "children"),
    inputs=Input("graph", "hoverData"),
    background=True,
    cancel=[Input("graph", "hoverData")],
)
def update_clicks(hoverData):
    return json.dumps(hoverData, indent=2)


if __name__ == "__main__":
    app.run(debug=True)

Hey @christoph.b!

Were you able to solve this problem? I’m running into the same issue!

I tried using BlockingCallbackTransform() from dash-extensions but I couldn’t get it to work with pages. Let me know if you have a solution!

Hey @winstonchiong, just out of curiosity, did you try what I proposed back then?

I’ve never really gotten background callbacks to work unfortunately, so I didn’t go that route. Celery is recommended for production but Celery requires some infrastructure.