Plotly Dash and Raspberry pi

Hello,
I have a question. I have written a python script in a raspberry pi3 to get data from a sensor and plot it with dash in Browser once per second. It works very well with pretty good accuracy when I do not do anything in browser, whoever when I open a new tab or whatever the dash starts to get two values or more per second or starts to delay for more than a second!
has any one idea why or how to fix it?
thanks

Hello @alejandrosando,

It’s really hard to say without seeing your code. Could you please provide a snippet, especially with your callback?

1 Like

It all depends on the workflow of your application. How is the data being sent from the sensor to your application? How often is the Dash application set to update?
There are many different ways this could be accomplished so extra information is useful.

1 Like

thats the callback:

		
app.layout = html.Div([ html.H1("RASPI"),  dcc.Graph(id='live-graph'),dcc.Interval(id='update_Graph', interval = 1000,  max_intervals = -1),
			daq.BooleanSwitch(id='switch', on=True),html.Div(id='boolean-switch-output-1') 
			])
	
#update graph in realtime 
@app.callback( Output('live-graph', 'figure'),
               Input('update_Graph', 'n_intervals'),
               Input('switch', 'on'),
              
               )
            
def update_Graph(n , on):	
	if on == True:
		x2 = monotonic() - x1
		Timer(1 - x2, sampledata).start()
		conn = sqlite3.connect(raspi)
		df = pd.read_sql_query("SELECT * FROM sensor ORDER BY timestamp DESC ", conn)
		row = df.iloc[:n,:]
		xs =  asarray(row[df.columns[0]])
		k1 = asarray(row[df.columns[1]])
		data = [  go.Scatter( x = xs , y = k1 , hoverinfo='text+name+y', name='kanal1', mode= 'lines+markers', ),]
		layout = go.Layout(xaxis = dict(range=[min(xs, default=0),max(xs, default=0)]), yaxis = dict(range=[-1,6]))
		figure = go.Figure({'data' :data, 'layout' : layout})
		figure 
		return figure
		
	else:
		no_update
		return no_update
    

sampledata is the function that’s read data from the sensor and save it into a SQLite db.

When you say open a tab, are you opening the same application?

I mean when I do literally anything. I use Google Chrome with Dash to show the graph when I open a new tab or write or reload anything in Google Chrome then it will get delays or twice measurement per second.

It sounds like your requests are piling up.

Are you using the raspberry pi at the same time? Is the sqlite db local?

Maybe try printing the time that the process is taking to return your figure.

ie

def update_Graph(n , on):	
	if on == True:
        start = time.time()
		x2 = monotonic() - x1
		Timer(1 - x2, sampledata).start()
		conn = sqlite3.connect(raspi)
		df = pd.read_sql_query("SELECT * FROM sensor ORDER BY timestamp DESC ", conn)
		row = df.iloc[:n,:]
		xs =  asarray(row[df.columns[0]])
		k1 = asarray(row[df.columns[1]])
		data = [  go.Scatter( x = xs , y = k1 , hoverinfo='text+name+y', name='kanal1', mode= 'lines+markers', ),]
		layout = go.Layout(xaxis = dict(range=[min(xs, default=0),max(xs, default=0)]), yaxis = dict(range=[-1,6]))
		figure = go.Figure({'data' :data, 'layout' : layout})
		figure 
        end = time.time()
        print(end - start)
		return figure

From here, you can see if you have any lag.

thanks for the hint
when I use Google Chrome during the dash then increase the process time from 0.07 to 0.12 or 0.11

1 Like

Since there is a delay, what can happen is things will continue to stack up even more so because of the network requests back and forth.

Something else you can look at is your network tab for the post requests and the time it takes for the round trip.

sorry, I did not understand it , what should I do?

You are trying to get updates every second, could you slow it down?

Are you running the server on your Pi and also using it to browse? If so, what’s your specs?

I am trying to get update once per second. the server is on the pi and I open the broswer on pc.

What are the specs on the Pi?

What is the api being used for, other than just the dash server?

pi 3 B+
I use the api to get data from an ADC and dash server

I don’t know if I’d use the Pi to host the dash server.

Can you pull up the resource monitor on the Pi once you start watching the requests come in?