2019-11-19 05:46:00 +08:00
|
|
|
.. currentmodule:: flask
|
2014-04-28 19:27:13 +08:00
|
|
|
|
|
|
|
Development Server
|
|
|
|
==================
|
|
|
|
|
2019-11-19 05:46:00 +08:00
|
|
|
Flask provides a ``run`` command to run the application with a
|
|
|
|
development server. In development mode, this server provides an
|
|
|
|
interactive debugger and will reload when code is changed.
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
|
|
|
Do not use the development server when deploying to production. It
|
|
|
|
is intended for use only during local development. It is not
|
|
|
|
designed to be particularly efficient, stable, or secure.
|
2014-04-28 19:27:13 +08:00
|
|
|
|
2019-11-19 05:46:00 +08:00
|
|
|
See :doc:`/deploying/index` for deployment options.
|
2014-04-28 19:27:13 +08:00
|
|
|
|
|
|
|
Command Line
|
|
|
|
------------
|
|
|
|
|
2019-11-19 05:46:00 +08:00
|
|
|
The ``flask run`` command line script is the recommended way to run the
|
|
|
|
development server. It requires setting the ``FLASK_APP`` environment
|
|
|
|
variable to point to your application, and ``FLASK_ENV=development`` to
|
|
|
|
fully enable development mode.
|
2014-04-28 19:27:13 +08:00
|
|
|
|
2019-11-19 05:46:00 +08:00
|
|
|
.. code-block:: text
|
|
|
|
|
|
|
|
$ export FLASK_APP=hello
|
2018-01-07 00:07:56 +08:00
|
|
|
$ export FLASK_ENV=development
|
2016-05-27 02:48:49 +08:00
|
|
|
$ flask run
|
2014-04-28 19:27:13 +08:00
|
|
|
|
2018-01-11 07:38:52 +08:00
|
|
|
This enables the development environment, including the interactive
|
|
|
|
debugger and reloader, and then starts the server on
|
2019-11-19 05:46:00 +08:00
|
|
|
http://localhost:5000/. Use ``flask run --help`` to see the available
|
|
|
|
options, and :doc:`/cli` for detailed instructions about configuring
|
|
|
|
and using the CLI.
|
2014-04-28 19:27:13 +08:00
|
|
|
|
2018-01-07 00:07:56 +08:00
|
|
|
.. note::
|
|
|
|
|
2019-11-19 05:46:00 +08:00
|
|
|
Prior to Flask 1.0 the ``FLASK_ENV`` environment variable was not
|
|
|
|
supported and you needed to enable debug mode by exporting
|
2018-01-11 07:38:52 +08:00
|
|
|
``FLASK_DEBUG=1``. This can still be used to control debug mode, but
|
|
|
|
you should prefer setting the development environment as shown
|
|
|
|
above.
|
2018-01-07 00:07:56 +08:00
|
|
|
|
2019-11-19 05:46:00 +08:00
|
|
|
|
|
|
|
Lazy or Eager Loading
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
When using the ``flask run`` command with the reloader, the server will
|
|
|
|
continue to run even if you introduce syntax errors or other
|
|
|
|
initialization errors into the code. Accessing the site will show the
|
|
|
|
interactive debugger for the error, rather than crashing the server.
|
|
|
|
This feature is called "lazy loading".
|
|
|
|
|
|
|
|
If a syntax error is already present when calling ``flask run``, it will
|
|
|
|
fail immediately and show the traceback rather than waiting until the
|
|
|
|
site is accessed. This is intended to make errors more visible initially
|
|
|
|
while still allowing the server to handle errors on reload.
|
|
|
|
|
|
|
|
To override this behavior and always fail immediately, even on reload,
|
|
|
|
pass the ``--eager-loading`` option. To always keep the server running,
|
|
|
|
even on the initial call, pass ``--lazy-loading``.
|
|
|
|
|
|
|
|
|
2014-04-28 19:27:13 +08:00
|
|
|
In Code
|
|
|
|
-------
|
|
|
|
|
2019-11-19 05:46:00 +08:00
|
|
|
As an alternative to the ``flask run`` command, the development server
|
|
|
|
can also be started from Python with the :meth:`Flask.run` method. This
|
|
|
|
method takes arguments similar to the CLI options to control the server.
|
|
|
|
The main difference from the CLI command is that the server will crash
|
|
|
|
if there are errors when reloading.
|
|
|
|
|
|
|
|
``debug=True`` can be passed to enable the debugger and reloader, but
|
|
|
|
the ``FLASK_ENV=development`` environment variable is still required to
|
|
|
|
fully enable development mode.
|
|
|
|
|
|
|
|
Place the call in a main block, otherwise it will interfere when trying
|
|
|
|
to import and run the application with a production server later.
|
2014-04-28 19:27:13 +08:00
|
|
|
|
2019-11-19 05:46:00 +08:00
|
|
|
.. code-block:: python
|
2014-04-28 19:27:13 +08:00
|
|
|
|
2019-11-19 05:46:00 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run(debug=True)
|
2014-04-28 19:27:13 +08:00
|
|
|
|
2019-11-19 05:46:00 +08:00
|
|
|
.. code-block:: text
|
2014-04-28 19:27:13 +08:00
|
|
|
|
2019-11-19 05:46:00 +08:00
|
|
|
$ python hello.py
|