2
votes

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
app.layout must be assigned to a Dash UI component, and in the case of the second snippet get_data does not return the latter. After reading the CSV data to df, I'd expect you would want to pass it down into a Dash UI component, which you should instead return.miqh
I've done some more research, and I found out that the file is 16mb and it's too big to be downloaded every time user interacts with dash components. So Ideally I'd like my app to download the file every 24 hours. I tried to do download it with requests and then run repeated function using schedule, but it code ends up running in an endless loop leaving the res unreachable. How can I run a function every 24 hours without interfering with the server?Sebastian Meckovski

1 Answers

1
votes

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