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?
app.layout
must be assigned to a Dash UI component, and in the case of the second snippetget_data
does not return the latter. After reading the CSV data todf
, I'd expect you would want to pass it down into a Dash UI component, which you should instead return. – miqh