Forums

Auto reload the webpage with new data everyday

Hello, I am on the free version of Python running a Flask App using Dash to plot data. I retrieve and save to a csv new data every day, but that new data isn't always being displayed updated on the app. If I hit the reload button on the web dashboard, then the new data appears. I've seen that the server log doesn't reload every day.

Do you have any recommendations? Some ideas:

  • Probably enhancing the dash code?
  • or enhancing the scheduled python script used to update the csv?
  • or forcing the server reloading the site?

Thank you in advance! Best, Lucas

PS: the website is lucasgday.pythonanywhere.com

Any of your suggestions will work fine. Choose the one that best fits how you'd like your site to work. I would think the simplest would be for the scheduled script to reload the web app after it's updated the csv.

Great Glenn, thanks for your prompt reply! How do I reload the web app from the script?

Thanks Lucas

If it's from inside a Python script, the best way is to use our API. The full API docs are here, but the summary for reloading a website is to go to the API tab of the "Account" page, create a new API token if necessary, and then use code like this:

import requests
username = "your username"
api_token = "your api token"
domain_name = "your domain name, eg. lucasgday.pythonanywhere.com"

response = requests.post(
    'https://www.pythonanywhere.com/api/v0/user/{username}/webapps/{domain_name}/reload/'.format(
        username=username, domain_name=domain_name
    ),
    headers={'Authorization': 'Token {token}'.format(token=api_token)}
)
if response.status_code == 200:
    print('reloaded OK')
else:
    print('Got unexpected status code {}: {!r}'.format(response.status_code, response.content))

...changing the values of the variables in lines 2-4, of course.

[edit: fixed get vs post in code]

EXCELLENT!!! Thank you very much giles!!! It worked perfectly.

Best! Lucas

Great! Thanks for confirming :-)

Hi Lucas, i am also struggling with the same thing. how did you use above code in your app?

What errors are you getting when you use it? You can see website errors in the error log, which is linked from the "Web" page. The most recent error will be at the bottom of the file.

.

import requests
from time import sleep
username = ''
api_token = ''
domain_name = "sardorbekjon.pythonanywhere.com"
while True:
    response = requests.post(
    'https://www.pythonanywhere.com/api/v0/user/{username}/webapps/{domain_name}/reload/'.format(
    username=username, domain_name=domain_name
    ),
    headers={'Authorization': 'Token {token}'.format(token=api_token)}
)
    if response.status_code == 200:
        print('reloaded OK')
    else:
        print('Got unexpected status code {}: {!r}'.format(response.status_code, response.content))
    sleep(60)

Can I use this code in pythonanywhere?

Sure, if you want to.

Should I be using the api code in the pythonanywhere wsgi file or my python script with the dash app? If in the latter, where exactly do I use the code in relation to my dash app?

I guess the minimal requirement is to have this section

from myapp import app
application = app.server

in the wsgi file, the rest depends on how you structurize your code, if I understand your question correctly. See also this help page.

You do not want it in either of those places. If you restart your web app from within your web app, it would just keep restarting itself. Use a daily scheduled task.

interesting spin on this solution, would it not work just to write a beautifulsoup script, load it in as a task, have it login to pythonanywhere, navigate to the web tab and hit the reload button once a day?

API is much easier to use.