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
Provenance
- Commit
e9d1fc4- Tree
02a374d- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
7bb3271
4 files changed+127 added−28 removed
modified:docs/contents.rst.inc+2 added
21 unmodified lines22 appcontext23 reqcontext24 blueprints25 extensions26+ cli27+ server28 shell29 patterns/index30 deploying/index31 becomingbig30 unmodified linesmodified:docs/quickstart.rst+65 added-28 removed
18 unmodified lines19 @app.route('/')20 def hello_world():21 return 'Hello World!'22 23- if __name__ == '__main__':24- app.run()25-23 Just save it as `hello.py` (or something similar) and run it with your Python24 interpreter. Make sure to not call your application `flask.py` because this25 would conflict with Flask itself.26 30-::27+To run the application you can either use the ``flask`` command or28+python's ``-m`` switch with Flask::29 32- $ python hello.py30+ $ flask -a hello run31 * Running on http://127.0.0.1:5000/32 33+or alternatively::34+35+ $ python -m flask -a hello run36+ * Running on http://127.0.0.1:5000/37+38 Now head over to `http://127.0.0.1:5000/ <http://127.0.0.1:5000/>`_, and you39 should see your hello world greeting.40 41 So what did that code do?11 unmodified lines53 should trigger our function.54 4. 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.54-5. 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.57+5. Finally we use the Flask development server to run the local server58+ with our application.59 60 To stop the server, hit control-C.61 62 .. _public-server:4 unmodified lines67 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.70 70- 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::74 74- app.run(host='0.0.0.0')75+ flask -a hello run --host=0.0.0.076 77 This tells your operating system to listen on all public IPs.78 79+What to do if the Server does not Start80+---------------------------------------81 82+In case the ``python -m flask`` fails or ``flask`` does not exist,83+there are multiple reasons this might be the case. First of all you need84+to look at the error message.85+86+Old Version of Flask87+````````````````````88+89+Versions of Flask older than 1.0 use to have different ways to start the90+application. In short, the ``flask`` command did not exist, and91+neither did ``python -m flask``. In that case you have two options:92+either upgrade to newer Flask versions or have a look at the :ref:`server`93+docs to see the alternative method for running a server.94+95+Python older 2.796+````````````````97+98+In case you have a version of Python older than 2.7 ``python -m flask``99+does not work. You can either use ``flask`` or ``python -m100+flask.cli`` as an alternative. This is because Python before 2.7 does no101+permit packages to act as executable modules. For more information see102+:ref:`cli`.103+104+Invalid Import Name105+```````````````````106+107+The ``-a`` argument to ``flask`` is the name of the module to import. In108+case that module is incorrectly named you will get an import error upon109+start (or if debug is enabled when you navigate to the application). It110+will tell you what it tried to import and why it failed.111+112+The most common reason is a typo or because you did not actually create an113+``app`` object.114+115 .. _debug-mode:116 117 Debug Mode118 ----------119 84-The :meth:`~flask.Flask.run` method is nice to start a local85-development server, but you would have to restart it manually after each86-change to your code. That is not very nice and Flask can do better. If87-you enable debug support the server will reload itself on code changes,88-and it will also provide you with a helpful debugger if things go wrong.120+The ``flask`` script is nice to start a local development server, but121+you would have to restart it manually after each change to your code.122+That is not very nice and Flask can do better. If you enable debug123+support the server will reload itself on code changes, and it will also124+provide you with a helpful debugger if things go wrong.125 90-There are two ways to enable debugging. Either set that flag on the91-application object::126+There are different ways to enable the debug mode. The most obvious one127+is the ``--debug`` parameter to the ``flask`` command::128 93- app.debug = True94- app.run()129+ flask --debug -a hello run130 96-Or pass it as a parameter to run::131+This does the following things:132 98- app.run(debug=True)133+1. it activates the debugger134+2. it activates the automatic reloader135+3. it enables the debug mode on the Flask application.136 100-Both methods have the exact same effect.137+There are more parameters that are explained in the :ref:`server` docs.138 139 .. admonition:: Attention140 141 Even though the interactive debugger does not work in forking environments130 unmodified lines272 below. It tells Flask to behave as though it is handling a request, even273 though we are interacting with it through a Python shell. Have a look at the274 explanation below. :ref:`context-locals`).275 239-Why would you want to build URLs using the URL reversing function :func:`~flask.url_for` 240-instead of hard-coding them into your templates? There are three good reasons 241-for this:276+Why would you want to build URLs using the URL reversing function277+:func:`~flask.url_for` instead of hard-coding them into your templates?278+There are three good reasons for this:279 280 1. 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 linesadded:docs/server.rst+50 added
1+.. _server:2+3+Development Server4+==================5+6+.. currentmodule:: flask7+8+Starting with Flask 1.0 there are multiple built-in ways to run a9+development server. The best one is the ``flask`` command line utility10+but you can also continue using the :meth:`Flask.run` method.11+12+Command Line13+------------14+15+The ``flask`` command line script (:ref:`cli`) is strongly recommende for16+development because it provides a superior reload experience due to how it17+loads the application. The basic usage is like this::18+19+ $ flask -a my_application --debug run20+21+This will enable the debugger, the reloader and then start the server on 22+*http://localhost:5000/*.23+24+The individual features of the server can be controlled by passing more25+arguments to the ``run`` option. For instance the reloader can be26+disabled::27+28+ $ flask -a my_application --debug run --no-reload29+30+In Code31+-------32+33+The alternative way to start the application is through the34+:meth:`Flask.run` method. This will immediately launch a local server35+exactly the same way the ``flask`` script does.36+37+Example::38+39+ if __name__ == '__main__':40+ app.run()41+42+This works well for the common case but it does not work well for43+development which is why from Flask 1.0 onwards the ``flask`` method is44+recommended. The reason for this is that due to how the reload mechanism45+works there are some bizarre side-effects (like executing certain code46+twice, sometimes crashing without message or dieing when a syntax or47+import error happens).48+49+It is however still a perfectly valid method for invoking a non automatic50+reloading application.modified:docs/shell.rst+10 added
24 unmodified lines25 26 Generally it's recommended that you read the :ref:`request-context`27 chapter of the documentation first.28 29+Command Line Interface30+----------------------31+32+Starting 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.34+For instance the shell is automatically initialized with a loaded35+application context.36+37+For more information see :ref:`cli`.38+39 Creating a Request Context40 --------------------------41 42 The easiest way to create a proper request context from the shell is by61 unmodified lines