Forums

Data For App Only Updates When I Reload It

Hello,

I have a dash application that can be seen here: https://jonathanbechtel.pythonanywhere.com/

At the beginning of the page I have a line where I pull in info from a database that reads like the following:

script_directory = os.path.dirname(os.path.abspath(__file__))
data_file        = os.path.join(script_directory, "info.pkl")
with open(data_file, 'rb') as info:
    connection_info = pickle.load(info)

connection_string = create_connection_string(connection_info)

engine            = create_engine(connection_string)

# and get our database info
with engine.connect() as connection:
    db_df = pd.read_sql_query('SELECT * FROM jonathanbechtel$covid.predictions ORDER BY dt DESC LIMIT 57', 
con=connection)

The issue I'm having is that the data from the query only seems to render new values after I refresh the application in the web console. The database is updated daily so checking the app after 9PM should display new values, but it doesn't automatically, only after the manual refresh.

Any idea why this would be the case? Thank you.

It's the case because the code can't guess what you want to achieve unless you explicitly say so -- if I understand correctly the code you've written says the app to load the data only once when the file is being evaluated by WSGI (thus the only way to reload the data is to reload the app itself). To achieve live reloading you need to create relevant bits of code which will execute this. I'm not an experienced Dash user but maybe this portion of docs could be helpful: https://dash.plotly.com/live-updates?

Okay, that makes sense now. Thank you!

Just curious -- is it possible to schedule the site to refresh at a regular interval? There's only one source of data that I need to call from, and since it does not change very often, doing the site refresh is probably better than having each user call the database individually.

You can create a task that refreshes the database on PythonAnywhere and you can make your web app talk to that database.