Skip to content
Myr

cli.rst

This file

Path

docs/cli.rst

260 lines · 8.9 KiB

Last changed by

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

History of this file

Take it

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

Further refactored script system to allow more customization for special apps.Armin Ronacherbefore adoption81576c2

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

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

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

Further refactored script system to allow more customization for special apps.Armin Ronacherbefore adoption81576c2

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

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

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

Further refactored script system to allow more customization for special apps.Armin Ronacherbefore adoption81576c2

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

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

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.

Further refactored script system to allow more customization for special apps.Armin Ronacherbefore adoption81576c2

lines 22–25why, 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 also

Merge pull request #1050 from julen/doc-fixesKenneth Reitzbefore adoption32d3224

line 26why, one step back
26register more commands there if they desire so.

Further refactored script system to allow more customization for special apps.Armin Ronacherbefore adoption81576c2

line 27why, one step back
27

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

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.

Further refactored script system to allow more customization for special apps.Armin Ronacherbefore adoption81576c2

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

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

lines 34–36why, one step back
34optionally be specified after a colon. For instance
35``mymodule:application`` would tell it to use the `application` object in36the :file:`mymodule.py` file.

Further refactored script system to allow more customization for special apps.Armin Ronacherbefore adoption81576c2

line 37why, one step back
37

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

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.

Further refactored script system to allow more customization for special apps.Armin Ronacherbefore adoption81576c2

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

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

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

Further refactored script system to allow more customization for special apps.Armin Ronacherbefore adoption81576c2

line 47why, one step back
47

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

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

Further refactored script system to allow more customization for special apps.Armin Ronacherbefore adoption81576c2

lines 50–58why, 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.58

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

lines 59–70why, one step back
59Edit the activate script for the shell you use. For example:
6061Unix Bash: ``venv/bin/activate``::6263 FLASK_APP=hello64 export FLASK_APP6566Windows CMD.exe: ``venv\Scripts\activate.bat``::6768 set "FLASK_APP=hello"69 :END70

Further refactored script system to allow more customization for special apps.Armin Ronacherbefore adoption81576c2

lines 71–73why, one step back
71Debug Flag
72----------73

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

lines 74–76why, one step back
74The :command:`flask` script can also be instructed to enable the debug
75mode of the application automatically by exporting ``FLASK_DEBUG``. If76set to ``1`` debug is enabled or ``0`` disables it::

Further refactored script system to allow more customization for special apps.Armin Ronacherbefore adoption81576c2

line 77why, one step back
77

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

lines 78–79why, one step back
78 export FLASK_DEBUG=1
79

Further refactored script system to allow more customization for special apps.Armin Ronacherbefore adoption81576c2

lines 80–84why, one step back
80Running a Shell
81---------------8283To run an interactive Python shell you can use the ``shell`` command::84

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

line 85why, one step back
85 flask shell

Further refactored script system to allow more customization for special apps.Armin Ronacherbefore adoption81576c2

lines 86–99why, one step back
86
87This will start up an interactive Python shell, setup the correct88application context and setup the local variables in the shell. This is89done by invoking the :meth:`Flask.make_shell_context` method of the90application. By default you have access to your ``app`` and :data:`g`.9192Custom Commands93---------------9495If you want to add more commands to the shell script you can do this96easily. Flask uses `click`_ for the command interface which makes97creating custom commands very easy. For instance if you want a shell98command to initialize the database you can do this::99

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

line 100why, one step back
100 import click

Further refactored script system to allow more customization for special apps.Armin Ronacherbefore adoption81576c2

lines 101–107why, one step back
101 from flask import Flask
102103 app = Flask(__name__)104105 @app.cli.command()106 def initdb():107 """Initialize the database."""

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

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

Further refactored script system to allow more customization for special apps.Armin Ronacherbefore adoption81576c2

lines 109–111why, one step back
109
110The command will then show up on the command line::111

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

line 112why, one step back
112 $ flask initdb

Further refactored script system to allow more customization for special apps.Armin Ronacherbefore adoption81576c2

lines 113–114why, one step back
113 Init the db
114

Refactored the scripting interface greatly.Armin Ronacherbefore adoption932f7d7

lines 115–122why, one step back
115Application Context
116-------------------117118Most commands operate on the application so it makes a lot of sense if119they have the application context setup. Because of this, if you register120a callback on ``app.cli`` with the :meth:`~flask.cli.AppGroup.command` the121callback will automatically be wrapped through :func:`cli.with_appcontext`122which informs the cli system to ensure that an application context is set

Merge pull request #1217 from jeffwidman/patch-1Markus Unterwaditzerbefore adoption7b3e52b

line 123why, one step back
123up. This behavior is not available if a command is added later with

Refactored the scripting interface greatly.Armin Ronacherbefore adoption932f7d7

lines 124–132why, one step back
124:func:`~click.Group.add_command` or through other means.
125126It can also be disabled by passing ``with_appcontext=False`` to the127decorator::128129 @app.cli.command(with_appcontext=False)130 def example():131 pass132

Further refactored script system to allow more customization for special apps.Armin Ronacherbefore adoption81576c2

lines 133–136why, one step back
133Factory Functions
134-----------------135136In case you are using factory functions to create your application (see

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

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

Merge pull request #1050 from julen/doc-fixesKenneth Reitzbefore adoption32d3224

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

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

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

Further refactored script system to allow more customization for special apps.Armin Ronacherbefore adoption81576c2

lines 143–147why, one step back
143
144For instance if you have a factory function that creates an application145from a filename you could make a separate file that creates such an146application from an environment variable.147

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

line 148why, one step back
148This could be a file named :file:`autoapp.py` with these contents::

Further refactored script system to allow more customization for special apps.Armin Ronacherbefore adoption81576c2

lines 149–153why, one step back
149
150 import os151 from yourapplication import create_app152 app = create_app(os.environ['YOURAPPLICATION_CONFIG'])153

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

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

Further refactored script system to allow more customization for special apps.Armin Ronacherbefore adoption81576c2

lines 156–159why, one step back
156
157 export YOURAPPLICATION_CONFIG=/path/to/config.cfg158 export FLASK_APP=/path/to/autoapp.py159

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

line 160why, one step back
160From this point onwards :command:`flask` will find your application.

Greatly refactored click integration and documented it a bit more.Armin Ronacherbefore adoption3569fc2

lines 161–166why, one step back
161
162.. _custom-scripts:163164Custom Scripts165--------------166

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

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

Greatly refactored click integration and documented it a bit more.Armin Ronacherbefore adoption3569fc2

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

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

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

Greatly refactored click integration and documented it a bit more.Armin Ronacherbefore adoption3569fc2

lines 182–187why, one step back
182
183With a custom script you don't have this problem as you can fully184customize how the application will be created. This is very useful if you185write reusable applications that you want to ship to users and they should186be presented with a custom management script.187

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

line 188why, one step back
188To explain all of this, here is an example :file:`manage.py` script that

Merge pull request #1050 from julen/doc-fixesKenneth Reitzbefore adoption32d3224

line 189why, one step back
189manages a hypothetical wiki application. We will go through the details

Greatly refactored click integration and documented it a bit more.Armin Ronacherbefore adoption3569fc2

lines 190–191why, one step back
190afterwards::
191

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

line 192why, one step back
192 import os

Greatly refactored click integration and documented it a bit more.Armin Ronacherbefore adoption3569fc2

line 193why, one step back
193 import click

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

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

Greatly refactored click integration and documented it a bit more.Armin Ronacherbefore adoption3569fc2

lines 195–197why, one step back
195
196 def create_wiki_app(info):197 from yourwiki import create_app

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

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

Greatly refactored click integration and documented it a bit more.Armin Ronacherbefore adoption3569fc2

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

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

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

Greatly refactored click integration and documented it a bit more.Armin Ronacherbefore adoption3569fc2

lines 203–210why, one step back
203 """This is a management script for the wiki application."""
204205 if __name__ == '__main__':206 cli()207208That's a lot of code for not much, so let's go through all parts step by209step.210

Merge pull request #1050 from julen/doc-fixesKenneth Reitzbefore adoption32d3224

line 211why, one step back
2111. First we import the ``click`` library as well as the click extensions

Greatly refactored click integration and documented it a bit more.Armin Ronacherbefore adoption3569fc2

line 212why, one step back
212 from the ``flask.cli`` package. Primarily we are here interested

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

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

Greatly refactored click integration and documented it a bit more.Armin Ronacherbefore adoption3569fc2

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

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

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

Greatly refactored click integration and documented it a bit more.Armin Ronacherbefore adoption3569fc2

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

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

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

Greatly refactored click integration and documented it a bit more.Armin Ronacherbefore adoption3569fc2

lines 220–221why, one step back
2203. Next step is to create a :class:`FlaskGroup`. In this case we just
221 make an empty function with a help doc string that just does nothing

Merge pull request #1050 from julen/doc-fixesKenneth Reitzbefore adoption32d3224

line 222why, one step back
222 and then pass the ``create_wiki_app`` function as a factory function.

Greatly refactored click integration and documented it a bit more.Armin Ronacherbefore adoption3569fc2

line 223why, one step back
223

Merge pull request #1050 from julen/doc-fixesKenneth Reitzbefore adoption32d3224

line 224why, one step back
224 Whenever click now needs to operate on a Flask application it will

Greatly refactored click integration and documented it a bit more.Armin Ronacherbefore adoption3569fc2

line 225why, one step back
225 call that function with the script info and ask for it to be created.

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

line 226why, one step back
2264. All is rounded up by invoking the script.

Greatly refactored click integration and documented it a bit more.Armin Ronacherbefore adoption3569fc2

line 227why, one step back
227

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

lines 228–229why, one step back
228CLI Plugins
229-----------

Greatly refactored click integration and documented it a bit more.Armin Ronacherbefore adoption3569fc2

line 230why, one step back
230

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

lines 231–235why, one step back
231Flask extensions can always patch the :attr:`Flask.cli` instance with more
232commands if they want. However there is a second way to add CLI plugins233to Flask which is through ``setuptools``. If you make a Python package that234should export a Flask command line plugin you can ship a :file:`setup.py` file235that declares an entrypoint that points to a click command:

Greatly refactored click integration and documented it a bit more.Armin Ronacherbefore adoption3569fc2

line 236why, one step back
236

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

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

Greatly refactored click integration and documented it a bit more.Armin Ronacherbefore adoption3569fc2

line 238why, one step back
238

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

line 239why, one step back
239 from setuptools import setup

Greatly refactored click integration and documented it a bit more.Armin Ronacherbefore adoption3569fc2

line 240why, one step back
240

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

lines 241–248why, one step back
241 setup(
242 name='flask-my-extension',243 ...244 entry_points='''245 [flask.commands]246 my-command=mypackage.commands:cli247 ''',248 )

Greatly refactored click integration and documented it a bit more.Armin Ronacherbefore adoption3569fc2

line 249why, one step back
249

Merge branch 'master' into jrmccarthy-masterDavid Lordbefore adoption8ad4f47

lines 250–260why, one step back
250Inside :file:`mypackage/commands.py` you can then export a Click object::
251252 import click253254 @click.command()255 def cli():256 """This is an example command."""257258Once that package is installed in the same virtualenv as Flask itself you259can run ``flask my-command`` to invoke your command. This is useful to260provide extra functionality that Flask itself cannot ship.