Skip to content
Myr

Updated docs for click support

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

authored by Armin Ronacher · committed by Armin Ronacher · parent 7bb3271 · Browse files · 4 files+127 added−28 removed
This commit

State

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

Diff stats

4 files+127 added−28 removed

  • other:+127 added−28 removed

4 files+127 added−28 removed

modified:docs/contents.rst.inc+2 added
21 unmodified lines
22 appcontext23 reqcontext24 blueprints25 extensions26 cli27 server28 shell29 patterns/index30 deploying/index31 becomingbig
30 unmodified lines
modified:docs/quickstart.rst+65 added-28 removed
18 unmodified lines
19 @app.route('/')20 def hello_world():21 return 'Hello World!'2223 if __name__ == '__main__':24 app.run()2523Just save it as `hello.py` (or something similar) and run it with your Python24interpreter. Make sure to not call your application `flask.py` because this25would conflict with Flask itself.2630::27To run the application you can either use the ``flask`` command or28python's ``-m`` switch with Flask::2932 $ python hello.py30 $ flask -a hello run31 * Running on http://127.0.0.1:5000/3233or alternatively::3435 $ python -m flask -a hello run36 * Running on http://127.0.0.1:5000/3738Now head over to `http://127.0.0.1:5000/ <http://127.0.0.1:5000/>`_, and you39should see your hello world greeting.4041So what did that code do?
11 unmodified lines
53 should trigger our function.544. The function is given a name which is also used to generate URLs for that55 particular function, and returns the message we want to display in the56 user's browser.545. Finally we use the :meth:`~flask.Flask.run` function to run the local server55 with our application. The ``if __name__ == '__main__':`` makes sure the56 server only runs if the script is executed directly from the Python57 interpreter and not used as an imported module.575. Finally we use the Flask development server to run the local server58 with our application.5960To stop the server, hit control-C.6162.. _public-server:
4 unmodified lines
67 from your own computer, not from any other in the network. This is the68 default because in debugging mode a user of the application can execute69 arbitrary Python code on your computer.7070 If you have `debug` disabled or trust the users on your network, you can71 make the server publicly available simply by changing the call of the72 :meth:`~flask.Flask.run` method to look like this::71 If you have the debugger disabled or trust the users on your network,72 you can make the server publicly available simply by adding73 ``--host=0.0.0.0`` to the command line::7474 app.run(host='0.0.0.0')75 flask -a hello run --host=0.0.0.07677 This tells your operating system to listen on all public IPs.7879What to do if the Server does not Start80---------------------------------------8182In case the ``python -m flask`` fails or ``flask`` does not exist,83there are multiple reasons this might be the case. First of all you need84to look at the error message.8586Old Version of Flask87````````````````````8889Versions of Flask older than 1.0 use to have different ways to start the90application. In short, the ``flask`` command did not exist, and91neither did ``python -m flask``. In that case you have two options:92either upgrade to newer Flask versions or have a look at the :ref:`server`93docs to see the alternative method for running a server.9495Python older 2.796````````````````9798In case you have a version of Python older than 2.7 ``python -m flask``99does not work. You can either use ``flask`` or ``python -m100flask.cli`` as an alternative. This is because Python before 2.7 does no101permit packages to act as executable modules. For more information see102:ref:`cli`.103104Invalid Import Name105```````````````````106107The ``-a`` argument to ``flask`` is the name of the module to import. In108case that module is incorrectly named you will get an import error upon109start (or if debug is enabled when you navigate to the application). It110will tell you what it tried to import and why it failed.111112The most common reason is a typo or because you did not actually create an113``app`` object.114115.. _debug-mode:116117Debug Mode118----------11984The :meth:`~flask.Flask.run` method is nice to start a local85development server, but you would have to restart it manually after each86change to your code. That is not very nice and Flask can do better. If87you enable debug support the server will reload itself on code changes,88and it will also provide you with a helpful debugger if things go wrong.120The ``flask`` script is nice to start a local development server, but121you would have to restart it manually after each change to your code.122That is not very nice and Flask can do better. If you enable debug123support the server will reload itself on code changes, and it will also124provide you with a helpful debugger if things go wrong.12590There are two ways to enable debugging. Either set that flag on the91application object::126There are different ways to enable the debug mode. The most obvious one127is the ``--debug`` parameter to the ``flask`` command::12893 app.debug = True94 app.run()129 flask --debug -a hello run13096Or pass it as a parameter to run::131This does the following things:13298 app.run(debug=True)1331. it activates the debugger1342. it activates the automatic reloader1353. it enables the debug mode on the Flask application.136100Both methods have the exact same effect.137There are more parameters that are explained in the :ref:`server` docs.138139.. admonition:: Attention140141 Even though the interactive debugger does not work in forking environments
130 unmodified lines
272below. It tells Flask to behave as though it is handling a request, even273though we are interacting with it through a Python shell. Have a look at the274explanation below. :ref:`context-locals`).275239Why would you want to build URLs using the URL reversing function :func:`~flask.url_for` 240instead of hard-coding them into your templates? There are three good reasons 241for this:276Why would you want to build URLs using the URL reversing function277:func:`~flask.url_for` instead of hard-coding them into your templates?278There are three good reasons for this:2792801. Reversing is often more descriptive than hard-coding the URLs. More281 importantly, it allows you to change URLs in one go, without having to282 remember to change URLs all over the place.
610 unmodified lines
added:docs/server.rst+50 added
1.. _server:23Development Server4==================56.. currentmodule:: flask78Starting with Flask 1.0 there are multiple built-in ways to run a9development server. The best one is the ``flask`` command line utility10but you can also continue using the :meth:`Flask.run` method.1112Command Line13------------1415The ``flask`` command line script (:ref:`cli`) is strongly recommende for16development because it provides a superior reload experience due to how it17loads the application. The basic usage is like this::1819 $ flask -a my_application --debug run2021This will enable the debugger, the reloader and then start the server on 22*http://localhost:5000/*.2324The individual features of the server can be controlled by passing more25arguments to the ``run`` option. For instance the reloader can be26disabled::2728 $ flask -a my_application --debug run --no-reload2930In Code31-------3233The alternative way to start the application is through the34:meth:`Flask.run` method. This will immediately launch a local server35exactly the same way the ``flask`` script does.3637Example::3839 if __name__ == '__main__':40 app.run()4142This works well for the common case but it does not work well for43development which is why from Flask 1.0 onwards the ``flask`` method is44recommended. The reason for this is that due to how the reload mechanism45works there are some bizarre side-effects (like executing certain code46twice, sometimes crashing without message or dieing when a syntax or47import error happens).4849It is however still a perfectly valid method for invoking a non automatic50reloading application.
modified:docs/shell.rst+10 added
24 unmodified lines
2526Generally it's recommended that you read the :ref:`request-context`27chapter of the documentation first.2829Command Line Interface30----------------------3132Starting with Flask 1.0 the recommended way to work with the shell is the33``flask shell`` command which does a lot of this automatically for you.34For instance the shell is automatically initialized with a loaded35application context.3637For more information see :ref:`cli`.3839Creating a Request Context40--------------------------4142The easiest way to create a proper request context from the shell is by
61 unmodified lines