6

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?

2
  • 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. Commented Feb 1, 2021 at 2:42
  • 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? Commented Feb 1, 2021 at 14:12

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

hi @SebastianMeckovski I am also running into the same issue. How did you manage to fix this? I couldnt make sense much from the live-updates doc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.