I’ve written code below, but not working. It reads data from mysql and then display in real time. When I run, it is not working -not plotting and and moving as it should be. Any help is appreciated.
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Output,Event
import mysql.connector
import plotly
import plotly.graph_objs as go
from collections import deque
import pyodbc
def mydb(host,user,passwd,db):
connMySQL=mysql.connector.connect(
host=host,
user=user,
passwd=passwd,
db=db,
autocommit = True
)
return connMySQL
name_title = ‘Stats from MySQL’
app=dash.Dash(name)
app.layout = html.Div(children=[
html.H1(children = ‘Read real-time data from MySQL on scatter plot’),
dcc.Graph(
id='example-graph',
animate=True),
dcc.Interval(
id='graph-update',
interval=1*1000),
])
@app.callback(Output(‘example-graph’,‘figure’),
events=[Event(‘graph-update’,‘interval’)])
def update_graph_scatter():
dataSQL= []
X= deque(maxlen=10)
Y= deque(maxlen=10)
sql_conn = mydb(' ....')
cursor=sql_conn.cursor()
cursor.execute("SELECT id,windsp FROM ...")
rows = cursor.fetchall()
for row in rows:
dataSQL.append(list(row))
labels=['id','windsp']
df = pd.DataFrame.from_records(dataSQL,columns=labels)
X= df ['id']
Y= df ['windsp']
data = plotly.graph_objs.Scatter(
x=list(X),
y=list(Y),
name = 'Scater',
mode = 'lines+markers'
)
return{
'data':[data],'layout':go.Layout(
xaxis=dict(range = [min(X),max(X)]),
yaxis=dict(range =[min(Y),max(Y)]),
)
}
if name == “main”:
app.run_server(debug=True)