代码更改后自动重新加载python Flask应用程序 [英] Auto reloading python Flask app upon code changes

查看:416
本文介绍了代码更改后自动重新加载python Flask应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究如何用Python开发一个体面的Web应用程序。由于我不想要一些高阶结构,所以我选择了轻量级的 Flask框架。时间会告诉你这是不是正确的选择。

因此,现在我已经建立了一个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 


$ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆