Say I have a model with 20 parameters and I made one input component for each param.
[dcc.Input(type = 'number', id = 'input %i'%i) for i in range(20)]
I want to have one button html.Button('populate parameters', id = 'button populate')
that is supposed to populate best pre-fitted value for all the inputs.
Code should look like below, except it doesn't work.
for i in range(20):
@app.callback(
dash.dependencies.Output('input %i'%i, 'value'),
[dash.dependencies.Input('button populate', 'n_clicks')]
)
def update(ignore):
return np.random.uniform()
Do I have to write 20 callbacks for each output with identical functionality? I can't find a way to make them in one go (loop?)
I have dealt with the same issue and found a solution. What you do is bypass the decorator and call the
app.callback
function directly:you can have as many input parameters/argument on a callback as you want. But only one output.
What solved a similar case for me was:
State()
as opposed toInput()
does not trigger the callback when an input value is changed.n_clicks
changes +1 with each click but doesn't need to be used.If your parameters are dependent on each other you would need more callbacks. But...with 20 parameters
There must be a better way
I have done something similar to populate my layout component after page reload.
Thanks to the first callback, the state of the components are store in a dcc.Store component. The second callback is to populate the layout components when their state changes or when you access the tab ( the layout is in a dcc.Tabs)
Note that you won't have access to the iterated values (component_id and component_property) inside the function ( set_back_component(...) )