cli.rst
This file
Path
docs/cli.rst
250 lines · 8.7 KiB
Last changed by
Fix typo in cli docsJochen Kupperschmidtbefore adoption83ae787
History of this fileTake it
The walk stopped after 200 commits: lines older than 41b5d77 point at it.
Merge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
lines 1–5why, one step back1.. _cli:23Command Line Interface4======================5s/1.0/0.11/ in versionadded/versionchanged markersThiefMasterbefore adoptionc5900a1
line 6why, one step back6.. versionadded:: 0.11Merge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
lines 7–9why, one step back78.. currentmodule:: flask91.0 -> 0.11 in the docsArmin Ronacherbefore adoptionf9ea3fe
line 10why, one step back10One of the nice new features in Flask 0.11 is the built-in integration ofMerge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
lines 11–17why, one step back11the `click <http://click.pocoo.org/>`_ command line interface. This12enables a wide range of new features for the Flask ecosystem and your own13applications.1415Basic Usage16-----------17Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
lines 18–21why, one step back18After installation of Flask you will now find a :command:`flask` script19installed 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.Merge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
lines 22–27why, one step back2223The 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.27Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
lines 28–31why, one step back28For the :command:`flask` script to work, an application needs to be29discovered. 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.Merge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
lines 32–33why, one step back3233In that imported file the name of the app needs to be called ``app`` orUpdate docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
lines 34–36why, one step back34optionally be specified after a colon. For instance35`mymodule:application` would tell it to use the `application` object in36the :file:`mymodule.py` file.Merge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
line 37why, one step back37Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
lines 38–39why, one step back38Given a :file:`hello.py` file with the application in it named ``app``39this is how it can be run.Merge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
lines 40–45why, one step back4041Environment variables (On Windows use ``set`` instead of ``export``)::4243 export FLASK_APP=hello44 flask run45Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
line 46why, one step back46Or with a filename::Merge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
line 47why, one step back47Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
lines 48–49why, one step back48 export FLASK_APP=/path/to/hello.py49 flask runMerge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
lines 50–61why, one step back5051Virtualenv 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----------61Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
lines 62–64why, one step back62The :command:`flask` script can also be instructed to enable the debug63mode of the application automatically by exporting ``FLASK_DEBUG``. If64set to ``1`` debug is enabled or ``0`` disables it.Merge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
line 65why, one step back65Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
lines 66–69why, one step back66Or with a filename::6768 export FLASK_DEBUG=169Merge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
lines 70–74why, one step back70Running a Shell71---------------7273To run an interactive Python shell you can use the ``shell`` command::74Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
line 75why, one step back75 flask shellMerge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
lines 76–89why, one step back7677This will start up an interactive Python shell, setup the correct78application context and setup the local variables in the shell. This is79done by invoking the :meth:`Flask.make_shell_context` method of the80application. By default you have access to your ``app`` and :data:`g`.8182Custom Commands83---------------8485If you want to add more commands to the shell script you can do this86easily. Flask uses `click`_ for the command interface which makes87creating custom commands very easy. For instance if you want a shell88command to initialize the database you can do this::89Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
line 90why, one step back90 import clickMerge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
lines 91–97why, one step back91 from flask import Flask9293 app = Flask(__name__)9495 @app.cli.command()96 def initdb():97 """Initialize the database."""Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
line 98why, one step back98 click.echo('Init the db')Merge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
lines 99–101why, one step back99100The command will then show up on the command line::101Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
line 102why, one step back102 $ flask initdbMerge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
lines 103–126why, one step back103 Init the db104105Application Context106-------------------107108Most commands operate on the application so it makes a lot of sense if109they have the application context setup. Because of this, if you register110a callback on ``app.cli`` with the :meth:`~flask.cli.AppGroup.command` the111callback will automatically be wrapped through :func:`cli.with_appcontext`112which informs the cli system to ensure that an application context is set113up. This behavior is not available if a command is added later with114:func:`~click.Group.add_command` or through other means.115116It can also be disabled by passing ``with_appcontext=False`` to the117decorator::118119 @app.cli.command(with_appcontext=False)120 def example():121 pass122123Factory Functions124-----------------125126In case you are using factory functions to create your application (seeUpdate docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
lines 127–128why, one step back127:ref:`app-factories`) you will discover that the :command:`flask` command128cannot work with them directly. Flask won't be able to figure out how toMerge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
lines 129–130why, one step back129instantiate your application properly by itself. Because of this reason130the recommendation is to create a separate file that instantiatesUpdate docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
lines 131–132why, one step back131applications. This is not the only way to make this work. Another is the132:ref:`custom-scripts` support.Merge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
lines 133–156why, one step back133134For instance if you have a factory function that creates an application135from a filename you could make a separate file that creates such an136application from an environment variable.137138This could be a file named :file:`autoapp.py` with these contents::139140 import os141 from yourapplication import create_app142 app = create_app(os.environ['YOURAPPLICATION_CONFIG'])143144Once this has happened you can make the flask command automatically pick145it up::146147 export YOURAPPLICATION_CONFIG=/path/to/config.cfg148 export FLASK_APP=/path/to/autoapp.py149150From this point onwards :command:`flask` will find your application.151152.. _custom-scripts:153154Custom Scripts155--------------156Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
lines 157–159why, one step back157While the most common way is to use the :command:`flask` command, you can158also make your own "driver scripts". Since Flask uses click for the159scripts there is no reason you cannot hook these scripts into any clickMerge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
lines 160–166why, one step back160application. There is one big caveat and that is, that commands161registered to :attr:`Flask.cli` will expect to be (indirectly at least)162launched from a :class:`flask.cli.FlaskGroup` click group. This is163necessary so that the commands know which Flask application they have to164work with.165166To understand why you might want custom scripts you need to understand howUpdate docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
lines 167–171why, one step back167click finds and executes the Flask application. If you use the168:command:`flask` script you specify the application to work with on the169command line or environment variable as an import name. This is simple170but it has some limitations. Primarily it does not work with application171factory functions (see :ref:`app-factories`).Merge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
lines 172–181why, one step back172173With a custom script you don't have this problem as you can fully174customize how the application will be created. This is very useful if you175write reusable applications that you want to ship to users and they should176be presented with a custom management script.177178To explain all of this, here is an example :file:`manage.py` script that179manages a hypothetical wiki application. We will go through the details180afterwards::181Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
line 182why, one step back182 import osMerge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
line 183why, one step back183 import clickUpdate docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
line 184why, one step back184 from flask.cli import FlaskGroupMerge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
lines 185–187why, one step back185186 def create_wiki_app(info):187 from yourwiki import create_appUpdate docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
lines 188–189why, one step back188 return create_app(189 config=os.environ.get('WIKI_CONFIG', 'wikiconfig.py'))Merge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
lines 190–191why, one step back190191 @click.group(cls=FlaskGroup, create_app=create_wiki_app)Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
line 192why, one step back192 def cli():Merge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
lines 193–202why, one step back193 """This is a management script for the wiki application."""194195 if __name__ == '__main__':196 cli()197198That's a lot of code for not much, so let's go through all parts step by199step.2002011. First we import the ``click`` library as well as the click extensions202 from the ``flask.cli`` package. Primarily we are here interestedUpdate docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
line 203why, one step back203 in the :class:`~flask.cli.FlaskGroup` click group.Merge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
line 204why, one step back2042. The next thing we do is defining a function that is invoked with theUpdate docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
line 205why, one step back205 script info object (:class:`~flask.cli.ScriptInfo`) from Flask and itsMerge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
lines 206–207why, one step back206 purpose is to fully import and create the application. This can207 either directly import an application object or create it (seeUpdate docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
lines 208–209why, one step back208 :ref:`app-factories`). In this case we load the config from an209 environment variable.Merge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
lines 210–215why, one step back2103. Next step is to create a :class:`FlaskGroup`. In this case we just211 make an empty function with a help doc string that just does nothing212 and then pass the ``create_wiki_app`` function as a factory function.213214 Whenever click now needs to operate on a Flask application it will215 call that function with the script info and ask for it to be created.Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
line 216why, one step back2164. All is rounded up by invoking the script.Merge pull request #1821 from pallets/feature/plugin-cliArmin Ronacherbefore adoptionc73cf0f
lines 217–239why, one step back217218CLI Plugins219-----------220221Flask extensions can always patch the `Flask.cli` instance with more222commands if they want. However there is a second way to add CLI plugins223to Flask which is through `setuptools`. If you make a Python package that224should export a Flask command line plugin you can ship a `setup.py` file225that declares an entrypoint that points to a click command:226227Example `setup.py`::228229 from setuptools import setup230231 setup(232 name='flask-my-extension',233 ...234 entry_points='''235 [flask.commands]236 my-command=mypackage.commands:cli237 ''',238 )239Fix typo in cli docsJochen Kupperschmidtbefore adoption83ae787
line 240why, one step back240Inside `mypackage/commands.py` you can then export a Click object::Merge pull request #1821 from pallets/feature/plugin-cliArmin Ronacherbefore adoptionc73cf0f
lines 241–250why, one step back241242 import click243244 @click.command()245 def cli():246 """This is an example command."""247248Once that package is installed in the same virtualenv as Flask itself you249can run ``flask my-command`` to invoke your command. This is useful to250provide extra functionality that Flask itself cannot ship.