Hi Dash community,
I would like to understand performance limitation of our current dashboard when it comes to displaying time series.
The part of the dashboard I would like to optimize looks like this :
To create these, I call two times make_voltage_subplots
which takes three dataframes (L1_df, L2_df, L3_df) of shape (2788, 5) as input and displays 3 out of 5 columns in each subplot using the following code:
@decorators.display_exec_time
def make_voltage_subplots(L1_df,L2_df,L3_df,mp = '',sorting=False):
#
fig = make_subplots(rows=1, cols=3)
if not L1_df.empty:
print(L1_df.shape)
fig = add_lines(L1_df,'L1',1,fig,sorting)
if not L2_df.empty:
fig = add_lines(L2_df,'L2',2,fig,sorting)
if not L3_df.empty:
fig = add_lines(L3_df,'L3',3,fig,sorting)
fig.update_layout(
title= f'''Spenningsdata per fase -- MP: {mp}''',
yaxis={'title':'Spenning (kWh/h)','range':RANGE_VOLTAGE},
yaxis2={'range':RANGE_VOLTAGE},
yaxis3={'range':RANGE_VOLTAGE},
legend=dict(orientation="h",x=0.4),
template="plotly_white"
)
return fig
The add_lines
just takes one dataframe as input and use append_trace
to add a trace to the current subplot using ScatterGL
. The function function looks like this :
@decorators.display_exec_time
def add_lines(df,str_phase,clr_line,fig,sorting):
#
if sorting:
x = list(range(0,len(df.index),1))
y_min = list(df['min'].sort_values(ascending = False))
y_max = list(df['max'].sort_values(ascending = False))
y_avg = list(df['average'].sort_values(ascending = False))
else:
#
x = list(df.index)
y_min = list(df['min'])
y_max = list(df['max'])
y_avg = list(df['average'])
#min
fig.append_trace(
go.Scattergl(
x = x,
y = y_min,
mode = 'lines',
name = f'{str_phase} spenning -- min',
showlegend = False,
line = dict(color = 'rgb(10, 10, 10)',width = 0.5)
),
1,
clr_line
)
#max
fig.append_trace(
go.Scattergl(
x = x,
y = y_max,
mode='lines',
name=f'{str_phase} spenning -- max',
showlegend=False,
line=dict(color='rgb(10, 10, 10)',width = 0.5)
),
1,
clr_line
)
#avg
fig.append_trace(
go.Scattergl(
x = x,
y = y_avg,
mode='lines',
name=f'{str_phase} spenning -- avg',
line=dict(color=clr.DEFAULT_PLOTLY_COLORS[clr_line],width = 1)
),
1,
clr_line
)
return fig
When running the dashboard, I get the following performance (I call the display two times, see below for the end result in the dashboard) :
Execution time for 'add_lines': 1.254s
Execution time for 'add_lines': 1.299s
Execution time for 'add_lines': 1.355s
Execution time for 'make_voltage_subplots': 4.780s
Execution time for 'add_lines': 1.135s
Execution time for 'add_lines': 1.179s
Execution time for 'add_lines': 1.104s
Execution time for 'make_voltage_subplots': 3.708s
Question: what could I do differently to accelerate the creation of these subplots ?
Almost 5 seconds for the first subplot and 4s for the second (which includes an extra step of sorting the data !?) is unfortunetly not responsive enough for our current use of the dashboard.
Thanks in advance