Automatically reload Python-Flask application hosted on PythonAnywhere.com

I need to reload my flask application hosted on pythonAnywhere everyday. Is it possible to automatically reload the application using the code i already have?

The application is a simple days counter:

import datetime
from flask import Flask, render_template

app = Flask(__name__)
wsgi_app = app.wsgi_app

currentDate = datetime.date.today()
userInput = '07/22/2015'
targetdate = datetime.datetime.strptime(userInput, '%m/%d/%Y').date()
calc = targetdate - currentDate
msg=str(calc.days)

@app.route('/', methods=['GET','POST'])
def index():
    return render_template('index.html', message=msg)

I have already gone through: This link to pythonAnywhere forum but is a script that i have to deploy on my pc and not on the application itself.

Question: How to automatically reload the application everyday? Is there any way of doing the same using schedule feature on the site?

Answer

Just want to point out that you can also just change your code slightly and you wouldn’t need to do all this reloading at all.

Just do the calculation within your index function and that will recalculate the number of days each time you visit that page again.

import datetime
from flask import Flask, render_template

app = Flask(__name__)
wsgi_app = app.wsgi_app

userInput = '07/22/2015'
targetdate = datetime.datetime.strptime(userInput, '%m/%d/%Y').date()

@app.route('/', methods=['GET','POST'])
def index():
    currentDate = datetime.date.today()
    calc = targetdate - currentDate
    msg=str(calc.days)
    return render_template('index.html', message=msg)

Leave a Reply

Your email address will not be published. Required fields are marked *