using data generator inside dash/plotly callbacks

Viewed 595

I am trying to employ a data generator inside a callback of a dash app. The idea is to plot some values which are being updated within a data generator function. The generator is created using yield and my question is that how can I employ the generator in an correct way in dash applications. Here are some information that might be helpful for problem clarification:

# generator
def generator():
    while True
        # do some calculations
        yield output 

and some information about the app itself:

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

It should be reminded the generator is already tested and next(generator) is producing correct values for each call; also, the dash app is working perfect without generator, but the combination results in errors like:

Callback error updating live-graph_1.figure, live-graph_2.figure, live-graph_3.figure
StopIteration
new_data = next(generator)

I would really appreciate any help on this matter.

1 Answers
Related