gpt4 book ai didi

python - 有没有更好的方法可以通过 Plotly 使用 Dash 执行多个输出?

转载 作者:太空狗 更新时间:2023-10-29 20:23:41 26 4
gpt4 key购买 nike

正如我们在 Interactivity part of Getting started 中看到的那样,一个回调函数可以接受多个输入,但始终只有一个输出。

假设我们分别有两个 block 必须在输入更改后更新。当然,最简单的方法是为每个 block 使用相同的输入进行两次回调。问题是请求执行两次,而一次足以获取所有数据。

@app.callback(
dash.dependencies.Output('element_1', 'children'),
[dash.dependencies.Input('filter', 'value')])
def callback_element_1(filter):
return get_data(filter).el1

@app.callback(
dash.dependencies.Output('element_2', 'children'),
[dash.dependencies.Input('filter', 'value')])
def callback_element_2(filter):
return get_data(filter).el2

我找到的解决方案是将这些元素包装在单个 block 中,并通过单个请求完全重新呈现它。但在这种情况下,包装器中的所有静态内容也将被刷新,尤其是当初始元素在 DOM 中彼此相距很远时。

@app.callback(
dash.dependencies.Output('wrapper', 'children'),
[dash.dependencies.Input('filter', 'value')])
def callback_element_wrapper(filter):
data = get_data(filter)
return html.Div(
children=[
data.el1,
# more static content
data.el2,
]
)

那么也许有更优雅的方式可以通过单个请求输出两个或更多元素?

最佳答案

现在,Plotly Dash 支持单个事件中的多个输出。这是最新版本的 dash==0.38.0rc1

@app.callback(
[
Output('output1', 'children'),
Output('output2', 'children')
],
[
Input('output-btn', 'n_clicks'),
State('output-btn', 'n_clicks_timestamp')
]
)
def on_click(n_clicks, n_clicks_timestamp):
if n_clicks is None:
raise PreventUpdate

return n_clicks, n_clicks_timestamp

Git Sample

关于python - 有没有更好的方法可以通过 Plotly 使用 Dash 执行多个输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45837031/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com