繁体   English   中英

破折号中图形的条件更新

[英]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 根据单选项目条件选择要更新的图表:

@app.callback(
Output('graph???', 'figure'),
Input('graph-selector', 'value))

def update_graph(graph):
  etc....
  return figure

我的第一个想法是使用类,并使用回调调用一个实例 function ,但显然这用破折号是不可能的? 回调似乎需要原始函数。

@app.callback(
Output('graph', 'figure'),
Input('graph-selector', 'value))

graph1.update_graph(graph=1)

SyntaxError: invalid syntax

我的第二个想法是将 output 的回调告诉两个图表,但我不知道如何从更新中排除一个。

@app.callback(
    Output('graph1', 'figure'),
    Output('graph2', 'figure'))
    Input('graph-selector', 'value'))
def update_graph(graph):
   # Updates both?
  return figure

我也尝试过进行嵌套回调,其中第一个是 function,它根据无线电项目条件启动回调,但这不起作用。

@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技巧只更新一个。 找出应该更新哪一个,然后将其用于另一个。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM