我正在研究如何用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应用程序。

app.run(debug=True)

或者,从壳层来看

$ export FLASK_DEBUG=1
$ flask run

http://flask.palletsprojects.com/quickstart/#debug-mode

在测试/开发环境中

werkzeug调试器已经有一个“自动重载”功能,可以通过执行以下操作之一来启用:

app.run(debug=True)

or

app.debug = True

如果需要,还可以使用单独的配置文件来管理所有设置。例如,我使用'settings.py'带有'DEBUG = True'选项。导入这个文件也很容易;

app.config.from_object('application.settings')

但是,这不适用于生产环境。

生产环境

就我个人而言,我选择Nginx + uWSGI而不是Apache + mod_wsgi是因为一些性能原因,也是因为配置选项。touch-reload选项允许您指定一个文件/文件夹,该文件/文件夹将导致uWSGI应用程序重新加载新部署的flask应用程序。

例如,您的更新脚本下拉最新的更改并触摸'reload_me.txt'文件。你的uWSGI ini脚本(由supervisor ord维护-显然)有这一行在它的某个地方:

touch-reload = '/opt/virtual_environments/application/reload_me.txt'

我希望这能有所帮助!

如果你正在使用uwsgi运行,看看python的auto reload选项:

uwsgi --py-autoreload 1

示例uwsgi-dev-example.ini:

[uwsgi]
socket = 127.0.0.1:5000
master = true
virtualenv = /Users/xxxx/.virtualenvs/sites_env
chdir = /Users/xxx/site_root
module = site_module:register_debug_server()
callable = app
uid = myuser
chmod-socket = 660
log-date = true
workers = 1
py-autoreload = 1

site_root / __init__ . py

def register_debug_server():
    from werkzeug.debug import DebuggedApplication

    app = Flask(__name__)
    app.debug = True
    app = DebuggedApplication(app, evalex=True)
    return app

然后运行:

uwsgi --ini uwsgi-dev-example.ini

注意:此示例还启用调试器。

我用nginx设置尽可能地模拟生产过程。简单地运行flask应用程序,在nginx后面的web服务器中,它会导致一个糟糕的网关错误。

在开启调试模式的情况下运行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。

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

PYTHONUNBUFFERED=1;
FLASK_DEBUG=1

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

对于Flask 1.0到2.2,热重新加载的基本方法是:

$ export FLASK_APP=my_application
$ export FLASK_ENV=development
$ flask run

你应该使用FLASK_ENV=development(而不是FLASK_DEBUG=1) 作为安全检查,你可以运行flask run——debugger来确保它是打开的 Flask CLI现在会自动读取FLASK_APP和FLASK_ENV之类的东西,如果你在项目根目录中有一个.env文件,并且安装了python-dotenv

我有不同的想法:

第一:

pip install python-dotenv

安装python-dotenv模块,该模块将读取项目环境的本地首选项。

第二:

在项目目录中添加.flaskenv文件。添加以下代码:

FLASK_ENV=development

这是完成了!

使用此配置为您的Flask项目,当您运行Flask运行时,您将在终端中看到以下输出:

编辑文件时,保存更改即可。你会看到自动重载为你:

更多解释:

当然,您可以在需要的时候手动点击export FLASK_ENV=development。但是使用不同的配置文件来处理实际的工作环境似乎是一个更好的解决方案,所以我强烈推荐我使用的这种方法。

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

在开发过程中,使服务器运行时启用重新加载程序是非常有用的,因为每次修改和保存源文件时,服务器都会自动重新启动并接受更改。

在终端上你可以简单地说

export FLASK_APP=app_name.py
export FLASK_ENV=development
flask run

或者在你的文件中

if __name__ == "__main__":
    app.run(debug=True)
app.run(use_reloader=True)

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

使用这个方法:

app.run(debug=True)

当发生代码更改时,它将自动重新加载flask应用程序。

示例代码:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def index():
   return "Hello World"


if __name__ == '__main__':
  app.run(debug=True)

好吧,如果你想节省时间,而不是每次发生更改时都重新加载网页,那么你可以尝试键盘快捷键Ctrl + R来快速重新加载页面。

帮助快速自动更改浏览器:

PIP安装负载

from livereload import Server

if __name__ == '__main__':
    server = Server(app.wsgi_app)
    server.serve()

接下来,再次启动服务器:

如。你的.py文件是app.py

python app.py

启用flask 2.2中的reloader:

flask run --reload

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
)