Update On Load using Dash Plotly

Viewed 2832

so here is a simple web app I deployed recently: https://covid19-visualisation-seb.herokuapp.com/

I used the Dash framework and deployed it using Heroku.

I use df = pd.read_csv('owid-covid-data.csv') to load the data set. The latest data set can be found here:

https://covid.ourworldindata.org/data/owid-covid-data.csv

The data is being updated every day. The problem is that if I only set df variable to the link, it's only going to get the data from the source once when the server starts. So if I wanted to keep the data up to date every day I would have to restart the server every day, which is nonsense.

Dash documentation provides Update on Page Load feature, which looks like this:

import datetime

import dash
import dash_html_components as html

def serve_layout():
    return html.H1('The time is: ' + str(datetime.datetime.now()))

app.layout = serve_layout

if __name__ == '__main__':
    app.run_server(debug=True)

it works when I refresh the page (time updates itself)

but I'd like to reassign a variable on page load, not sure how to do this I get an error. I tried something like this:

import dash
import pandas as pd

app = dash.Dash()

df = ''

def get_data():
    global df
    df = pd.read_csv('https://covid.ourworldindata.org/data/owid-covid-data.csv')


app.layout = get_data

if __name__ == '__main__':
    app.run_server(debug=True)

Any help?

1 Answers

Plotly dash has a feature called dash_core_components.Interval that will define an interval upon which to trigger a callback in your app. Obviously you will have to set up a callback feature in order to utilize this. Check the documentation below, its very thorough. Also you'll see at the bottom of the app, they recommend implementing a time expiring cache to handle memory bottlenecks from long term apps.

https://dash.plotly.com/live-updates

Related