我正在研究如何用Python开发一个不错的web应用程序。因为我不想让一些高阶结构妨碍我,所以我选择了轻量级的Flask框架。时间会证明这是否是正确的选择。

So, now I've set up an Apache server with mod_wsgi, and my test site is running fine. However, I'd like to speed up the development routine by making the site automatically reload upon any changes in py or template files I make. I see that any changes in site's .wsgi file causes reloading (even without WSGIScriptReloading On in the apache config file), but I still have to prod it manually (ie, insert extra linebreak, save). Is there some way how to cause reload when I edit some of the app's py files? Or, I am expected to use IDE that refreshes the .wsgi file for me?


当前回答

app.run(use_reloader=True)

我们可以使用这个use_reloader,这样每次重新加载页面时,我们的代码更改都会被更新。

其他回答

在开启调试模式的情况下运行flask Run CLI命令,将自动启用reloader。从Flask 2.2开始,你可以在命令行上传递——app和——debug选项。

$ flask --app main.py --debug run

——app也可以设置为module:app或module:create_app,而不是module.py。详见医生的详细说明。

有更多的选择:

$ flask run --help

在Flask 2.2之前,您需要设置FLASK_APP和FLASK_ENV=开发环境变量。

$ export FLASK_APP=main.py
$ export FLASK_ENV=development
$ flask run

在Flask 2.2中仍然可以设置FLASK_APP和FLASK_DEBUG=1。

app.run(use_reloader=True)

我们可以使用这个use_reloader,这样每次重新加载页面时,我们的代码更改都会被更新。

I believe a better solution is to set the app configuration. For me, I built the tool and then pushed it to a development server where I had to set up a WSGI pipeline to manage the flask web app. I had some data being updated to a template and I wanted it to refresh every X minutes (WSGI deployment for the Flask site through APACHE2 on UBUNTU 18). In your app.py or whatever your main app is, add app.config.update dictionary below and mark TEMPLATES_AUTO_RELOAD=True, you will find that any templates that are automatically updated on the server will be reflected in the browser. There is some great documentation on the Flask site for configuration handling found here.

app = Flask(__name__)
app.config.update(
    TEMPLATES_AUTO_RELOAD=True
)

启用flask 2.2中的reloader:

flask run --reload

要实现这一点,在PyCharm中设置“环境变量”部分为:

PYTHONUNBUFFERED=1;
FLASK_DEBUG=1

对于Flask '运行/调试配置'。