代码更改时自动重新加载 python Flask 应用程序 [英] Auto reloading python Flask app upon code changes
问题描述
我正在研究如何使用 Python 开发一个不错的网络应用程序.由于我不希望一些高阶结构妨碍我,我的选择落在了轻量级 烧瓶框架.时间会证明这是否是正确的选择.
I'm investigating how to develop a decent web app with Python. Since I don't want some high-order structures to get in my way, my choice fell on the lightweight Flask framework. Time will tell if this was the right choice.
所以,现在我已经使用 mod_wsgi 设置了一个 Apache 服务器,并且我的测试站点运行良好.但是,我想通过使站点在我所做的 py 或模板文件中的任何更改时自动重新加载来加快开发程序.我看到站点的 .wsgi 文件中的任何更改都会导致重新加载(即使在 apache 配置文件中没有 WSGIScriptReloading On ),但我仍然必须手动刺激它(即,插入额外的换行符,保存).当我编辑一些应用程序的 py 文件时,有什么方法可以导致重新加载?或者,我应该使用 IDE 为我刷新 .wsgi 文件?
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?
推荐答案
目前推荐的方式是使用 flask
命令行工具.
The current recommended way is with the flask
command line utility.
https://flask.palletsprojects.com/en/1.1.x/quickstart/#debug-mode
示例:
$ export FLASK_APP=main.py
$ export FLASK_ENV=development
$ flask run
或在一个命令中:
$ FLASK_APP=main.py FLASK_ENV=development flask run
如果您想要与默认端口不同的端口 (5000
),请添加 --port
选项.
If you want different port than the default (5000
) add --port
option.
示例:
$ FLASK_APP=main.py FLASK_ENV=development flask run --port 8080
更多选项可用:
$ flask run --help
FLASK_APP
也可以设置为 module:app
或 module:create_app
而不是 module.py
.请参阅 https://flask.palletsprojects.com/en/1.1.x/cli/#application-discovery 以获得完整的解释.
FLASK_APP
can also be set to module:app
or module:create_app
instead of module.py
. See https://flask.palletsprojects.com/en/1.1.x/cli/#application-discovery for a full explanation.
这篇关于代码更改时自动重新加载 python Flask 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!