server.rst
This file
Path
docs/server.rst
89 lines · 3.0 KiB
Last changed by
Merge pull request #3434 from pallets/eager-loadDavid Lordbefore adoptionbfa9ace
History of this fileTake it
1.. currentmodule:: flask23Development Server4==================56Flask 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.910.. 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------------2021The ``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:: text2728 $ export FLASK_APP=hello29 $ export FLASK_ENV=development30 $ flask run3132This enables the development environment, including the interactive33debugger and reloader, and then starts the server on34http://localhost:5000/. Use ``flask run --help`` to see the available35options, and :doc:`/cli` for detailed instructions about configuring36and using the CLI.3738.. note::3940 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-------6869As 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.7475``debug=True`` can be passed to enable the debugger and reloader, but76the ``FLASK_ENV=development`` environment variable is still required to77fully enable development mode.7879Place the call in a main block, otherwise it will interfere when trying80to import and run the application with a production server later.8182.. code-block:: python8384 if __name__ == "__main__":85 app.run(debug=True)8687.. code-block:: text8889 $ python hello.py