Skip to content
Myr

cli.rst

This file

Path

docs/cli.rst

248 lines · 8.7 KiB

Last changed by

Style the flask command consistently (#2120)Sven-Hendrik Haasebefore adoption0ba1a87

History of this file

Take it

The walk stopped after 200 commits: lines older than bd66710 point at it.

Changelog for #1763Markus Unterwaditzerbefore adoptionbd66710

lines 1–5why, one step back
1.. _cli:
23Command Line Interface4======================5

s/1.0/0.11/ in versionadded/versionchanged markersThiefMasterbefore adoptionc5900a1

line 6why, one step back
6.. versionadded:: 0.11

Changelog for #1763Markus Unterwaditzerbefore adoptionbd66710

lines 7–9why, one step back
7
8.. currentmodule:: flask9

1.0 -> 0.11 in the docsArmin Ronacherbefore adoptionf9ea3fe

line 10why, one step back
10One of the nice new features in Flask 0.11 is the built-in integration of

Changelog for #1763Markus Unterwaditzerbefore adoptionbd66710

lines 11–17why, one step back
11the `click <http://click.pocoo.org/>`_ command line interface. This
12enables a wide range of new features for the Flask ecosystem and your own13applications.1415Basic Usage16-----------17

Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c

lines 18–21why, one step back
18After installation of Flask you will now find a :command:`flask` script
19installed into your virtualenv. If you don't want to install Flask or you20have a special use-case you can also use ``python -m flask`` to accomplish21exactly the same.

Changelog for #1763Markus Unterwaditzerbefore adoptionbd66710

lines 22–27why, one step back
22
23The 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 desire so.27

Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c

lines 28–31why, one step back
28For the :command:`flask` script to work, an application needs to be
29discovered. This is achieved by exporting the ``FLASK_APP`` environment30variable. It can be either set to an import path or to a filename of a31Python module that contains a Flask application.

Changelog for #1763Markus Unterwaditzerbefore adoptionbd66710

lines 32–33why, one step back
32
33In that imported file the name of the app needs to be called ``app`` or

Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c

line 34why, one step back
34optionally be specified after a colon. For instance

clean up code formatting in some docsDavid Lordbefore adoption1928f28

line 35why, one step back
35``mymodule:application`` would tell it to use the `application` object in

Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c

line 36why, one step back
36the :file:`mymodule.py` file.

Changelog for #1763Markus Unterwaditzerbefore adoptionbd66710

line 37why, one step back
37

Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c

lines 38–39why, one step back
38Given a :file:`hello.py` file with the application in it named ``app``
39this is how it can be run.

Changelog for #1763Markus Unterwaditzerbefore adoptionbd66710

lines 40–45why, one step back
40
41Environment variables (On Windows use ``set`` instead of ``export``)::4243 export FLASK_APP=hello44 flask run45

Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c

line 46why, one step back
46Or with a filename::

Changelog for #1763Markus Unterwaditzerbefore adoptionbd66710

line 47why, one step back
47

Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c

lines 48–49why, one step back
48 export FLASK_APP=/path/to/hello.py
49 flask run

Changelog for #1763Markus Unterwaditzerbefore adoptionbd66710

lines 50–61why, one step back
50
51Virtualenv Integration52----------------------5354If you are constantly working with a virtualenv you can also put the55``export FLASK_APP`` into your ``activate`` script by adding it to the56bottom of the file. That way every time you activate your virtualenv you57automatically also activate the correct application name.5859Debug Flag60----------61

Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c

lines 62–63why, one step back
62The :command:`flask` script can also be instructed to enable the debug
63mode of the application automatically by exporting ``FLASK_DEBUG``. If

Remove nonsense from cli docsAndrew Arendtbefore adoption270355a

line 64why, one step back
64set to ``1`` debug is enabled or ``0`` disables it::

Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c

lines 65–67why, one step back
65
66 export FLASK_DEBUG=167

Changelog for #1763Markus Unterwaditzerbefore adoptionbd66710

lines 68–72why, one step back
68Running a Shell
69---------------7071To run an interactive Python shell you can use the ``shell`` command::72

Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c

line 73why, one step back
73 flask shell

Changelog for #1763Markus Unterwaditzerbefore adoptionbd66710

lines 74–87why, one step back
74
75This will start up an interactive Python shell, setup the correct76application context and setup the local variables in the shell. This is77done by invoking the :meth:`Flask.make_shell_context` method of the78application. By default you have access to your ``app`` and :data:`g`.7980Custom Commands81---------------8283If you want to add more commands to the shell script you can do this84easily. Flask uses `click`_ for the command interface which makes85creating custom commands very easy. For instance if you want a shell86command to initialize the database you can do this::87

Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c

line 88why, one step back
88 import click

Changelog for #1763Markus Unterwaditzerbefore adoptionbd66710

lines 89–95why, one step back
89 from flask import Flask
9091 app = Flask(__name__)9293 @app.cli.command()94 def initdb():95 """Initialize the database."""

Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c

line 96why, one step back
96 click.echo('Init the db')

Changelog for #1763Markus Unterwaditzerbefore adoptionbd66710

lines 97–99why, one step back
97
98The command will then show up on the command line::99

Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c

line 100why, one step back
100 $ flask initdb

Changelog for #1763Markus Unterwaditzerbefore adoptionbd66710

lines 101–124why, one step back
101 Init the db
102103Application Context104-------------------105106Most commands operate on the application so it makes a lot of sense if107they have the application context setup. Because of this, if you register108a callback on ``app.cli`` with the :meth:`~flask.cli.AppGroup.command` the109callback will automatically be wrapped through :func:`cli.with_appcontext`110which informs the cli system to ensure that an application context is set111up. This behavior is not available if a command is added later with112:func:`~click.Group.add_command` or through other means.113114It can also be disabled by passing ``with_appcontext=False`` to the115decorator::116117 @app.cli.command(with_appcontext=False)118 def example():119 pass120121Factory Functions122-----------------123124In case you are using factory functions to create your application (see

Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c

lines 125–126why, one step back
125:ref:`app-factories`) you will discover that the :command:`flask` command
126cannot work with them directly. Flask won't be able to figure out how to

Changelog for #1763Markus Unterwaditzerbefore adoptionbd66710

lines 127–128why, one step back
127instantiate your application properly by itself. Because of this reason
128the recommendation is to create a separate file that instantiates

Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c

lines 129–130why, one step back
129applications. This is not the only way to make this work. Another is the
130:ref:`custom-scripts` support.

Changelog for #1763Markus Unterwaditzerbefore adoptionbd66710

lines 131–141why, one step back
131
132For instance if you have a factory function that creates an application133from a filename you could make a separate file that creates such an134application from an environment variable.135136This could be a file named :file:`autoapp.py` with these contents::137138 import os139 from yourapplication import create_app140 app = create_app(os.environ['YOURAPPLICATION_CONFIG'])141

Style the flask command consistently (#2120)Sven-Hendrik Haasebefore adoption0ba1a87

lines 142–143why, one step back
142Once this has happened you can make the :command:`flask` command automatically
143pick it up::

Changelog for #1763Markus Unterwaditzerbefore adoptionbd66710

lines 144–154why, one step back
144
145 export YOURAPPLICATION_CONFIG=/path/to/config.cfg146 export FLASK_APP=/path/to/autoapp.py147148From this point onwards :command:`flask` will find your application.149150.. _custom-scripts:151152Custom Scripts153--------------154

Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c

lines 155–157why, one step back
155While the most common way is to use the :command:`flask` command, you can
156also make your own "driver scripts". Since Flask uses click for the157scripts there is no reason you cannot hook these scripts into any click

Changelog for #1763Markus Unterwaditzerbefore adoptionbd66710

lines 158–164why, one step back
158application. There is one big caveat and that is, that commands
159registered to :attr:`Flask.cli` will expect to be (indirectly at least)160launched from a :class:`flask.cli.FlaskGroup` click group. This is161necessary so that the commands know which Flask application they have to162work with.163164To understand why you might want custom scripts you need to understand how

Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c

lines 165–169why, one step back
165click finds and executes the Flask application. If you use the
166:command:`flask` script you specify the application to work with on the167command line or environment variable as an import name. This is simple168but it has some limitations. Primarily it does not work with application169factory functions (see :ref:`app-factories`).

Changelog for #1763Markus Unterwaditzerbefore adoptionbd66710

lines 170–179why, one step back
170
171With a custom script you don't have this problem as you can fully172customize how the application will be created. This is very useful if you173write reusable applications that you want to ship to users and they should174be presented with a custom management script.175176To explain all of this, here is an example :file:`manage.py` script that177manages a hypothetical wiki application. We will go through the details178afterwards::179

Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c

line 180why, one step back
180 import os

Changelog for #1763Markus Unterwaditzerbefore adoptionbd66710

line 181why, one step back
181 import click

Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c

line 182why, one step back
182 from flask.cli import FlaskGroup

Changelog for #1763Markus Unterwaditzerbefore adoptionbd66710

lines 183–185why, one step back
183
184 def create_wiki_app(info):185 from yourwiki import create_app

Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c

lines 186–187why, one step back
186 return create_app(
187 config=os.environ.get('WIKI_CONFIG', 'wikiconfig.py'))

Changelog for #1763Markus Unterwaditzerbefore adoptionbd66710

lines 188–189why, one step back
188
189 @click.group(cls=FlaskGroup, create_app=create_wiki_app)

Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c

line 190why, one step back
190 def cli():

Changelog for #1763Markus Unterwaditzerbefore adoptionbd66710

lines 191–200why, one step back
191 """This is a management script for the wiki application."""
192193 if __name__ == '__main__':194 cli()195196That's a lot of code for not much, so let's go through all parts step by197step.1981991. First we import the ``click`` library as well as the click extensions200 from the ``flask.cli`` package. Primarily we are here interested

Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c

line 201why, one step back
201 in the :class:`~flask.cli.FlaskGroup` click group.

Changelog for #1763Markus Unterwaditzerbefore adoptionbd66710

line 202why, one step back
2022. The next thing we do is defining a function that is invoked with the

Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c

line 203why, one step back
203 script info object (:class:`~flask.cli.ScriptInfo`) from Flask and its

Changelog for #1763Markus Unterwaditzerbefore adoptionbd66710

lines 204–205why, one step back
204 purpose is to fully import and create the application. This can
205 either directly import an application object or create it (see

Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c

lines 206–207why, one step back
206 :ref:`app-factories`). In this case we load the config from an
207 environment variable.

Changelog for #1763Markus Unterwaditzerbefore adoptionbd66710

lines 208–213why, one step back
2083. Next step is to create a :class:`FlaskGroup`. In this case we just
209 make an empty function with a help doc string that just does nothing210 and then pass the ``create_wiki_app`` function as a factory function.211212 Whenever click now needs to operate on a Flask application it will213 call that function with the script info and ask for it to be created.

Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c

line 214why, one step back
2144. All is rounded up by invoking the script.

Merge pull request #1821 from pallets/feature/plugin-cliArmin Ronacherbefore adoptionc73cf0f

lines 215–218why, one step back
215
216CLI Plugins217-----------218

clean up code formatting in some docsDavid Lordbefore adoption1928f28

line 219why, one step back
219Flask extensions can always patch the :attr:`Flask.cli` instance with more

Merge pull request #1821 from pallets/feature/plugin-cliArmin Ronacherbefore adoptionc73cf0f

line 220why, one step back
220commands if they want. However there is a second way to add CLI plugins

clean up code formatting in some docsDavid Lordbefore adoption1928f28

lines 221–222why, one step back
221to Flask which is through ``setuptools``. If you make a Python package that
222should export a Flask command line plugin you can ship a :file:`setup.py` file

Merge pull request #1821 from pallets/feature/plugin-cliArmin Ronacherbefore adoptionc73cf0f

lines 223–224why, one step back
223that declares an entrypoint that points to a click command:
224

clean up code formatting in some docsDavid Lordbefore adoption1928f28

line 225why, one step back
225Example :file:`setup.py`::

Merge pull request #1821 from pallets/feature/plugin-cliArmin Ronacherbefore adoptionc73cf0f

lines 226–237why, one step back
226
227 from setuptools import setup228229 setup(230 name='flask-my-extension',231 ...232 entry_points='''233 [flask.commands]234 my-command=mypackage.commands:cli235 ''',236 )237

clean up code formatting in some docsDavid Lordbefore adoption1928f28

line 238why, one step back
238Inside :file:`mypackage/commands.py` you can then export a Click object::

Merge pull request #1821 from pallets/feature/plugin-cliArmin Ronacherbefore adoptionc73cf0f

lines 239–248why, one step back
239
240 import click241242 @click.command()243 def cli():244 """This is an example command."""245246Once that package is installed in the same virtualenv as Flask itself you247can run ``flask my-command`` to invoke your command. This is useful to248provide extra functionality that Flask itself cannot ship.