When I click the button with the id ‘button_click’ the second callback gets triggered too.
I want the second callback to be triggered with the second input button.
@app.callback(
Output(component_id='add_stations', component_property='children'),
Input(component_id='button_click', component_property='n_clicks'),
State(component_id='no_of_stations', component_property='value'),
prevent_initial_call=True
)
def update_output(n_clicks, no_of_stations):
if n_clicks is None or n_clicks == 0: # Output works when clicked on button create
raise PreventUpdate
if no_of_stations < 2: # Stations cannot be less than 1
return(dbc.Row(dbc.Col(dbc.Alert('Error: Enter a Value Greater than 2!', color='danger')))) # Error if staions are less than 2
else:
stations = Patch()
stations.clear()
# Creates the inputs for station name and chainage depending on the number of stations
stations.append(html.H3('Enter Station and Chainage Details:'))
for i in range(no_of_stations):
stations.append(
dbc.Row([
dbc.Col(dbc.Label('Station Name'), width='2', style={'margin-top': '0.5rem', 'text-align': 'right'}),
dbc.Col(dbc.Input(type='text', placeholder=f'Enter Station Name {i+1}', id={'type': 'station_name_value', 'index': i}), width='4'),
dbc.Col(dbc.Label('Chainage'), width='2', style={'margin-top': '0.5rem', 'text-align': 'right'}),
dbc.Col(dbc.Input(type='number', placeholder=f'Enter Chainage {i+1} (In Km)', id={'type': 'chainage_num_value', 'index': i}), width='4'),
], style={'margin-bottom': '0.5rem'}),
)
stations.append(html.Br())
stations.append(dbc.Button("Generate Graph", color='primary', id={'type': 'create_button', 'index': 1}, style={'margin-left': '50%'})) # Button to generate graph
return stations
@app.callback(
Output('graph', 'src'),
Input({'type': 'create_button', 'index': ALL}, 'n_clicks'),
[State('headway', 'value'),
State('dwell_time', 'value'),
State({'type': 'station_name_value', 'index': ALL}, 'value'),
State({'type': 'chainage_num_value', 'index': ALL}, 'value')],
prevent_initial_call = True,
)
def plot_graph(n_clicks, headway, dwell_time, station_name, station_dis):
if n_clicks is not None:
fig_bytes = generate_matplotlib_graph(headway, dwell_time, station_name, station_dis)
fig_base64 = base64.b64encode(fig_bytes).decode('utf-8')
print(ctx.triggered_id)
print(ctx.inputs)
print(n_clicks)
return f'data:img/png;base64, {fig_base64}'