import dash
from dash.dependencies import Output, Event
import dash_core_components as dcc
import dash_html_components as html
import plotly
import plotly.graph_objs as go
import numpy as np
from time import time
import serial
from collections import deque
x = deque(maxlen=20)
y = deque(maxlen=20)
x.append(1) #initial starting data for x
y.append(1) #initial starting data for y
app = dash.Dash(name)
app.layout =html.Div(
[
dcc.Graph(id=‘live-graph’, animate =True),
dcc.Interval(
id=‘graph-update’, #the event (fct) that will update the page
interval=100 #and it will occure every 100ms
)
]
)
@app.callback(Output(‘live-graph’, ‘figure’),
events = [Event(‘graph-update’, ‘interval’)])
def update_graph():
global x
global y
ser = serial.Serial('COM3', 9600)
start_time = time()
timepoints = []
y = []
#apdate data
y.append(float(data[0])*5.0/1024)
timepoints.append(time()-start_time)
x = timepoints[-1]
data = go.Scatter(
x = list(x),
y = list(y),
name = 'Scatter',
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)