I can’t seem to get the callback decorator to work in a loop for my problem:
(Making this post in case the issue is more general (title))
I looked into this post for my problem:
However, it doesn’t seem to work in my case. I posted my code in a stack overflow post: python - Plotly Dash: Callback Decorator in Loop Not Possible? - Stack Overflow
But basics resembles:
app = dash.Dash()
app.config['suppress_callback_exceptions'] = True
app.layout = html.Div([
html.Button('Create Cell', id='cell-geometry-button', n_clicks=0),
html.Div(id='cell-geometry-config-container'),
html.A(id='click-register'),
])
num_clicks = 0
# Initiate cell geometry config with button
@app.callback(
Output('cell-geometry-config-container', 'children'),
[Input('cell-geometry-button', 'n_clicks')],)
def invoke_cell_geometry_options(n_clicks):
geometry_ui_list = []
global num_clicks
num_clicks = n_clicks
for i in range(n_clicks):
graph_id = 'cell-graph-{}'.format(i)
planes_list_id = 'planes-list-{}'.format(i)
button_id = 'fill-region-button-{}'.format(i)
click_register_id = 'click-register-{}'.format(i)
geometry_ui_list.extend([dcc.Graph(id=graph_id),
dcc.Input(id=planes_list_id, placeholder='Enter list of radial planes (comma separated)',
type="text"),
html.Button('Fill Region', id=button_id, n_clicks=0),
html.A(id=click_register_id),
html.Br(),
])
options = html.Div(geometry_ui_list)
return options
@app.callback(
Output('cell-geometry-config-container', 'children'),
[Input('cell-geometry-button', 'n_clicks')],)
def invoke_cell_geometry_options(n_clicks):
geometry_ui_list = []
global num_clicks
num_clicks = n_clicks
for i in range(n_clicks):
graph_id = 'cell-graph-{}'.format(i)
planes_list_id = 'planes-list-{}'.format(i)
button_id = 'fill-region-button-{}'.format(i)
click_register_id = 'click-register-{}'.format(i)
geometry_ui_list.extend([dcc.Graph(id=graph_id),
dcc.Input(id=planes_list_id, placeholder='Enter list of radial planes (comma separated)',
type="text"),
html.Button('Fill Region', id=button_id, n_clicks=0),
html.A(id=click_register_id),
html.Br(),
])
options = html.Div(geometry_ui_list)
return options
for val in range(num_clicks):
@app.callback(
Output('click-register-{}'.format(val), 'children'),
[Input('cell-graph-{}'.format(val), 'clickData')])
def click_register_function(clickData):
...
return [region, click_x, click_y]
# Fill Region
@app.callback(
Output('cell-graph-{}'.format(val), 'figure'),
[Input('planes-list-{}'.format(val), 'value'),
Input('fill-region-button-{}'.format(val), 'n_clicks')],
[State('material-dropdown', 'value'),
State('click-register-{}'.format(val), 'children')]
)
def fill_region(planes, n_clicks, selected_material, click_register):
...
return figure
if __name__ == '__main__':
app.run_server()
Is there something I am missing?