Hi everyone,
I have written a dashboard to explore COVID-19 data, the code can be found here: [GitHub - matteomancini/covid19ita-dash: Dashboard for monitoring COVID-19 spread in Italy using Python and Dash.].(GitHub - matteomancini/covid19ita-dash: Dashboard for monitoring COVID-19 spread in Italy using Python and Dash.). Briefly, I use a choroplethmapbox to show the number of cases in Italy and with some controls one can look at a different date or plot daily changes. When I run it locally or in JupyterLab, everything is fine. However, when deployed on Heroku or on AWS, updating the map takes between 45 and 55 seconds (!), while locally it takes a couple of seconds. The function that takes care of the update is the following:
@app.callback(
[Output('prov-choropleth', 'figure'),
Output('table', 'data')],
[Input('date-slider', 'value'),
Input('selectmeasure', 'value')],
[State('prov-choropleth', 'figure')])
def update_figure(selected_date, measure, figure):
filtered_df = data[data.date_index == selected_date]
z_min = filtered_df[filtered_df.Provincia != temp_label][measure].min()
z_max = filtered_df[filtered_df.Provincia != temp_label][measure].max()
table_data = filtered_df.to_dict('rows')
if figure['data']:
figure['data'][0]['z'] = filtered_df[measure]
figure['data'][0]['zmin'] = z_min
figure['data'][0]['zmax'] = z_max
figure['data'][0]['name'] = measure
else:
trace = go.Choroplethmapbox(z=filtered_df[measure], geojson=counties, locations=filtered_df['codice_provincia'],
featureidkey='properties.prov_istat_code_num', zmax=z_max, zmin=z_min, name=measure,
hovertemplate='%{text}<extra>%{z}</extra>',
text=filtered_df['Provincia'] + "<br>" + filtered_df['Data'])
figure['data'] = [trace]
return [figure, table_data]
Basically, the first time that is called it generates the map using go.Choroplethmapbox, and after that it just changes the z values in the existing figure.
I have tested a few things to try to figure out how to fix the problem:
- there is another function callback in the code, and I tried to completely remove it to see if things would improve (they didn’t);
- I tried to strip the function of most of its operations, and the bottleneck is specifically the map update (as one could expect);
- I tried to deploy the app “locally” on two different raspberry pi devices: the first one, RPi-zero with a single core processor, would take 40 seconds each time to update the map (still less than Heroku and AWS); the second one, RPi3 with a quad-core processor, would take 15 seconds;
- On what observed from the “local” deployment, I tried a different instance on AWS: instead of a minimal t2.micro, I tried first a t3a.nano and then a t3a.xlarge (!!!), but same result (45-50 seconds).
At this point, I’m not sure what should I try to make the update times reasonable. I’ve looked at dashboards with maps but I could not find anyone actually updating a Choroplethmap. Is there anyone else experienced something similar? I would be very happy to try other ways to implement the map update if anyone has any ideas.
Thanks in advance.