First post here, I hope you can help me with this weird situation… I’m working on some realtime plotting with plotly (dcc.Interval) and I just find one of the functions I’m using for getting my data is being called twice during the execution. This is very strange as I’m just calling this function once and it’s really messing with my code:
import dash
from dash.dependencies import Output, Input
import dash_core_components as dcc
import dash_html_components as html
import plotly
import plotly.graph_objs as go
import random
X = []; X.append(0)
Y = []; Y.append(1)
# THIS IS AN EXAMPLE OF A FUNCTION
def formula():
er = 'text'
return(er)
# CALLING THE FORMULA, JUST PRINTING THE TEXT
print(formula())
app = dash.Dash(__name__)
app.layout = html.Div(children=[
html.H1(children='Ejemplo grafica'),
dcc.Graph(id='live-graph', animate=True),
dcc.Interval(
id='graph-update',
interval=1000
),
]
)
@app.callback(Output('live-graph', 'figure'),
[Input('graph-update', 'n_intervals')])
def update_graph(input_data):
X.append(X[-1]+1)
Y.append(Y[-1]+Y[-1]*random.uniform(-0.1,0.1))
data = plotly.graph_objs.Scatter(
x=list(X),
y=list(Y),
name='Scatter',
mode= 'lines'
)
return {'data' : [data],
'layout' : go.Layout(
xaxis=dict(range=[X[0],X[-1]]),
yaxis=dict(range=[min(Y)-0.1,max(Y)+0.1]),)
}
if __name__ == '__main__':
app.run_server(host='127.0.0.1', port=8080 ,debug=True)
The output of the program is:
C:\Users\s3853339\Miniconda3\envs\spec\python.exe C:/pythonprojects/spectrometer/test_plotly.py
text # FUNCTION HAS BEEN CALLED ONCE
Dash is running on http://127.0.0.1:8080/
* Serving Flask app 'test_plotly' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
text # SECOND CALLING OF THE FUNCTION... WHAT HAPPENED HERE???