Hi. I am very new to dash, and I do not have a solid understanding of how it works, so any suggestions will help me here.
I created a fairly simple app to monitor some hardware output. This is simulated using the class DataObject
.
I initialise a DataObject
object for every component that is being monitored, and I pull data using the 2 functions spec_read
and adc_get_samples
. I then plot the data using a scatter plot and a histogram, for every object, and use and dcc.Interval
to update all the plots every 10 seconds.
The issue is, the server is extremely slow, and the web browser uses a lot of CPU resources, for what I think is a simple process. Is there a way I can optimise this code? Thanks!
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import dash
import dash_table
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input,Output
import numpy as np
import time
class DataObject(object):
def __init__(self, name, *args, **kwargs):
self.name = name
self.nchans = 4096
self.rms_x = 17.
self.rms_y = 17.
def adc_get_samples(self):
x = np.random.normal(0, scale=self.rms_x, size=2*self.nchans).astype("int8")
y = np.random.normal(0, scale=self.rms_y, size=2*self.nchans).astype("int8")
return x,y
def spec_read(self):
xx = 10**(np.random.normal(size=self.nchans))
yy = 10**(np.random.normal(size=self.nchans))
return xx, yy
def get_x_data():
return np.arange(4096)
obj_names = ['object1', 'object2', 'object3',
'object4', 'object5', 'object6']
objects = [DataObject(obj_name) for obj_name in obj_names]
FIGS = {}
for obj in objects:
fig = make_subplots(rows=1, cols=2,
column_widths=[0.8, 0.2], horizontal_spacing=0.05,
column_titles=['Spectra', 'ADC values'])
x = get_x_data()
xx,yy = obj.spec_read()
adc_x, adc_y = obj.adc_get_samples()
fig.append_trace({
'x': x,
'y': 10*np.log10(xx+0.1),
'name': 'X-pol',
'mode': 'lines+markers',
'type': 'scatter'
}, 1, 1)
fig.append_trace({
'x': x,
'y': 10*np.log10(yy+0.1),
'name': 'Y-pol',
'mode': 'lines+markers',
'type': 'scatter'
}, 1, 1)
fig.append_trace(
go.Histogram(x=adc_x, name='', marker_color='blue'),
1, 2)
fig.append_trace(
go.Histogram(x=adc_y, name='', marker_color='red'),
1, 2)
fig.update_layout(
title="<b>%s<b>" %obj.name,
margin=dict(l=30, r=30, b=50, t=50),
font = dict(family='Times new roman', size=20),
annotations = dict(size=60)
)
FIGS[obj.name] = fig
graphs = [dcc.Graph(figure=FIGS[obj_name], id=obj_name) for obj_name in obj_names]
FIGS_HTML = html.Div(graphs, id='figs_html')
app = dash.Dash()
app.layout = html.Div(
[FIGS_HTML] +
[dcc.Interval(
id='plot-update',
interval = 10*1000,
n_intervals = 0)]
)
@app.callback(
[Output("figs_html", "children")],
[Input("plot-update", "n_intervals")])
def gen_bp(interval=None):
x = get_x_data()
for i,obj in enumerate(objects):
xx,yy = obj.spec_read()
adc_x, adc_y = obj.adc_get_samples()
FIGS_HTML.children[i].figure.data[0].y = 10*np.log10(xx + 0.1)
FIGS_HTML.children[i].figure.data[0].x = x
FIGS_HTML.children[i].figure.data[1].y = 10*np.log10(yy + 0.1)
FIGS_HTML.children[i].figure.data[1].x = x
FIGS_HTML.children[i].figure.data[2].x = adc_x
FIGS_HTML.children[i].figure.data[2].name = 'RMS_x: %.2f' %np.std(adc_x)
FIGS_HTML.children[i].figure.data[3].x = adc_y
FIGS_HTML.children[i].figure.data[3].name = 'RMS_y: %.2f' %np.std(adc_y)
return [FIGS_HTML.children]
app.run_server(debug=False)