Skip to content
Myr

rewrite the development server docs

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by David Lord · parent 240a110 · Browse files · 3 files+64 added−37 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+64 added−37 removed

  • other:+64 added−37 removed

3 files+64 added−37 removed

modified:docs/quickstart.rst+3 added-3 removed
106 unmodified lines
107108Versions of Flask older than 0.11 use to have different ways to start the109application. In short, the :command:`flask` command did not exist, and110neither did :command:`python -m flask`. In that case you have two options:111either upgrade to newer Flask versions or have a look at the :ref:`server`112docs to see the alternative method for running a server.111either upgrade to newer Flask versions or have a look at :doc:`/server`112to see the alternative method for running a server.113114Invalid Import Name115```````````````````116
35 unmodified lines
152153You can also control debug mode separately from the environment by154exporting ``FLASK_DEBUG=1``.155156There are more parameters that are explained in the :ref:`server` docs.156There are more parameters that are explained in :doc:`/server`.157158.. admonition:: Attention159160 Even though the interactive debugger does not work in forking environments
740 unmodified lines
modified:docs/server.rst+60 added-33 removed
1.. _server:1.. currentmodule:: flask23Development Server4==================56.. currentmodule:: flask6Flask provides a ``run`` command to run the application with a7development server. In development mode, this server provides an8interactive debugger and will reload when code is changed.98Starting with Flask 0.11 there are multiple built-in ways to run a9development server. The best one is the :command:`flask` command line utility10but you can also continue using the :meth:`Flask.run` method.10.. warning::1112 Do not use the development server when deploying to production. It13 is intended for use only during local development. It is not14 designed to be particularly efficient, stable, or secure.1516 See :doc:`/deploying/index` for deployment options.1718Command Line19------------2015The :command:`flask` command line script (:ref:`cli`) is strongly16recommended for development because it provides a superior reload17experience due to how it loads the application. The basic usage is like18this::21The ``flask run`` command line script is the recommended way to run the22development server. It requires setting the ``FLASK_APP`` environment23variable to point to your application, and ``FLASK_ENV=development`` to24fully enable development mode.2526.. code-block:: text2720 $ export FLASK_APP=my_application28 $ export FLASK_APP=hello29 $ export FLASK_ENV=development30 $ flask run3132This enables the development environment, including the interactive33debugger and reloader, and then starts the server on26*http://localhost:5000/*.34http://localhost:5000/. Use ``flask run --help`` to see the available35options, and :doc:`/cli` for detailed instructions about configuring36and using the CLI.3728The individual features of the server can be controlled by passing more29arguments to the ``run`` option. For instance the reloader can be30disabled::3132 $ flask run --no-reload3338.. note::3936 Prior to Flask 1.0 the :envvar:`FLASK_ENV` environment variable was37 not supported and you needed to enable debug mode by exporting40 Prior to Flask 1.0 the ``FLASK_ENV`` environment variable was not41 supported and you needed to enable debug mode by exporting42 ``FLASK_DEBUG=1``. This can still be used to control debug mode, but43 you should prefer setting the development environment as shown44 above.454647Lazy or Eager Loading48~~~~~~~~~~~~~~~~~~~~~4950When using the ``flask run`` command with the reloader, the server will51continue to run even if you introduce syntax errors or other52initialization errors into the code. Accessing the site will show the53interactive debugger for the error, rather than crashing the server.54This feature is called "lazy loading".5556If a syntax error is already present when calling ``flask run``, it will57fail immediately and show the traceback rather than waiting until the58site is accessed. This is intended to make errors more visible initially59while still allowing the server to handle errors on reload.6061To override this behavior and always fail immediately, even on reload,62pass the ``--eager-loading`` option. To always keep the server running,63even on the initial call, pass ``--lazy-loading``.646566In Code67-------6845The alternative way to start the application is through the46:meth:`Flask.run` method. This will immediately launch a local server47exactly the same way the :command:`flask` script does.69As an alternative to the ``flask run`` command, the development server70can also be started from Python with the :meth:`Flask.run` method. This71method takes arguments similar to the CLI options to control the server.72The main difference from the CLI command is that the server will crash73if there are errors when reloading.7449Example::75``debug=True`` can be passed to enable the debugger and reloader, but76the ``FLASK_ENV=development`` environment variable is still required to77fully enable development mode.7851 if __name__ == '__main__':52 app.run()79Place the call in a main block, otherwise it will interfere when trying80to import and run the application with a production server later.8154This works well for the common case but it does not work well for55development which is why from Flask 0.11 onwards the :command:`flask`56method is recommended. The reason for this is that due to how the reload57mechanism works there are some bizarre side-effects (like executing58certain code twice, sometimes crashing without message or dying when a59syntax or import error happens).82.. code-block:: python8361It is however still a perfectly valid method for invoking a non automatic62reloading application.84 if __name__ == "__main__":85 app.run(debug=True)8687.. code-block:: text8889 $ python hello.py
modified:src/flask/cli.py+1 added-1 removed
804 unmodified lines
805 help="Enable or disable the debugger. By default the debugger "806 "is active if debug is enabled.",807)808@click.option(809 "--eager-loading/--lazy-loader",809 "--eager-loading/--lazy-loading",810 default=None,811 help="Enable or disable eager loading. By default eager "812 "loading is enabled if the reloader is disabled.",813)
159 unmodified lines