我正在研究如何用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?
Flask应用程序可以选择在调试模式下执行。在这种模式下,默认情况下启用了开发服务器的两个非常方便的模块,即重新加载器和调试器。
当重新加载器被启用时,Flask会监视项目的所有源代码文件,并在任何文件被修改时自动重新启动服务器。
缺省情况下,调试模式是关闭的。要启用它,在调用flask run之前设置一个FLASK_DEBUG=1的环境变量:
(venv) $ export FLASK_APP=hello.py for Windows use > set FLASK_APP=hello.py
(venv) $ export FLASK_DEBUG=1 for Windows use > set FLASK_DEBUG=1
(venv) $ flask run
* Serving Flask app "hello"
* Forcing debug mode on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 273-181-528
在开发过程中,使服务器运行时启用重新加载程序是非常有用的,因为每次修改和保存源文件时,服务器都会自动重新启动并接受更改。
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应用程序可以选择在调试模式下执行。在这种模式下,默认情况下启用了开发服务器的两个非常方便的模块,即重新加载器和调试器。
当重新加载器被启用时,Flask会监视项目的所有源代码文件,并在任何文件被修改时自动重新启动服务器。
缺省情况下,调试模式是关闭的。要启用它,在调用flask run之前设置一个FLASK_DEBUG=1的环境变量:
(venv) $ export FLASK_APP=hello.py for Windows use > set FLASK_APP=hello.py
(venv) $ export FLASK_DEBUG=1 for Windows use > set FLASK_DEBUG=1
(venv) $ flask run
* Serving Flask app "hello"
* Forcing debug mode on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 273-181-528
在开发过程中,使服务器运行时启用重新加载程序是非常有用的,因为每次修改和保存源文件时,服务器都会自动重新启动并接受更改。