我正在尝试在仪表板应用程序的回调中使用数据生成器。想法是绘制一些在数据生成器函数中正在更新的值。生成器是使用yield创建的,我的问题是如何在仪表板应用程序中以正确的方式使用生成器。以下是一些有助于澄清问题的信息:
# 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)
在此问题上的任何帮助,我将非常感谢。
StopIteration
异常表示that there are no further items produced by the iterator。根据看到的错误,似乎该错误与Dash应用程序无关,而与迭代器的实现有关(它用完了数据)。