Skip to content
Myr

Merge pull request #1050 from julen/doc-fixes

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

authored by Kenneth Reitz · committed by Kenneth Reitz · parents 8fd1825, a700cb2 · Browse files · 1 file+16 added−17 removed
This commit

State

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

Trailers

  • Docs: fixes

Diff stats

1 file+16 added−17 removed

  • other:+16 added−17 removed

1 file+16 added−17 removed

modified:docs/cli.rst+16 added-17 removed
21 unmodified lines
2223The way this script works is by providing access to all the commands on24your Flask application's :attr:`Flask.cli` instance as well as some25built-in commands that are always there. Flask extensions can also26register more commands there if they so desire.26register more commands there if they desire so.2728For the ``flask`` script to work, an application needs to be discovered.29The two most common ways are either an environment variable30(``FLASK_APP``) or the ``--app`` / ``-a`` parameter. It should be the
74 unmodified lines
105106In case you are using factory functions to create your application (see107:ref:`app-factories`) you will discover that the ``flask`` command cannot108work with them directly. Flask won't be able to figure out how to109instanciate your application properly by itself. Because of this reason110the recommendation is to create a separate file that instanciates109instantiate your application properly by itself. Because of this reason110the recommendation is to create a separate file that instantiates111applications. This is by far not the only way to make this work. Another112is the :ref:`custom-scripts` support.113114For instance if you have a factory function that creates an application115from a filename you could make a separate file that creates such an116application from an environment variable.117118For instance this could be a file named ``autoapp.py`` with these119contents::118This could be a file named ``autoapp.py`` with these contents::119120 import os121 from yourapplication import create_app122 app = create_app(os.environ['YOURAPPLICATION_CONFIG'])
37 unmodified lines
160We won't go into detail now about the differences but if you are curious161you can have a look at the :ref:`script-info-object` section to learn all162about it.163165To explain all of this here an example ``manage.py`` script that manages a166hypothetical wiki application. We will go through the details164To explain all of this, here is an example ``manage.py`` script that165manages a hypothetical wiki application. We will go through the details166afterwards::167168 import click169 from flask.cli import FlaskGroup, script_info_option
13 unmodified lines
183184That's a lot of code for not much, so let's go through all parts step by185step.1861881. At first we import regular ``click`` as well as the click extensions1871. First we import the ``click`` library as well as the click extensions188 from the ``flask.cli`` package. Primarily we are here interested189 in the :class:`~flask.cli.FlaskGroup` click group and the190 :func:`~flask.cli.script_info_option` decorator.1912. The next thing we do is defining a function that is invoked with the193 script info object (:ref:`script-info-object`) from flask and it's192 script info object (:ref:`script-info-object`) from Flask and its193 purpose is to fully import and create the application. This can194 either directly import an application object or create it (see195 :ref:`app-factories`).196
1 unmodified line
198 script info that can be filled by options or through other means. We199 will come back to this later.2003. Next step is to create a :class:`FlaskGroup`. In this case we just201 make an empty function with a help doc string that just does nothing203 and then pass the ``create_wiki_app`` function as factory function.202 and then pass the ``create_wiki_app`` function as a factory function.203205 Whenever click now needs to operate on a flask application it will204 Whenever click now needs to operate on a Flask application it will205 call that function with the script info and ask for it to be created.2064. In step 2 you could see that the config is passed to the actual207 creation function. This config comes from the :func:`script_info_option`208 decorator for the main script. It accepts a ``--config`` option and
12 unmodified lines
221application unless it has to. The reason for this is added flexibility.222223This way an application can provide custom commands, but even in the224absence of an application the ``flask`` script is still operational on a226basic level. In addition to that does it mean that the individual227commands have the option to not create an instance of the Flask228application unless required. This is very useful as it allows the server229command for instance, the load the application on first request instead of230immediately to give a better debug experience.225basic level. In addition to that it means that the individual commands226have the option to avoid creating an instance of the Flask application227unless required. This is very useful as it allows the server commands for228instance to load the application on a first request instead of229immediately, therefore giving a better debug experience.230231All of this is provided through the :class:`flask.cli.ScriptInfo` object232and some helper utilities around. The basic way it operates is that when233the :class:`flask.cli.FlaskGroup` executes as a script it creates a script
3 unmodified lines
237238One Flask actually needs the individual Flask application it will invoke239the :meth:`flask.cli.ScriptInfo.load_app` method. This happens when the240server starts, when the shell is launched or when the script looks for an242application provided click command.241application-provided click command.