Basically, I want to add 1-6 live update figures into my page. The actual number is decided by some condition run-time’ly. My current solution is to still declare the callbacks statically, and update the layout dynamically. So the ‘app.config.supress_callback_exceptions’ is needed. Once one of the maximum six graph object is added to the layout, the corresponding callback get fired every update interval. This concept basically works.
My original idea was to call the make_graph_callback as needed so that only callbacks that is needed is declared. That would make it possible to support variant number of graphs in a single page. But I didn’t find a right place to call this function.
Does anybody has a better solution than this?
app = dash.Dash()
app.config.supress_callback_exceptions=True
def make_graph_callback(index):
@app.callback(Output('live-update-graph'+str(index), 'figure'),
events=[Event('interval-component-update-graph', 'interval')])
def graph_callback():
### get data to gdf ###
return {
'data': [
go.Scatter(
name=field,
mode='lines',
type='scatter',
x=gdf['Time'],
y=gdf[field],
line={'width':1, 'dash': 'solid','shape':'spline','smoothing':\
'0','simplify':True})for field in ['A','B','C']
],
'layout': go.Layout(
xaxis={'title': 'My Title', \
'tickformat': '%m-%d %H:%M:%S',\
},
yaxis=dict(title='Readings'),
margin={'l': 100, 'b': 60, 't': 40, 'r': 100},
)
}
return fig
def getLayout():
# get the number of graph to be shown to ngraph
for i in range(ngraph):
graphs.append(html.Div(dcc.Graph(id='live-update-graph'+str(i))))
return html.Div([
html.Div(graphs,id='graphs'),
dcc.Interval(
id='interval-component-update-graph',
interval=3*1000 # in milliseconds
),
dcc.Interval(
id='interval-component-update-layout',
interval=6*1000 # in milliseconds
)
])
app.layout = getLayout
make_graph_callback(0)
make_graph_callback(1)
make_graph_callback(2)
make_graph_callback(3)
make_graph_callback(4)
make_graph_callback(5)
@app.callback(Output('graphs', 'children'),
events=[Event('interval-component-update-layout', 'interval')],
state=[dash.dependencies.State('graphs','children')])
def update_graph_layout(current_graphs):
# get the number of graph to be shown to ngraph
for i in range(ngraph):
graphs.append(html.Div(dcc.Graph(id='live-update-graph'+str(i))))
return graphs
if __name__ == '__main__':
app.run_server(debug=False, port=8090, host='0.0.0.0',processes=3)