Just built a fairly light-weighted live dash app. It’s working but would take more than 1s to update graphs as opposed to tens of ms when updating html texts, as shown in the above screenshot. Is this normal? What could possibly go wrong here?
app.layout = html.Div(
[
html.H1("Sensor Dashboard"),
html.Div(
[
html.Div(id="live-update-text"),
html.Div([dcc.Graph(id="environmental-sensors-graph-pressure")]),
html.Div([dcc.Graph(id="environmental-sensors-graph-temperature")]),
html.Div([dcc.Graph(id="environmental-sensors-graph-humidity")]),
dcc.Interval(
id="environmental-sensors-interval",
interval=UPDATE_INTERVAL * 1000,
n_intervals=0,
),
html.Div(id="intermediate-value", style={"display": "none"}),
]
),
]
)
def plot(data, key, x_range=LIVE_FIGURE_LENGTH_SECONDS):
value = data[key][-x_range:]
fig = go.Figure(data=go.Scatter(y=value, mode="lines+markers", hoverinfo="y"))
return fig
@app.callback(
Output("live-update-text", "children"),
[Input("environmental-sensors-interval", "n_intervals")],
)
def update_environmental_data(n_intervals):
p, t, h = get_environmental_data()
msg = f"Temperature: {t}\nPressure: {p}\nHumidity: {h}\n"
return msg
@app.callback(
Output("intermediate-value", "children"),
[Input("environmental-sensors-interval", "n_intervals")],
[State("intermediate-value", "children")],
)
def update_environmental_figure(n_intervals, children):
if children is not None:
data = json.loads(children)
else:
data = defaultdict(list)
p, t, h = get_environmental_data()
data["pressure"].append(p)
data["temperature"].append(t)
data["humidity"].append(h)
return json.dumps(data)
@app.callback(
[
Output("environmental-sensors-graph-pressure", "figure"),
Output("environmental-sensors-graph-temperature", "figure"),
Output("environmental-sensors-graph-humidity", "figure"),
],
[Input("intermediate-value", "children")],
)
def update_environmental_figure(children):
data = json.loads(children)
fig_p = plot(data, "pressure")
fig_t = plot(data, "temperature")
fig_h = plot(data, "humidity")
return fig_p, fig_t, fig_h