So I kinda in a struggle to find the solution to this. I have callbacks to display values from a dictionary to multiple html.Divs. I saw an example using for loop for callbacks on one of the Dash documentations and decided to use it on my project so that I can avoid long list of inputs and outputs inside @app.callback.
for i in range(7):
@app.callback(
[Output('gd{}'.format(i+1), 'children'),
Output('gd{}-type'.format(i+1), 'children'),
Output('gd{}-unit'.format(i+1), 'children')],
Input('ret_data', 'data'))
def glob_output(data):
results = simulation_store(**data)
g = results[0]
gl = list(g.keys())
# print(i)
val, desc, unit = g[gl[i]]['value'], gl[i], \
g[gl[i]]['units']
return val, desc, unit
I have tons of outputs and using loop would be the way since the naming convention is just the numbering of the ids. The results from the function would have two dictionary inside a list, the first on the list would be the g. Since it is a dictionary and inside the dict there’s a nested dictionary, I kinda make a new list from the keys of the g, which I call gl.
The idea is that at each iteration, the outputs will have its value, description and units shown from the dictionary ‘g’. But when running the code, all outputs show the value, description and units of the last iteration, (in this case, 6 which is the last iteration from range (7)).
Simple test print(i) made that clear that each iteration it prints ‘6’ and the loop isnt working inside the function. But I know for a fact the loop works with the @app.callbacks since all outputs show the data from the dict, but they all show the same data, which is the last iteration of the gl list. Anyone has any idea what I did wrong?