forked from orbit-oss/flask
Add Support for FLASK_ENV (#2570)
This introduces environments to Flask
This commit is contained in:
parent
60eecb547d
commit
2433522d29
10 changed files with 151 additions and 71 deletions
15
docs/cli.rst
15
docs/cli.rst
|
|
@ -116,6 +116,16 @@ context will be active, and the app instance will be imported. ::
|
|||
|
||||
Use :meth:`~Flask.shell_context_processor` to add other automatic imports.
|
||||
|
||||
Environments
|
||||
------------
|
||||
|
||||
.. versionadded:: 1.0
|
||||
|
||||
The environment in which the Flask app should run is set by the
|
||||
:envvar:`FLASK_ENV` environment variable. If not set it defaults to
|
||||
``production``. The other default environment which is known is
|
||||
``development``. If the env is set to ``development`` the debug mode is
|
||||
for instance automatically enabled.
|
||||
|
||||
Debug Mode
|
||||
----------
|
||||
|
|
@ -123,11 +133,14 @@ Debug Mode
|
|||
Set the :envvar:`FLASK_DEBUG` environment variable to override the
|
||||
application's :attr:`~Flask.debug` flag. The value ``1`` enables it, ``0``
|
||||
disables it. Forcing the debug flag on also enables the debugger and reloader
|
||||
when running the development server. ::
|
||||
when running the development server.
|
||||
|
||||
::
|
||||
|
||||
$ FLASK_DEBUG=1 flask run
|
||||
* Serving Flask app "hello"
|
||||
* Forcing debug mode on
|
||||
* Env production
|
||||
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
|
||||
* Restarting with inotify reloader
|
||||
* Debugger is active!
|
||||
|
|
|
|||
|
|
@ -27,35 +27,52 @@ The :attr:`~flask.Flask.config` is actually a subclass of a dictionary and
|
|||
can be modified just like any dictionary::
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['DEBUG'] = True
|
||||
app.config['TESTING'] = True
|
||||
|
||||
Certain configuration values are also forwarded to the
|
||||
:attr:`~flask.Flask` object so you can read and write them from there::
|
||||
|
||||
app.debug = True
|
||||
app.testing = True
|
||||
|
||||
To update multiple keys at once you can use the :meth:`dict.update`
|
||||
method::
|
||||
|
||||
app.config.update(
|
||||
DEBUG=True,
|
||||
TESTING=True,
|
||||
SECRET_KEY=b'_5#y2L"F4Q8z\n\xec]/'
|
||||
)
|
||||
|
||||
.. admonition:: Debug Mode with the ``flask`` Script
|
||||
Environment and Debug Features
|
||||
------------------------------
|
||||
|
||||
If you use the :command:`flask` script to start a local development
|
||||
server, to enable the debug mode, you need to export the ``FLASK_DEBUG``
|
||||
environment variable before running the server::
|
||||
Some values are special in that they can show unexpected behavior when
|
||||
changed late. In particular that applies to the Flask environment and
|
||||
debug mode.
|
||||
|
||||
$ export FLASK_DEBUG=1
|
||||
$ flask run
|
||||
If you use the :command:`flask` script to start a local development server
|
||||
for instance you should tell Flask that you want to work in the
|
||||
development environment. For safety reasons we default the flask
|
||||
environment to production mode instead of development. This is done
|
||||
because development mode can turn on potentially unsafe features such as
|
||||
the debugger by default.
|
||||
|
||||
(On Windows you need to use ``set`` instead of ``export``).
|
||||
To control the environment and such fundamental features Flask provides
|
||||
the two environment variables :envvar:`FLASK_ENV` and :envvar:`FLASK_DEBUG`.
|
||||
In versions of Flask older than 1.0 the :envvar:`FLASK_ENV` environment
|
||||
variable did not exist.
|
||||
|
||||
``app.debug`` and ``app.config['DEBUG']`` are not compatible with
|
||||
the :command:`flask` script. They only worked when using ``Flask.run()``
|
||||
method.
|
||||
The most common way to switch Flask to development mode is to tell it to
|
||||
work on the ``development`` environment::
|
||||
|
||||
$ export FLASK_ENV=development
|
||||
$ flask run
|
||||
|
||||
(On Windows you need to use ``set`` instead of ``export``).
|
||||
|
||||
While you can attempt to flip the environment and debug flag separately in
|
||||
the Flask config from the config file this is strongly discouraged as
|
||||
those flags are often loaded early and changing them late might not apply
|
||||
to all systems and extensions.
|
||||
|
||||
Builtin Configuration Values
|
||||
----------------------------
|
||||
|
|
|
|||
|
|
@ -65,10 +65,10 @@ that tells Flask where to find the application instance::
|
|||
export FLASK_APP=yourapplication
|
||||
|
||||
If you are outside of the project directory make sure to provide the exact
|
||||
path to your application directory. Similarly you can turn on "debug
|
||||
mode" with this environment variable::
|
||||
path to your application directory. Similarly you can turn on the
|
||||
development features like this::
|
||||
|
||||
export FLASK_DEBUG=true
|
||||
export FLASK_ENV=development
|
||||
|
||||
In order to install and run the application you need to issue the following
|
||||
commands::
|
||||
|
|
|
|||
|
|
@ -130,13 +130,16 @@ That is not very nice and Flask can do better. If you enable debug
|
|||
support the server will reload itself on code changes, and it will also
|
||||
provide you with a helpful debugger if things go wrong.
|
||||
|
||||
To enable debug mode you can export the ``FLASK_DEBUG`` environment variable
|
||||
To enable all development features (and to disable the debug mode) you can
|
||||
export the ``FLASK_ENV`` environment variable and set it to
|
||||
``development``
|
||||
before running the server::
|
||||
|
||||
$ export FLASK_DEBUG=1
|
||||
$ export FLASK_ENV=development
|
||||
$ flask run
|
||||
|
||||
(On Windows you need to use ``set`` instead of ``export``).
|
||||
(On Windows you need to use ``set`` instead of ``export`` and on Flask
|
||||
versions older than 1.0 you need to export ``FLASK_DEBUG=1`` instead).
|
||||
|
||||
This does the following things:
|
||||
|
||||
|
|
|
|||
|
|
@ -12,12 +12,13 @@ but you can also continue using the :meth:`Flask.run` method.
|
|||
Command Line
|
||||
------------
|
||||
|
||||
The :command:`flask` command line script (:ref:`cli`) is strongly recommended for
|
||||
development because it provides a superior reload experience due to how it
|
||||
loads the application. The basic usage is like this::
|
||||
The :command:`flask` command line script (:ref:`cli`) is strongly
|
||||
recommended for development because it provides a superior reload
|
||||
experience due to how it loads the application. The basic usage is like
|
||||
this::
|
||||
|
||||
$ export FLASK_APP=my_application
|
||||
$ export FLASK_DEBUG=1
|
||||
$ export FLASK_ENV=development
|
||||
$ flask run
|
||||
|
||||
This will enable the debugger, the reloader and then start the server on
|
||||
|
|
@ -29,6 +30,13 @@ disabled::
|
|||
|
||||
$ flask run --no-reload
|
||||
|
||||
.. note::
|
||||
|
||||
On older Flask version (before 1.0) the :envvar:`FLASK_ENV`
|
||||
environment variable is not supported and you need to enable the
|
||||
debug mode separately by setting the :envvar:`FLASK_DEBUG` environment
|
||||
variable to ``1``.
|
||||
|
||||
In Code
|
||||
-------
|
||||
|
||||
|
|
|
|||
|
|
@ -78,11 +78,13 @@ With that out of the way, you should be able to start up the application.
|
|||
Do this on Mac or Linux with the following commands in ``flaskr/``::
|
||||
|
||||
export FLASK_APP=flaskr
|
||||
export FLASK_DEBUG=true
|
||||
export FLASK_ENV=development
|
||||
flask run
|
||||
|
||||
(In case you are on Windows you need to use ``set`` instead of ``export``).
|
||||
The :envvar:`FLASK_DEBUG` flag enables or disables the interactive debugger.
|
||||
The :envvar:`FLASK_ENV` flag if set to ``development`` turns on all
|
||||
development features such as enabling the interactive debugger.
|
||||
|
||||
*Never leave debug mode activated in a production system*, because it will
|
||||
allow users to execute code on the server!
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue