破折号中图形的条件更新
Conditional updating of graphs in dash
我有一个基本的破折号应用程序,其中包含两个图表,我想根据单选项目选择的条件对其进行更新。我想使用单选项选择要更新的图表:
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]}]}
)
一直想不通如何使用@app.callback根据radio item条件选择更新哪个图表:
@app.callback(
Output('graph???', 'figure'),
Input('graph-selector', 'value))
def update_graph(graph):
etc....
return figure
我的第一个想法是使用类,并通过回调调用一个实例函数,但显然这对破折号来说是不可能的?回调似乎需要原始函数。
@app.callback(
Output('graph', 'figure'),
Input('graph-selector', 'value))
graph1.update_graph(graph=1)
SyntaxError: invalid syntax
我的第二个想法是告诉回调输出到两个图,但我不知道如何从更新中排除一个。
@app.callback(
Output('graph1', 'figure'),
Output('graph2', 'figure'))
Input('graph-selector', 'value'))
def update_graph(graph):
# Updates both?
return figure
我也试过进行嵌套回调,其中第一个是根据单选项条件启动回调的函数,但这没有用。
@app.callback(
Input('graph-selector', 'value'))
def select_graph(selector):
if selector==1:
@app.callback(
Output('graph1', 'figure'),
Input('updater', 'value'))
def update_graph(update):
return figure
if selector==2:
@app.callback(
Output('graph2', 'figure'),
Input('updater', 'value'))
def update_graph(update):
return figure
我根本不使用无线电项目,而是使用两个具有相同更新功能的不同回调系统。这不是一个理想的解决方案。
@app.callback(
Output('graph1', 'figure')
Input('updater1', 'value'))
def update_graph(update):
return figure
@app.callback(
Output('graph2', 'figure')
Input('updater2', 'value'))
def update_graph(update):
return figure
有更好的方法吗?
这最接近我在这种情况下的建议:
@app.callback(
[
Output('graph1', 'figure'),
Output('graph2', 'figure'),
],
Input('graph-selector', 'value'))
def update_graph(graph):
return figure, dash.no_update
您可以使用 no_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]}]}
)
一直想不通如何使用@app.callback根据radio item条件选择更新哪个图表:
@app.callback(
Output('graph???', 'figure'),
Input('graph-selector', 'value))
def update_graph(graph):
etc....
return figure
我的第一个想法是使用类,并通过回调调用一个实例函数,但显然这对破折号来说是不可能的?回调似乎需要原始函数。
@app.callback(
Output('graph', 'figure'),
Input('graph-selector', 'value))
graph1.update_graph(graph=1)
SyntaxError: invalid syntax
我的第二个想法是告诉回调输出到两个图,但我不知道如何从更新中排除一个。
@app.callback(
Output('graph1', 'figure'),
Output('graph2', 'figure'))
Input('graph-selector', 'value'))
def update_graph(graph):
# Updates both?
return figure
我也试过进行嵌套回调,其中第一个是根据单选项条件启动回调的函数,但这没有用。
@app.callback(
Input('graph-selector', 'value'))
def select_graph(selector):
if selector==1:
@app.callback(
Output('graph1', 'figure'),
Input('updater', 'value'))
def update_graph(update):
return figure
if selector==2:
@app.callback(
Output('graph2', 'figure'),
Input('updater', 'value'))
def update_graph(update):
return figure
我根本不使用无线电项目,而是使用两个具有相同更新功能的不同回调系统。这不是一个理想的解决方案。
@app.callback(
Output('graph1', 'figure')
Input('updater1', 'value'))
def update_graph(update):
return figure
@app.callback(
Output('graph2', 'figure')
Input('updater2', 'value'))
def update_graph(update):
return figure
有更好的方法吗?
这最接近我在这种情况下的建议:
@app.callback(
[
Output('graph1', 'figure'),
Output('graph2', 'figure'),
],
Input('graph-selector', 'value'))
def update_graph(graph):
return figure, dash.no_update
您可以使用 no_update
技巧只更新一个。弄清楚应该更新哪一个,然后将其用于另一个。