As the others have mentioned, we’ll allow multiple callbacks targeting the same output soon.
In the meantime, I’d recommend breaking your long callback out into multiple functions. Here’s an example of how to organize your code:
@app.callback(
Output('graph', 'figure'),
Input('reset', 'n_clicks'),
Input('draw', 'n_clicks'),
prevent_initial_call=True
)
def update_graph(b1, b2):
triggered_id = callback_context.triggered[0]['prop_id']
if 'reset.n_clicks' == triggered_id:
return reset_graph()
elif 'draw.n_clicks' == triggered_id:
return draw_graph()
def draw_graph():
df = px.data.iris()
return px.scatter(df, x=df.columns[0], y=df.columns[1])
def reset_graph():
return go.Figure()
You will still have the if
/elif
statements, but don’t include any more code within those statements than necessary… just call a separate function. This will clean up your code a fair bit. Then, if you really want to remove the if/elif
, you’ll be able to refactor your code to this in a week or so:
app = Dash(__name__)
app.layout = html.Div([
html.Button('Draw Graph', id='draw'),
html.Button('Reset Graph', id='reset'),
dcc.Graph(id='graph')
])
@app.callback(
Output('graph', 'figure'),
Input('draw', 'n_clicks'),
prevent_initial_call=True
)
def draw_graph(_):
df = px.data.iris()
return px.scatter(df, x=df.columns[0], y=df.columns[1])
@app.callback(
Output('graph', 'figure'),
Input('reset', 'n_clicks'),
prevent_initial_call=True
)
def reset_graph(_):
return go.Figure()
app.run_server(debug=True)