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 werkzeug debugger already has an ‘auto reload’ function available that can be enabled by doing one of the following:
app.run(debug=True)
or
app.debug = True
You can also use a separate configuration file to manage all your setup if you need be. For example I use ‘settings.py’ with a ‘DEBUG = True’ option. Importing this file is easy too;
app.config.from_object('application.settings')
However this is not suitable for a production environment.
Production environment
Personally I chose Nginx + uWSGI over Apache + mod_wsgi for a few performance reasons but also the configuration options. The touch-reload option allows you to specify a file/folder that will cause the uWSGI application to reload your newly deployed flask app.
For example, your update script pulls your newest changes down and touches ‘reload_me.txt’ file. Your uWSGI ini script (which is kept up by Supervisord – obviously) has this line in it somewhere:
I went this route to mimic production as close as possible with my nginx setup. Simply running the flask app with it’s built in web server behind nginx it would result in a bad gateway error.
回答 4
Flask 1.0及更高版本的一些更新
热重装的基本方法是:
$ export FLASK_APP=my_application
$ export FLASK_ENV=development
$ flask run
Install the python-dotenv module, which will read local preference for your project environment.
Second:
Add .flaskenv file in your project directory. Add following code:
FLASK_ENV=development
It’s done!
With this config for your Flask project, when you run flask run and you will see this output in your terminal:
And when you edit your file, just save the change. You will see auto-reload is there for you:
With more explanation:
Of course you can manually hit export FLASK_ENV=development every time you need. But using different configuration file to handle the actual working environment seems like a better solution, so I strongly recommend this method I use.
(venv) $ export FLASK_APP=hello.py forWindows use > set FLASK_APP=hello.py
(venv) $ export FLASK_DEBUG=1forWindows use > set FLASK_DEBUG=1(venv) $ flask run
*ServingFlask app "hello"*Forcing debug mode on
*Running on http://127.0.0.1:5000/(Press CTRL+C to quit)*Restartingwith stat
*Debuggeris active!*Debugger PIN:273-181-528
Flask applications can optionally be executed in debug mode. In this mode, two very convenient modules of the development server called the reloader and the debugger are enabled by default.
When the reloader is enabled, Flask watches all the source code files of your project and automatically restarts the server when any of the files are modified.
By default, debug mode is disabled. To enable it, set a FLASK_DEBUG=1 environment variable before invoking flask run:
(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
Having a server running with the reloader enabled is extremely useful during development, because every time you modify and save a source file, the server automatically restarts and picks up the change.