2

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?

3
  • Shell script would be the best option for this. you may write simple .sh file containing the commands to run your Flask app and that can be further included in CRON JOB. Commented Jun 4, 2015 at 20:08
  • Ok let me try, thank you. Commented Jun 4, 2015 at 20:11
  • What i did is made a file - reload.sh with #!/bin/bash and touch /var/www/wsgi.py lines and schedules the reload.sh file. Was there anything wrong in the steps? Commented Jun 4, 2015 at 20:14

1 Answer 1

5

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)
Sign up to request clarification or add additional context in comments.

1 Comment

yeah thank you, i was thinking the calculation was going on every time someone hits the link. But this is the correct way.

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.