【发布时间】:2020-10-27 03:06:18
【问题描述】:
我正在尝试在破折号应用的回调中使用数据生成器。这个想法是绘制一些正在数据生成器函数中更新的值。生成器是使用 yield 创建的,我的问题是如何在 dash 应用程序中以正确的方式使用生成器。以下是一些可能有助于澄清问题的信息:
# generator
def generator():
while True
# do some calculations
yield output
以及有关应用程序本身的一些信息:
app = dash.Dash(__name__)
app.layout = html.Div(
[
html.H1(children='Trial'),
dcc.Graph(id='live-graph_1', style={'float': 'left','margin': 'auto'}),
dcc.Graph(id='live-graph_2', style={'float': 'left','margin': 'auto'}),
dcc.Graph(id='live-graph_3', style={'float': 'left','margin': 'auto'}),
dcc.Interval(
id='graph-update',
interval=2*1000),
]
)
#############
## callback
#############
@app.callback([Output('live-graph_1', 'figure'),
Output('live-graph_2', 'figure'),
Output('live-graph_3', 'figure')],
[Input('graph-update', 'n_intervals')])
def update_data(input_data):
# step 1
###########################################
# use data generator to produce new data;
# which is not a simple loading or importing
# function.
###########################################
new_data = next(generator)
# step 2
# create three figures using new_data
# step 3
return fig1, fig2, fig3
应该提醒生成器已经过测试,并且 next(generator) 正在为每个调用生成正确的值;此外,dash 应用程序在没有生成器的情况下也能完美运行,但组合会导致以下错误:
Callback error updating live-graph_1.figure, live-graph_2.figure, live-graph_3.figure
StopIteration
new_data = next(generator)
我非常感谢您对此事的任何帮助。
【问题讨论】:
标签: callback generator intervals yield plotly-dash