Help: Just Creating a Button in code calls its callback, even when it's not clicked

#1

I have 100 dynamically created buttons, each linked to its own callback via n_clicks_timestamp (also dynamically created). However the callbacks get called just on button creation even though they are not pressed yet. How can I avoid this?

This results in 100 callbacks one after the other, really slowing everything down.

#2

Hey, Didn’t tried it but it should work:

from dash.exceptions import PreventUpdate

@app.callback(
    Output(...)
    [Input(...)]
def button_click(n_clicks_timestamp)
    if n_click_timestamps is None:  # maybe if n_click_timestamp == 0:
        raise PreventUpdate
    Your stuff
#3

This works but doesn’t avoid the Callback. I am talking about avoiding the callback entirely.

If this can’t be done, then I think this is a bug in Dash. If the click property hasn’t changed, there is no reason to alert the callback.

Why this matters: For an app hosted on a remote server, it introduces a lot of request/response activity from client to server, and adds a ton of unneeded latency if you have 100+ buttons. This can be easily seen in Chrome Developer Tools Network tab.

#4

All callbacks are fired once when the app loads. This is the current default behaviour of the Dash renderer to ensure that the app starts from a valid state.

I can see why this is undesirable for a remotely hosted app with a large number of callbacks. It’s possible this may change in the future to avoid this performance hit, but I’m not aware of any specific plans.

#5

There’s a pretty good explanation on github. Discussion around a possible breaking change in dash 1.x / 2.x

I’m personally most interested in the use case with localStorage

2 Likes
#6

This is very useful and detailed. I will take a look - perhaps it will help me find another way of solving my problem.