【问题标题】:Automatically reload Python-Flask application hosted on PythonAnywhere.com自动重新加载 PythonAnywhere.com 上托管的 Python-Flask 应用程序
【发布时间】:2015-08-19 14:34:41
【问题描述】:

我需要每天重新加载托管在pythonAnywhere 上的烧瓶应用程序。是否可以使用我已有的代码自动重新加载应用程序?

该应用程序是一个简单的天数计数器:

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)

我已经经历过: This link 到 pythonAnywhere 论坛,但它是一个脚本,我必须部署在我的电脑上而不是应用程序本身上。

问题:如何每天自动重新加载应用程序?有没有办法使用网站上的日程安排功能做同样的事情?

【问题讨论】:

  • Shell 脚本将是最好的选择。您可以编写简单的 .sh 文件,其中包含运行 Flask 应用程序的命令,并且可以进一步包含在 CRON JOB 中。
  • 好的,我试试,谢谢。
  • 我所做的是创建一个文件 - reload.sh 和 #!/bin/bash 并触摸 /var/www/wsgi.py 行并安排 reload.sh 文件。步骤有什么问题吗?

标签: python flask reload pythonanywhere


【解决方案1】:

只想指出,您也可以稍微更改代码,根本不需要重新加载所有这些。

只需在您的index 函数中进行计算,然后您每次再次访问该页面时都会重新计算天数。

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)

【讨论】:

  • 是的,谢谢,我在想每次有人点击链接时都会进行计算。但这是正确的方法。
猜你喜欢
  • 2013-04-27
  • 1970-01-01
  • 2020-08-22
  • 2015-05-09
  • 2011-06-21
  • 2015-03-31
  • 2014-06-17
  • 2021-05-20
相关资源
最近更新 更多