File: server.rst

package info (click to toggle)
flask 1.1.2-2%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,324 kB
  • sloc: python: 9,573; makefile: 30; sql: 22
file content (62 lines) | stat: -rw-r--r-- 1,991 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
.. _server:

Development Server
==================

.. currentmodule:: flask

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
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::

    $ export FLASK_APP=my_application
    $ export FLASK_ENV=development
    $ flask run

This enables the development environment, including the interactive
debugger and reloader, and then starts the server on
*http://localhost:5000/*.

The individual features of the server can be controlled by passing more
arguments to the ``run`` option. For instance the reloader can be
disabled::

    $ flask run --no-reload

.. note::

    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.

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.

Example::

    if __name__ == '__main__':
        app.run()

This works well for the common case but it does not work well for
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).

It is however still a perfectly valid method for invoking a non automatic
reloading application.