cli.rst
This file
Path
docs/cli.rst
543 lines · 16.4 KiB
Last changed by
Merge pull request #2784 from doobeh/masterDavid Lordbefore adoptionb837606
History of this fileTake it
The walk stopped after 200 commits: lines older than 8e2ef5b point at it.
Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 1–2why, one step back1.. currentmodule:: flask2Fix typo in docs (#2419)asmith26before adoption8e2ef5b
lines 3–7why, one step back3.. _cli:45Command Line Interface6======================7Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 8–11why, one step back8Installing Flask installs the ``flask`` script, a `Click`_ command line9interface, in your virtualenv. Executed from the terminal, this script gives10access to built-in, extension, and application-defined commands. The ``--help``11option will give more information about any commands and options.Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 12why, one step back12Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 13why, one step back13.. _Click: http://click.pocoo.org/Fix typo in docs (#2419)asmith26before adoption8e2ef5b
lines 14–15why, one step back1415Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 16–17why, one step back16Application Discovery17---------------------Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 18why, one step back18Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 19–21why, one step back19The ``flask`` command is installed by Flask, not your application; it must be20told where to find your application in order to use it. The ``FLASK_APP``21environment variable is used to specify how to load the application.Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 22why, one step back22Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 23why, one step back23Unix Bash (Linux, Mac, etc.)::Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 24why, one step back24Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 25–26why, one step back25 $ export FLASK_APP=hello26 $ flask runFix typo in docs (#2419)asmith26before adoption8e2ef5b
line 27why, one step back27Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 28why, one step back28Windows CMD::Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 29why, one step back29Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 30–31why, one step back30 > set FLASK_APP=hello31 > flask runFix typo in docs (#2419)asmith26before adoption8e2ef5b
line 32why, one step back32Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 33why, one step back33Windows PowerShell::Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 34why, one step back34Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 35–36why, one step back35 > $env:FLASK_APP = "hello"36 > flask runFix typo in docs (#2419)asmith26before adoption8e2ef5b
line 37why, one step back37Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 38–39why, one step back38While ``FLASK_APP`` supports a variety of options for specifying your39application, most use cases should be simple. Here are the typical values:Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 40why, one step back40Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 41–44why, one step back41(nothing)42 The file :file:`wsgi.py` is imported, automatically detecting an app43 (``app``). This provides an easy way to create an app from a factory with44 extra arguments.Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 45why, one step back45Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 46–48why, one step back46``FLASK_APP=hello``47 The name is imported, automatically detecting an app (``app``) or factory48 (``create_app``).Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 49why, one step back49Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 50why, one step back50----Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 51why, one step back51Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 52–56why, one step back52``FLASK_APP`` has three parts: an optional path that sets the current working53directory, a Python file or dotted import path, and an optional variable54name of the instance or factory. If the name is a factory, it can optionally55be followed by arguments in parentheses. The following values demonstrate these56parts:Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 57why, one step back57Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 58–59why, one step back58``FLASK_APP=src/hello``59 Sets the current working directory to ``src`` then imports ``hello``.Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 60why, one step back60Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 61–62why, one step back61``FLASK_APP=hello.web``62 Imports the path ``hello.web``.Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 63why, one step back63Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 64–65why, one step back64``FLASK_APP=hello:app2``65 Uses the ``app2`` Flask instance in ``hello``.Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 66why, one step back66Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 67–69why, one step back67``FLASK_APP="hello:create_app('dev')"``68 The ``create_app`` factory in ``hello`` is called with the string ``'dev'``69 as the argument.Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 70why, one step back70Merge branch '1.0.x'David Lordbefore adoption2236ba9
lines 71–73why, one step back71If ``FLASK_APP`` is not set, the command will try to import "app" or72"wsgi" (as a ".py" file, or package) and try to detect an application73instance or factory.Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 74why, one step back74Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 75–78why, one step back75Within the given import, the command looks for an application instance named76``app`` or ``application``, then any application instance. If no instance is77found, the command looks for a factory function named ``create_app`` or78``make_app`` that returns an instance.Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 79why, one step back79Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 80why, one step back80When calling an application factory, if the factory takes an argument namedMerge pull request #2870 from jerr0328/docs-update-cli-scriptinfoDavid Lordbefore adoption93e4b71
lines 81–86why, one step back81``script_info``, then the :class:`~cli.ScriptInfo` instance is passed as a82keyword argument. If the application factory takes only one argument and no83parentheses follow the factory name, the :class:`~cli.ScriptInfo` instance84is passed as a positional argument. If parentheses follow the factory name,85their contents are parsed as Python literals and passes as arguments to the86function. This means that strings must still be in quotes.Fix typo in docs (#2419)asmith26before adoption8e2ef5b
lines 87–88why, one step back8788Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 89–90why, one step back89Run the Development Server90--------------------------Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 91why, one step back91Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 92–93why, one step back92The :func:`run <cli.run_command>` command will start the development server. It93replaces the :meth:`Flask.run` method in most cases. ::Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 94why, one step back94Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 95–97why, one step back95 $ flask run96 * Serving Flask app "hello"97 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 98why, one step back98Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 99–102why, one step back99.. warning:: Do not use this command to run your application in production.100 Only use the development server during development. The development server101 is provided for convenience, but is not designed to be particularly secure,102 stable, or efficient. See :ref:`deployment` for how to run in production.Fix typo in docs (#2419)asmith26before adoption8e2ef5b
lines 103–104why, one step back103104Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 105–106why, one step back105Open a Shell106------------Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 107why, one step back107Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 108–110why, one step back108To explore the data in your application, you can start an interactive Python109shell with the :func:`shell <cli.shell_command>` command. An application110context will be active, and the app instance will be imported. ::Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 111why, one step back111Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 112–117why, one step back112 $ flask shell113 Python 3.6.2 (default, Jul 20 2017, 03:52:27)114 [GCC 7.1.1 20170630] on linux115 App: example116 Instance: /home/user/Projects/hello/instance117 >>>Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 118why, one step back118Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 119why, one step back119Use :meth:`~Flask.shell_context_processor` to add other automatic imports.Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 120why, one step back120Merge pull request #2593 from pallets/env-docsDavid Lordbefore adoptionc3a9978
line 121why, one step back121Add Support for FLASK_ENV (#2570)Armin Ronacherbefore adoption2433522
lines 122–123why, one step back122Environments123------------Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 124why, one step back124Add Support for FLASK_ENV (#2570)Armin Ronacherbefore adoption2433522
lines 125–126why, one step back125.. versionadded:: 1.0126Merge pull request #2593 from pallets/env-docsDavid Lordbefore adoptionc3a9978
lines 127–131why, one step back127The environment in which the Flask app runs is set by the128:envvar:`FLASK_ENV` environment variable. If not set it defaults to129``production``. The other recognized environment is ``development``.130Flask and extensions may choose to enable behaviors based on the131environment.Add Support for FLASK_ENV (#2570)Armin Ronacherbefore adoption2433522
line 132why, one step back132Merge pull request #2593 from pallets/env-docsDavid Lordbefore adoptionc3a9978
lines 133–135why, one step back133If the env is set to ``development``, the ``flask`` command will enable134debug mode and ``flask run`` will enable the interactive debugger and135reloader.Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 136why, one step back136Add Support for FLASK_ENV (#2570)Armin Ronacherbefore adoption2433522
lines 137–138why, one step back137::138Merge pull request #2593 from pallets/env-docsDavid Lordbefore adoptionc3a9978
line 139why, one step back139 $ FLASK_ENV=development flask runMerge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 140why, one step back140 * Serving Flask app "hello"Merge pull request #2593 from pallets/env-docsDavid Lordbefore adoptionc3a9978
lines 141–142why, one step back141 * Environment: development142 * Debug mode: onMerge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 143–146why, one step back143 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)144 * Restarting with inotify reloader145 * Debugger is active!146 * Debugger PIN: 223-456-919Merge pull request #2593 from pallets/env-docsDavid Lordbefore adoptionc3a9978
lines 147–148why, one step back147148Merge pull request #2898 from rochacbruno/2897-Add-Extra-Files-to-CLIDavid Lordbefore adoption2616d97
lines 149–167why, one step back149Watch Extra Files with the Reloader150~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~151152When using development mode, the reloader will trigger whenever your153Python code or imported modules change. The reloader can watch154additional files with the ``--extra-files`` option, or the155``FLASK_RUN_EXTRA_FILES`` environment variable. Multiple paths are156separated with ``:``, or ``;`` on Windows.157158.. code-block:: none159160 $ flask run --extra-files file1:dirA/file2:dirB/161 # or162 $ export FLASK_RUN_EXTRA_FILES=file1:dirA/file2:dirB/163 $ flask run164 * Running on http://127.0.0.1:8000/165 * Detected change in '/path/to/file1', reloading166167Merge pull request #2593 from pallets/env-docsDavid Lordbefore adoptionc3a9978
lines 168–173why, one step back168Debug Mode169----------170171Debug mode will be enabled when :envvar:`FLASK_ENV` is ``development``,172as described above. If you want to control debug mode separately, use173:envvar:`FLASK_DEBUG`. The value ``1`` enables it, ``0`` disables it.Fix typo in docs (#2419)asmith26before adoption8e2ef5b
lines 174–175why, one step back174175Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
lines 176–177why, one step back176.. _dotenv:177Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 178–179why, one step back178Environment Variables From dotenv179---------------------------------Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
line 180why, one step back180Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 181–184why, one step back181Rather than setting ``FLASK_APP`` each time you open a new terminal, you can182use Flask's dotenv support to set environment variables automatically.183184If `python-dotenv`_ is installed, running the ``flask`` command will setMerge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
lines 185–194why, one step back185environment variables defined in the files :file:`.env` and :file:`.flaskenv`.186This can be used to avoid having to set ``FLASK_APP`` manually every time you187open a new terminal, and to set configuration using environment variables188similar to how some deployment services work.189190Variables set on the command line are used over those set in :file:`.env`,191which are used over those set in :file:`.flaskenv`. :file:`.flaskenv` should be192used for public variables, such as ``FLASK_APP``, while :file:`.env` should not193be committed to your repository so that it can set private variables.194Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 195why, one step back195Directories are scanned upwards from the directory you call ``flask``Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
lines 196–199why, one step back196from to locate the files. The current working directory will be set to the197location of the file, with the assumption that that is the top level project198directory.199Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 200–202why, one step back200The files are only loaded by the ``flask`` command or calling201:meth:`~Flask.run`. If you would like to load these files when running in202production, you should call :func:`~cli.load_dotenv` manually.Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
lines 203–205why, one step back203204.. _python-dotenv: https://github.com/theskumar/python-dotenv#readme205Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 206why, one step back206document cli option env varsDavid Lordbefore adoption93698b5
lines 207–214why, one step back207Setting Command Options208~~~~~~~~~~~~~~~~~~~~~~~209210Click is configured to load default values for command options from211environment variables. The variables use the pattern212``FLASK_COMMAND_OPTION``. For example, to set the port for the run213command, instead of ``flask run --port 8000``:214Merge pull request #3175 from singingwolfboy/fix-rst-line-too-longDavid Baumgoldbefore adoption819368a
line 215why, one step back215.. code-block:: bashdocument cli option env varsDavid Lordbefore adoption93698b5
line 216why, one step back216Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6
lines 217–218why, one step back217 $ export FLASK_RUN_PORT=8000218 $ flask rundocument cli option env varsDavid Lordbefore adoption93698b5
lines 219–224why, one step back219 * Running on http://127.0.0.1:8000/220221These can be added to the ``.flaskenv`` file just like ``FLASK_APP`` to222control default command options.223224Merge branch '1.0-maintenance'David Lordbefore adoption1fa9185
lines 225–230why, one step back225Disable dotenv226~~~~~~~~~~~~~~227228The ``flask`` command will show a message if it detects dotenv files but229python-dotenv is not installed.230Merge pull request #3175 from singingwolfboy/fix-rst-line-too-longDavid Baumgoldbefore adoption819368a
line 231why, one step back231.. code-block:: bashMerge branch '1.0-maintenance'David Lordbefore adoption1fa9185
line 232why, one step back232Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6
line 233why, one step back233 $ flask runMerge branch '1.0-maintenance'David Lordbefore adoption1fa9185
lines 234–242why, one step back234 * Tip: There are .env files present. Do "pip install python-dotenv" to use them.235236You can tell Flask not to load dotenv files even when python-dotenv is237installed by setting the ``FLASK_SKIP_DOTENV`` environment variable.238This can be useful if you want to load them manually, or if you're using239a project runner that loads them already. Keep in mind that the240environment variables must be set before the app loads or it won't241configure as expected.242Merge pull request #3175 from singingwolfboy/fix-rst-line-too-longDavid Baumgoldbefore adoption819368a
line 243why, one step back243.. code-block:: bashMerge branch '1.0-maintenance'David Lordbefore adoption1fa9185
line 244why, one step back244Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6
lines 245–246why, one step back245 $ export FLASK_SKIP_DOTENV=1246 $ flask runMerge branch '1.0-maintenance'David Lordbefore adoption1fa9185
lines 247–248why, one step back247248Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 249–250why, one step back249Environment Variables From virtualenv250-------------------------------------Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 251why, one step back251Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 252–254why, one step back252If you do not want to install dotenv support, you can still set environment253variables by adding them to the end of the virtualenv's :file:`activate`254script. Activating the virtualenv will set the variables.Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 255why, one step back255Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 256why, one step back256Unix Bash, :file:`venv/bin/activate`::Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 257why, one step back257Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6
line 258why, one step back258 $ export FLASK_APP=helloFix typo in docs (#2419)asmith26before adoption8e2ef5b
line 259why, one step back259Merge pull request #2553 from greyli/patch-6David Lordbefore adoptiondb56e38
line 260why, one step back260Windows CMD, :file:`venv\\Scripts\\activate.bat`::Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 261why, one step back261Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6
line 262why, one step back262 > set FLASK_APP=helloFix typo in docs (#2419)asmith26before adoption8e2ef5b
line 263why, one step back263Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 264–266why, one step back264It is preferred to use dotenv support over this, since :file:`.flaskenv` can be265committed to the repository so that it works automatically wherever the project266is checked out.Fix typo in docs (#2419)asmith26before adoption8e2ef5b
lines 267–268why, one step back267268Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 269–270why, one step back269Custom Commands270---------------Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 271why, one step back271Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 272–273why, one step back272The ``flask`` command is implemented using `Click`_. See that project's273documentation for full information about writing commands.Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 274why, one step back274Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 275–276why, one step back275This example adds the command ``create_user`` that takes the argument276``name``. ::Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 277why, one step back277Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 278–279why, one step back278 import click279 from flask import FlaskFix typo in docs (#2419)asmith26before adoption8e2ef5b
line 280why, one step back280Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 281why, one step back281 app = Flask(__name__)Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 282why, one step back282Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 283–286why, one step back283 @app.cli.command()284 @click.argument('name')285 def create_user(name):286 ...Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 287why, one step back287Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 288–289why, one step back288::289Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6
line 290why, one step back290 $ flask create_user adminMerge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 291–294why, one step back291292This example adds the same command, but as ``user create``, a command in a293group. This is useful if you want to organize multiple related commands. ::294Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 295why, one step back295 import clickMerge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 296–297why, one step back296 from flask import Flask297 from flask.cli import AppGroupFix typo in docs (#2419)asmith26before adoption8e2ef5b
line 298why, one step back298Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 299–300why, one step back299 app = Flask(__name__)300 user_cli = AppGroup('user')Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 301why, one step back301Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 302–305why, one step back302 @user_cli.command('create')303 @click.argument('name')304 def create_user(name):305 ...Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 306why, one step back306Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 307why, one step back307 app.cli.add_command(user_cli)Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 308why, one step back308Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 309why, one step back309::Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 310why, one step back310Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6
line 311why, one step back311 $ flask user create demoFix typo in docs (#2419)asmith26before adoption8e2ef5b
line 312why, one step back312Merge pull request #2784 from doobeh/masterDavid Lordbefore adoptionb837606
line 313why, one step back313Merge pull request #2611 from pallets/cli-test-docsDavid Lordbefore adoption8c2c802
lines 314–315why, one step back314See :ref:`testing-cli` for an overview of how to test your custom315commands.Merge pull request #2784 from doobeh/masterDavid Lordbefore adoptionb837606
lines 316–370why, one step back316317318Registering Commands with Blueprints319~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~320321If your application uses blueprints, you can optionally register CLI322commands directly onto them. When your blueprint is registered onto your323application, the associated commands will be available to the ``flask``324command. By default, those commands will be nested in a group matching325the name of the blueprint.326327.. code-block:: python328329 from flask import Blueprint330331 bp = Blueprint('students', __name__)332333 @bp.cli.command('create')334 @click.argument('name')335 def create(name):336 ...337338 app.register_blueprint(bp)339340.. code-block:: text341342 $ flask students create alice343344You can alter the group name by specifying the ``cli_group`` parameter345when creating the :class:`Blueprint` object, or later with346:meth:`app.register_blueprint(bp, cli_group='...') <Flask.register_blueprint>`.347The following are equivalent:348349.. code-block:: python350351 bp = Blueprint('students', __name__, cli_group='other')352 # or353 app.register_blueprint(bp, cli_group='other')354355.. code-block:: text356357 $ flask other create alice358359Specifying ``cli_group=None`` will remove the nesting and merge the360commands directly to the application's level:361362.. code-block:: python363364 bp = Blueprint('students', __name__, cli_group=None)365 # or366 app.register_blueprint(bp, cli_group=None)367368.. code-block:: text369370 $ flask create aliceMerge pull request #2611 from pallets/cli-test-docsDavid Lordbefore adoption8c2c802
line 371why, one step back371Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 372why, one step back372Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 373–374why, one step back373Application Context374~~~~~~~~~~~~~~~~~~~Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 375why, one step back375Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 376–381why, one step back376Commands added using the Flask app's :attr:`~Flask.cli`377:meth:`~cli.AppGroup.command` decorator will be executed with an application378context pushed, so your command and extensions have access to the app and its379configuration. If you create a command using the Click :func:`~click.command`380decorator instead of the Flask decorator, you can use381:func:`~cli.with_appcontext` to get the same behavior. ::Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 382why, one step back382Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 383–384why, one step back383 import click384 from flask.cli import with_appcontextFix typo in docs (#2419)asmith26before adoption8e2ef5b
line 385why, one step back385Merge pull request #2915 from loomchild/masterDavid Lordbefore adoptionc92001d
line 386why, one step back386 @click.command()Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 387–406why, one step back387 @with_appcontext388 def do_work():389 ...390391 app.cli.add_command(do_work)392393If you're sure a command doesn't need the context, you can disable it::394395 @app.cli.command(with_appcontext=False)396 def do_work():397 ...398399400Plugins401-------402403Flask will automatically load commands specified in the ``flask.commands``404`entry point`_. This is useful for extensions that want to add commands when405they are installed. Entry points are specified in :file:`setup.py` ::406Fix typo in docs (#2419)asmith26before adoption8e2ef5b
lines 407–410why, one step back407 from setuptools import setup408409 setup(410 name='flask-my-extension',Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 411–416why, one step back411 ...,412 entry_points={413 'flask.commands': [414 'my-command=flask_my_extension.commands:cli'415 ],416 },Fix typo in docs (#2419)asmith26before adoption8e2ef5b
lines 417–419why, one step back417 )418419Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 420–424why, one step back420.. _entry point: https://packaging.python.org/tutorials/distributing-packages/#entry-points421422Inside :file:`flask_my_extension/commands.py` you can then export a Click423object::424Fix typo in docs (#2419)asmith26before adoption8e2ef5b
lines 425–428why, one step back425 import click426427 @click.command()428 def cli():Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 429why, one step back429 ...Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 430why, one step back430Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 431–432why, one step back431Once that package is installed in the same virtualenv as your Flask project,432you can run ``flask my-command`` to invoke the command.Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 433why, one step back433Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 434–479why, one step back434435.. _custom-scripts:436437Custom Scripts438--------------439440When you are using the app factory pattern, it may be more convenient to define441your own Click script. Instead of using ``FLASK_APP`` and letting Flask load442your application, you can create your own Click object and export it as a443`console script`_ entry point.444445Create an instance of :class:`~cli.FlaskGroup` and pass it the factory::446447 import click448 from flask import Flask449 from flask.cli import FlaskGroup450451 def create_app():452 app = Flask('wiki')453 # other setup454 return app455456 @click.group(cls=FlaskGroup, create_app=create_app)457 def cli():458 """Management script for the Wiki application."""459460Define the entry point in :file:`setup.py`::461462 from setuptools import setup463464 setup(465 name='flask-my-extension',466 ...,467 entry_points={468 'console_scripts': [469 'wiki=wiki:cli'470 ],471 },472 )473474Install the application in the virtualenv in editable mode and the custom475script is available. Note that you don't need to set ``FLASK_APP``. ::476477 $ pip install -e .478 $ wiki run479Merge pull request #2610 from pallets/cli-syntaxerrorDavid Lordbefore adoption94aaa24
lines 480–488why, one step back480.. admonition:: Errors in Custom Scripts481482 When using a custom script, if you introduce an error in your483 module-level code, the reloader will fail because it can no longer484 load the entry point.485486 The ``flask`` command, being separate from your code, does not have487 this issue and is recommended in most cases.488Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 489–491why, one step back489.. _console script: https://packaging.python.org/tutorials/distributing-packages/#console-scripts490491Fix typo in docs (#2419)asmith26before adoption8e2ef5b
lines 492–494why, one step back492PyCharm Integration493-------------------494Merge pull request #2610 from pallets/cli-syntaxerrorDavid Lordbefore adoption94aaa24
lines 495–498why, one step back495Prior to PyCharm 2018.1, the Flask CLI features weren't yet fully496integrated into PyCharm. We have to do a few tweaks to get them working497smoothly. These instructions should be similar for any other IDE you498might want to use.Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 499why, one step back499Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21
lines 500–502why, one step back500In PyCharm, with your project open, click on *Run* from the menu bar and501go to *Edit Configurations*. You'll be greeted by a screen similar to502this:Fix typo in docs (#2419)asmith26before adoption8e2ef5b
lines 503–504why, one step back503504.. image:: _static/pycharm-runconfig.pngMerge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21
lines 505–507why, one step back505 :align: center506 :class: screenshot507 :alt: screenshot of pycharm's run configuration settingsFix typo in docs (#2419)asmith26before adoption8e2ef5b
line 508why, one step back508Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21
lines 509–512why, one step back509There's quite a few options to change, but once we've done it for one510command, we can easily copy the entire configuration and make a single511tweak to give us access to other commands, including any custom ones you512may implement yourself.Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 513why, one step back513Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21
lines 514–517why, one step back514Click the + (*Add New Configuration*) button and select *Python*. Give515the configuration a good descriptive name such as "Run Flask Server".516For the ``flask run`` command, check "Single instance only" since you517can't run the server more than once at the same time.Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 518why, one step back518Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21
line 519why, one step back519Select *Module name* from the dropdown (**A**) then input ``flask``.Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 520why, one step back520Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21
lines 521–523why, one step back521The *Parameters* field (**B**) is set to the CLI command to execute522(with any arguments). In this example we use ``run``, which will run523the development server.Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 524why, one step back524Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21
lines 525–528why, one step back525You can skip this next step if you're using :ref:`dotenv`. We need to526add an environment variable (**C**) to identify our application. Click527on the browse button and add an entry with ``FLASK_APP`` on the left and528the Python import or file on the right (``hello`` for example).Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 529why, one step back529Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21
lines 530–531why, one step back530Next we need to set the working directory (**D**) to be the folder where531our application resides.Fix typo in docs (#2419)asmith26before adoption8e2ef5b
line 532why, one step back532Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21
lines 533–542why, one step back533If you have installed your project as a package in your virtualenv, you534may untick the *PYTHONPATH* options (**E**). This will more accurately535match how you deploy the app later.536537Click *Apply* to save the configuration, or *OK* to save and close the538window. Select the configuration in the main PyCharm window and click539the play button next to it to run the server.540541Now that we have a configuration which runs ``flask run`` from within542PyCharm, we can copy that configuration and alter the *Script* argumentMerge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 543why, one step back543to run a different CLI command, e.g. ``flask shell``.