I have a basic dash app with two graphs which I would like to update based on the condition of a radio item selection. I want to choose which graph to update:
html.Div([
dcc.RadioItems(
id='graph-selector',
options=[{'label': i, 'value': i} for i in [1, 2]],
value=1
),
])
dcc.Graph(id='graph1',
clickData={'points': [{'x': fixed_df['Abs time'].values[0]}]}
),
dcc.Graph(id='graph2',
clickData={'points': [{'x': fixed_df['Abs time'].values[0]}]}
)
I haven’t been able to figure out how to use @app.callback to choose which graph to update based on the radio item condition:
@app.callback(
Output('graph???', 'figure'),
Input('graph-selector', 'value))
def update_graph(graph):
etc....
return figure
My first idea was to use classes, and call an instance function with the callback, but apparently this is impossible with dash? Callbacks seem to want raw functions.
@app.callback(
Output('graph', 'figure'),
Input('graph-selector', 'value))
graph1.update_graph(graph=1)
SyntaxError: invalid syntax
My second idea was to tell the callback to output to both graphs, but I don’t know how to exclude one from updating.
@app.callback(
Output('graph1', 'figure'),
Output('graph2', 'figure'))
Input('graph-selector', 'value'))
def update_graph(graph):
# Updates both?
return figure
Now I’m thinking I need to use two different callbacks and just throw away one based on condition, but this is a messy solution…
Is there a better way to do this?