Plotly Dash Callback initialization insanely slow

I have 200 toggle switches on my page, with 200 callbacks to do things with them obviously. They initialize very quickly. But, I added a single button to “TOGGLE ALL ON/OFF” that makes all 200 toggles turn on or off. After adding this control, and the callback with 200 corresponding outputs, the app initialization now takes 20+ seconds.

This is the callback causing the problems:

#Enforce-All button
outputList=[]
#loop to fill outputList
for i in range(0,maxNumInvCards):
    outputList.append(Output(str(i)+'-enforce', 'on' ))
@app.callback(outputList,
              [Input('enforce-all-button','n_clicks_timestamp'),
               Input('disable-all-button','n_clicks_timestamp')])
def enforce_all(enforce_all_ts, disable_all_ts):
    returnList=[]
    if not enforce_all_ts:
        enforce_all_ts=0
    if not disable_all_ts:
        disable_all_ts=0
    if disable_all_ts > enforce_all_ts:
        for i in range(0,maxNumInvCards):
            returnList.append(False)
    else:
        for i in range(0,maxNumInvCards):
            returnList.append(True)
    return returnList

Hm, not sure exactly what’s going on here but I highly recommend checking out the new pattern matching callbacks for use cases like this, it might speed things up.

1 Like

OK thanks I will search for that. As far as whats going on, I think the output of this callback is the input of the other callbacks so, its firing everything a whole lot more than it needs to fire for the initialization. Maybe?

@chriddyp The pattern matching callback stuff looks like EXACTLY what I need. The reason I have 200+ elements is because I don’t know how many the user is going to need, so I render and wire-up 200 of them as overkill.

Thanks to you and the team for an awesome product that keeps getting better.

-Nathan

1 Like