I am struggling to understand why the following code is not updating the figure. My intention is to add a horizontal line to the chart if a particular condition is satisfied, but for some reason, the code below doesn’t update the figure. I would appreciate any help on this issue.
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
import plotly.graph_objects as go
from dash.dependencies import Input, Output, State
from dash.exceptions import PreventUpdate
if __name__ == "__main__":
app = dash.Dash()
layout = [dcc.Graph(id="graph", figure=px.scatter(),
style={"height": "75vh"}),
dcc.Interval(id="ms-beat",
interval=1000,
n_intervals=0)
]
app.layout = html.Div(children=layout)
flag = False
@app.callback(Output("graph", "figure"),
Input("ms-beat", "n_intervals"),
State("graph", "figure"))
def add_level_lines(n_intervals, figure):
global flag
if flag:
raise PreventUpdate
flag = True
figure = go.Figure(figure)
figure.add_hline(y=42, line_dash="dot", annotation_text="label")
figure.update_layout(autosize=True)
return figure
app.run_server(debug=True)
# app.run_server()