flask/docs/server.rst

63 lines
1.9 KiB
ReStructuredText
Raw Normal View History

2014-04-28 19:27:13 +08:00
.. _server:
Development Server
==================
.. currentmodule:: flask
2016-05-27 02:52:17 +08:00
Starting with Flask 0.11 there are multiple built-in ways to run a
development server. The best one is the :command:`flask` command line utility
2014-04-28 19:27:13 +08:00
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::
2014-04-28 19:27:13 +08:00
2016-05-27 02:48:49 +08:00
$ export FLASK_APP=my_application
$ 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
2014-04-28 19:27:13 +08:00
*http://localhost:5000/*.
The individual features of the server can be controlled by passing more
2018-01-11 07:38:52 +08:00
arguments to the ``run`` option. For instance the reloader can be
2014-04-28 19:27:13 +08:00
disabled::
2016-05-27 02:48:49 +08:00
$ flask run --no-reload
2014-04-28 19:27:13 +08:00
.. note::
2018-01-11 07:38:52 +08:00
Prior to Flask 1.0 the :envvar:`FLASK_ENV` environment variable was
not supported and you needed to enable debug mode by exporting
``FLASK_DEBUG=1``. This can still be used to control debug mode, but
you should prefer setting the development environment as shown
above.
2014-04-28 19:27:13 +08:00
In Code
-------
The alternative way to start the application is through the
:meth:`Flask.run` method. This will immediately launch a local server
exactly the same way the :command:`flask` script does.
2014-04-28 19:27:13 +08:00
Example::
if __name__ == '__main__':
app.run()
This works well for the common case but it does not work well for
2016-05-27 02:48:49 +08:00
development which is why from Flask 0.11 onwards the :command:`flask`
method is recommended. The reason for this is that due to how the reload
mechanism works there are some bizarre side-effects (like executing
certain code twice, sometimes crashing without message or dying when a
syntax or import error happens).
2014-04-28 19:27:13 +08:00
It is however still a perfectly valid method for invoking a non automatic
reloading application.