Black Lives Matter. Please consider donating to Black Girls Code today.

How to register callbacks inside a function

I’m in the following situation. My team made a framework that has a global app that parses a URL and, with the query params, selects the dashboard to render. On a specific one, the number of filters is dynamic, so we made a dynamic callback creation using a function found on this community forum. The problem is: Initially we don’t have the number of filters we need, so we have to compute it on a function and, after that, register the associated callback. But, declaring the decorator inside the function was not working. Does anyone know how to declare the decorator after I have the number of filters?

I used an example with a fixed number of 5 filters, registering it globally:

[Output(‘predictive-switch-{}’.format(i),‘value’) for i in range(5)],
[Input(‘predictive-trash-{}’.format(i), ‘n_clicks’) for i in range(5)]

And it works, but calculating the number of filters inside the rendering function and, after that, register the callback on this form:

[Output(‘predictive-switch-{}’.format(i),‘value’) for i in range(NUM_FILTERS)],
[Input(‘predictive-trash-{}’.format(i), ‘n_clicks’) for i in range(NUM_FILTERS)]
it dont register the callback on the app.

this isn’t possible right now, see the posts on “dynamic inputs and dynamic components” for workarounds. however, we are now working on a new “wildcards” feature that will relax some of these constrains. an early version is available in the dash github pull requests.

Ty @chriddyp, The solution I found was to set a fixed number of callbacks and hidden the ones that i didn’t use.