代码更改后自动重新加载python Flask应用程序 [英] Auto reloading python Flask app upon code changes
问题描述
因此,现在我已经建立了一个mod_wsgi的Apache服务器,我的测试网站运行良好。但是,我想通过使网站自动重新加载py或模板文件中的任何更改来加速开发例程。我发现网站的.wsgi文件中的任何更改都会导致重新加载(即使在Apache配置文件中没有WSGIScriptReloading On),但是我仍然需要手动生成它(即插入额外的换行符,保存)。有什么办法,当我编辑一些应用程序的py文件如何导致重新加载?或者,我希望使用IDE来为我刷新.wsgi文件?
当前推荐的方式(Flask> = 0.11)与 flask
命令行工具。
http://flask.pocoo.org/docs/0.11/server/
示例:
$ export FLASK_APP = main.py
$ export FLASK_DEBUG = 1
$ python -
或在一个命令中:
$ FLASK_APP = main.py FLASK_DEBUG = 1 python -m flask run
我更喜欢 python -m flask run
而不是 flask run
,因为前者也可以使用<$ c如果你想要使用不同的端口( 5000
), )添加 - 端口
选项。
$ python -m flask run --port 8080
$ c
$ b 更多选项可用:
$ python -m flask run --help
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.
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?
解决方案 The current recommended way (Flask >= 0.11) is with the flask
command line utility.
http://flask.pocoo.org/docs/0.11/server/
Example:
$ export FLASK_APP=main.py
$ export FLASK_DEBUG=1
$ python -m flask run
or in one command:
$ FLASK_APP=main.py FLASK_DEBUG=1 python -m flask run
I prefer python -m flask run
rather than flask run
because the former also works with virtualenv
.
If you want different port than the default (5000
) add --port
option.
Example:
$ python -m flask run --port 8080
More options are available with:
$ python -m flask run --help
这篇关于代码更改后自动重新加载python Flask应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!