Skip to content
Myr

cli.rst

This file

Path

docs/cli.rst

466 lines · 14.2 KiB

Last changed by

Merge branch '1.0-maintenance'David Lordbefore adoption1fa9185

History of this file

Take it

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

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 1–2why, one step back
1.. currentmodule:: flask
2

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

lines 3–7why, one step back
3.. _cli:
45Command Line Interface6======================7

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 8–11why, one step back
8Installing Flask installs the ``flask`` script, a `Click`_ command line
9interface, 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.

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 12why, one step back
12

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

line 13why, one step back
13.. _Click: http://click.pocoo.org/

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

lines 14–15why, one step back
14
15

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 16–17why, one step back
16Application Discovery
17---------------------

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 18why, one step back
18

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 19–21why, one step back
19The ``flask`` command is installed by Flask, not your application; it must be
20told where to find your application in order to use it. The ``FLASK_APP``21environment variable is used to specify how to load the application.

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 22why, one step back
22

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

line 23why, one step back
23Unix Bash (Linux, Mac, etc.)::

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 24why, one step back
24

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 25–26why, one step back
25 $ export FLASK_APP=hello
26 $ flask run

Merge pull request #2378 from miguelgrinberg/default-flask-appDavid Lordbefore adoption29c055a

line 27why, one step back
27

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

line 28why, one step back
28Windows CMD::

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 29why, one step back
29

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 30–31why, one step back
30 > set FLASK_APP=hello
31 > flask run

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 32why, one step back
32

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

line 33why, one step back
33Windows PowerShell::

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 34why, one step back
34

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 35–36why, one step back
35 > $env:FLASK_APP = "hello"
36 > flask run

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 37why, one step back
37

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 38–39why, one step back
38While ``FLASK_APP`` supports a variety of options for specifying your
39application, most use cases should be simple. Here are the typical values:

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 40why, one step back
40

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 41–44why, one step back
41(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.

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 45why, one step back
45

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 46–48why, one step back
46``FLASK_APP=hello``
47 The name is imported, automatically detecting an app (``app``) or factory48 (``create_app``).

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 49why, one step back
49

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

line 50why, one step back
50----

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 51why, one step back
51

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 52–56why, one step back
52``FLASK_APP`` has three parts: an optional path that sets the current working
53directory, 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:

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 57why, one step back
57

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 58–59why, one step back
58``FLASK_APP=src/hello``
59 Sets the current working directory to ``src`` then imports ``hello``.

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 60why, one step back
60

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 61–62why, one step back
61``FLASK_APP=hello.web``
62 Imports the path ``hello.web``.

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 63why, one step back
63

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 64–65why, one step back
64``FLASK_APP=hello:app2``
65 Uses the ``app2`` Flask instance in ``hello``.

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 66why, one step back
66

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 67–69why, one step back
67``FLASK_APP="hello:create_app('dev')"``
68 The ``create_app`` factory in ``hello`` is called with the string ``'dev'``69 as the argument.

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 70why, one step back
70

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 71–73why, one step back
71If ``FLASK_APP`` is not set, the command will look for a file called
72:file:`wsgi.py` or :file:`app.py` and try to detect an application instance or73factory.

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 74why, one step back
74

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 75–78why, one step back
75Within the given import, the command looks for an application instance named
76``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.

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 79why, one step back
79

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 80–84why, one step back
80When calling an application factory, if the factory takes an argument named
81``info``, then the :class:`~cli.ScriptInfo` instance is passed as a keyword82argument. If parentheses follow the factory name, their contents are parsed83as Python literals and passes as arguments to the function. This means that84strings must still be in quotes.

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 85why, one step back
85

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 86why, one step back
86

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 87–88why, one step back
87Run the Development Server
88--------------------------

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 89why, one step back
89

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 90–91why, one step back
90The :func:`run <cli.run_command>` command will start the development server. It
91replaces the :meth:`Flask.run` method in most cases. ::

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 92why, one step back
92

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 93–95why, one step back
93 $ flask run
94 * Serving Flask app "hello"95 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 96why, one step back
96

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 97–100why, one step back
97.. warning:: Do not use this command to run your application in production.
98 Only use the development server during development. The development server99 is provided for convenience, but is not designed to be particularly secure,100 stable, or efficient. See :ref:`deployment` for how to run in production.

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

lines 101–102why, one step back
101
102

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 103–104why, one step back
103Open a Shell
104------------

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 105why, one step back
105

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 106–108why, one step back
106To explore the data in your application, you can start an interactive Python
107shell with the :func:`shell <cli.shell_command>` command. An application108context will be active, and the app instance will be imported. ::

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 109why, one step back
109

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 110–115why, one step back
110 $ flask shell
111 Python 3.6.2 (default, Jul 20 2017, 03:52:27)112 [GCC 7.1.1 20170630] on linux113 App: example114 Instance: /home/user/Projects/hello/instance115 >>>

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 116why, one step back
116

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

line 117why, one step back
117Use :meth:`~Flask.shell_context_processor` to add other automatic imports.

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 118why, one step back
118

Merge pull request #2593 from pallets/env-docsDavid Lordbefore adoptionc3a9978

line 119why, one step back
119

Add Support for FLASK_ENV (#2570)Armin Ronacherbefore adoption2433522

lines 120–121why, one step back
120Environments
121------------

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 122why, one step back
122

Add Support for FLASK_ENV (#2570)Armin Ronacherbefore adoption2433522

lines 123–124why, one step back
123.. versionadded:: 1.0
124

Merge pull request #2593 from pallets/env-docsDavid Lordbefore adoptionc3a9978

lines 125–129why, one step back
125The environment in which the Flask app runs is set by the
126:envvar:`FLASK_ENV` environment variable. If not set it defaults to127``production``. The other recognized environment is ``development``.128Flask and extensions may choose to enable behaviors based on the129environment.

Add Support for FLASK_ENV (#2570)Armin Ronacherbefore adoption2433522

line 130why, one step back
130

Merge pull request #2593 from pallets/env-docsDavid Lordbefore adoptionc3a9978

lines 131–133why, one step back
131If the env is set to ``development``, the ``flask`` command will enable
132debug mode and ``flask run`` will enable the interactive debugger and133reloader.

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 134why, one step back
134

Add Support for FLASK_ENV (#2570)Armin Ronacherbefore adoption2433522

lines 135–136why, one step back
135::
136

Merge pull request #2593 from pallets/env-docsDavid Lordbefore adoptionc3a9978

line 137why, one step back
137 $ FLASK_ENV=development flask run

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

line 138why, one step back
138 * Serving Flask app "hello"

Merge pull request #2593 from pallets/env-docsDavid Lordbefore adoptionc3a9978

lines 139–140why, one step back
139 * Environment: development
140 * Debug mode: on

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 141–144why, one step back
141 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
142 * Restarting with inotify reloader143 * Debugger is active!144 * Debugger PIN: 223-456-919

Merge pull request #2593 from pallets/env-docsDavid Lordbefore adoptionc3a9978

lines 145–152why, one step back
145
146147Debug Mode148----------149150Debug mode will be enabled when :envvar:`FLASK_ENV` is ``development``,151as described above. If you want to control debug mode separately, use152:envvar:`FLASK_DEBUG`. The value ``1`` enables it, ``0`` disables it.

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

lines 153–154why, one step back
153
154

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

lines 155–156why, one step back
155.. _dotenv:
156

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 157–158why, one step back
157Environment Variables From dotenv
158---------------------------------

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

line 159why, one step back
159

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 160–163why, one step back
160Rather than setting ``FLASK_APP`` each time you open a new terminal, you can
161use Flask's dotenv support to set environment variables automatically.162163If `python-dotenv`_ is installed, running the ``flask`` command will set

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

lines 164–173why, one step back
164environment variables defined in the files :file:`.env` and :file:`.flaskenv`.
165This can be used to avoid having to set ``FLASK_APP`` manually every time you166open a new terminal, and to set configuration using environment variables167similar to how some deployment services work.168169Variables set on the command line are used over those set in :file:`.env`,170which are used over those set in :file:`.flaskenv`. :file:`.flaskenv` should be171used for public variables, such as ``FLASK_APP``, while :file:`.env` should not172be committed to your repository so that it can set private variables.173

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

line 174why, one step back
174Directories are scanned upwards from the directory you call ``flask``

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

lines 175–178why, one step back
175from to locate the files. The current working directory will be set to the
176location of the file, with the assumption that that is the top level project177directory.178

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 179–181why, one step back
179The files are only loaded by the ``flask`` command or calling
180:meth:`~Flask.run`. If you would like to load these files when running in181production, you should call :func:`~cli.load_dotenv` manually.

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

lines 182–184why, one step back
182
183.. _python-dotenv: https://github.com/theskumar/python-dotenv#readme184

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 185why, one step back
185

document cli option env varsDavid Lordbefore adoption93698b5

lines 186–203why, one step back
186Setting Command Options
187~~~~~~~~~~~~~~~~~~~~~~~188189Click is configured to load default values for command options from190environment variables. The variables use the pattern191``FLASK_COMMAND_OPTION``. For example, to set the port for the run192command, instead of ``flask run --port 8000``:193194.. code-block:: none195196 export FLASK_RUN_PORT=8000197 flask run198 * Running on http://127.0.0.1:8000/199200These can be added to the ``.flaskenv`` file just like ``FLASK_APP`` to201control default command options.202203

Merge branch '1.0-maintenance'David Lordbefore adoption1fa9185

lines 204–227why, one step back
204Disable dotenv
205~~~~~~~~~~~~~~206207The ``flask`` command will show a message if it detects dotenv files but208python-dotenv is not installed.209210.. code-block:: none211212 flask run213 * Tip: There are .env files present. Do "pip install python-dotenv" to use them.214215You can tell Flask not to load dotenv files even when python-dotenv is216installed by setting the ``FLASK_SKIP_DOTENV`` environment variable.217This can be useful if you want to load them manually, or if you're using218a project runner that loads them already. Keep in mind that the219environment variables must be set before the app loads or it won't220configure as expected.221222.. code-block:: none223224 export FLASK_SKIP_DOTENV=1225 flask run226227

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 228–229why, one step back
228Environment Variables From virtualenv
229-------------------------------------

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 230why, one step back
230

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 231–233why, one step back
231If you do not want to install dotenv support, you can still set environment
232variables by adding them to the end of the virtualenv's :file:`activate`233script. Activating the virtualenv will set the variables.

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 234why, one step back
234

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

line 235why, one step back
235Unix Bash, :file:`venv/bin/activate`::

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 236why, one step back
236

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

line 237why, one step back
237 export FLASK_APP=hello

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 238why, one step back
238

Merge pull request #2553 from greyli/patch-6David Lordbefore adoptiondb56e38

line 239why, one step back
239Windows CMD, :file:`venv\\Scripts\\activate.bat`::

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 240why, one step back
240

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

line 241why, one step back
241 set FLASK_APP=hello

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 242why, one step back
242

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 243–245why, one step back
243It is preferred to use dotenv support over this, since :file:`.flaskenv` can be
244committed to the repository so that it works automatically wherever the project245is checked out.

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

lines 246–247why, one step back
246
247

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 248–249why, one step back
248Custom Commands
249---------------

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 250why, one step back
250

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 251–252why, one step back
251The ``flask`` command is implemented using `Click`_. See that project's
252documentation for full information about writing commands.

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 253why, one step back
253

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 254–255why, one step back
254This example adds the command ``create_user`` that takes the argument
255``name``. ::

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 256why, one step back
256

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 257–258why, one step back
257 import click
258 from flask import Flask

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 259why, one step back
259

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

line 260why, one step back
260 app = Flask(__name__)

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 261why, one step back
261

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 262–265why, one step back
262 @app.cli.command()
263 @click.argument('name')264 def create_user(name):265 ...

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 266why, one step back
266

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 267–273why, one step back
267::
268269 flask create_user admin270271This example adds the same command, but as ``user create``, a command in a272group. This is useful if you want to organize multiple related commands. ::273

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 274why, one step back
274 import click

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 275–276why, one step back
275 from flask import Flask
276 from flask.cli import AppGroup

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 277why, one step back
277

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 278–279why, one step back
278 app = Flask(__name__)
279 user_cli = AppGroup('user')

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 280why, one step back
280

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 281–284why, one step back
281 @user_cli.command('create')
282 @click.argument('name')283 def create_user(name):284 ...

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 285why, one step back
285

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

line 286why, one step back
286 app.cli.add_command(user_cli)

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 287why, one step back
287

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

line 288why, one step back
288::

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 289why, one step back
289

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

line 290why, one step back
290 flask user create demo

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 291why, one step back
291

Merge pull request #2611 from pallets/cli-test-docsDavid Lordbefore adoption8c2c802

lines 292–294why, one step back
292See :ref:`testing-cli` for an overview of how to test your custom
293commands.294

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 295why, one step back
295

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 296–297why, one step back
296Application Context
297~~~~~~~~~~~~~~~~~~~

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 298why, one step back
298

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 299–304why, one step back
299Commands added using the Flask app's :attr:`~Flask.cli`
300:meth:`~cli.AppGroup.command` decorator will be executed with an application301context pushed, so your command and extensions have access to the app and its302configuration. If you create a command using the Click :func:`~click.command`303decorator instead of the Flask decorator, you can use304:func:`~cli.with_appcontext` to get the same behavior. ::

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 305why, one step back
305

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 306–307why, one step back
306 import click
307 from flask.cli import with_appcontext

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 308why, one step back
308

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 309–329why, one step back
309 @click.command
310 @with_appcontext311 def do_work():312 ...313314 app.cli.add_command(do_work)315316If you're sure a command doesn't need the context, you can disable it::317318 @app.cli.command(with_appcontext=False)319 def do_work():320 ...321322323Plugins324-------325326Flask will automatically load commands specified in the ``flask.commands``327`entry point`_. This is useful for extensions that want to add commands when328they are installed. Entry points are specified in :file:`setup.py` ::329

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 331why, one step back
331

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 332–333why, one step back
332 setup(
333 name='flask-my-extension',

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 334–339why, one step back
334 ...,
335 entry_points={336 'flask.commands': [337 'my-command=flask_my_extension.commands:cli'338 ],339 },

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 340why, one step back
340 )

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 341why, one step back
341

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 342why, one step back
342

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 343–347why, one step back
343.. _entry point: https://packaging.python.org/tutorials/distributing-packages/#entry-points
344345Inside :file:`flask_my_extension/commands.py` you can then export a Click346object::347

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 348–351why, one step back
348 import click
349350 @click.command()351 def cli():

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

line 352why, one step back
352 ...

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 353why, one step back
353

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 354–355why, one step back
354Once that package is installed in the same virtualenv as your Flask project,
355you can run ``flask my-command`` to invoke the command.

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 356why, one step back
356

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 357–402why, one step back
357
358.. _custom-scripts:359360Custom Scripts361--------------362363When you are using the app factory pattern, it may be more convenient to define364your own Click script. Instead of using ``FLASK_APP`` and letting Flask load365your application, you can create your own Click object and export it as a366`console script`_ entry point.367368Create an instance of :class:`~cli.FlaskGroup` and pass it the factory::369370 import click371 from flask import Flask372 from flask.cli import FlaskGroup373374 def create_app():375 app = Flask('wiki')376 # other setup377 return app378379 @click.group(cls=FlaskGroup, create_app=create_app)380 def cli():381 """Management script for the Wiki application."""382383Define the entry point in :file:`setup.py`::384385 from setuptools import setup386387 setup(388 name='flask-my-extension',389 ...,390 entry_points={391 'console_scripts': [392 'wiki=wiki:cli'393 ],394 },395 )396397Install the application in the virtualenv in editable mode and the custom398script is available. Note that you don't need to set ``FLASK_APP``. ::399400 $ pip install -e .401 $ wiki run402

Merge pull request #2610 from pallets/cli-syntaxerrorDavid Lordbefore adoption94aaa24

lines 403–411why, one step back
403.. admonition:: Errors in Custom Scripts
404405 When using a custom script, if you introduce an error in your406 module-level code, the reloader will fail because it can no longer407 load the entry point.408409 The ``flask`` command, being separate from your code, does not have410 this issue and is recommended in most cases.411

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

lines 412–414why, one step back
412.. _console script: https://packaging.python.org/tutorials/distributing-packages/#console-scripts
413414

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 415–417why, one step back
415PyCharm Integration
416-------------------417

Merge pull request #2610 from pallets/cli-syntaxerrorDavid Lordbefore adoption94aaa24

lines 418–421why, one step back
418Prior to PyCharm 2018.1, the Flask CLI features weren't yet fully
419integrated into PyCharm. We have to do a few tweaks to get them working420smoothly. These instructions should be similar for any other IDE you421might want to use.

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 422why, one step back
422

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

lines 423–425why, one step back
423In PyCharm, with your project open, click on *Run* from the menu bar and
424go to *Edit Configurations*. You'll be greeted by a screen similar to425this:

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 426–427why, one step back
426
427.. image:: _static/pycharm-runconfig.png

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

lines 428–430why, one step back
428 :align: center
429 :class: screenshot430 :alt: screenshot of pycharm's run configuration settings

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 431why, one step back
431

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

lines 432–435why, one step back
432There's quite a few options to change, but once we've done it for one
433command, we can easily copy the entire configuration and make a single434tweak to give us access to other commands, including any custom ones you435may implement yourself.

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 436why, one step back
436

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

lines 437–440why, one step back
437Click the + (*Add New Configuration*) button and select *Python*. Give
438the configuration a good descriptive name such as "Run Flask Server".439For the ``flask run`` command, check "Single instance only" since you440can't run the server more than once at the same time.

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 441why, one step back
441

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

line 442why, one step back
442Select *Module name* from the dropdown (**A**) then input ``flask``.

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 443why, one step back
443

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

lines 444–446why, one step back
444The *Parameters* field (**B**) is set to the CLI command to execute
445(with any arguments). In this example we use ``run``, which will run446the development server.

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 447why, one step back
447

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

lines 448–451why, one step back
448You can skip this next step if you're using :ref:`dotenv`. We need to
449add an environment variable (**C**) to identify our application. Click450on the browse button and add an entry with ``FLASK_APP`` on the left and451the Python import or file on the right (``hello`` for example).

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 452why, one step back
452

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

lines 453–454why, one step back
453Next we need to set the working directory (**D**) to be the folder where
454our application resides.

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 455why, one step back
455

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

lines 456–465why, one step back
456If you have installed your project as a package in your virtualenv, you
457may untick the *PYTHONPATH* options (**E**). This will more accurately458match how you deploy the app later.459460Click *Apply* to save the configuration, or *OK* to save and close the461window. Select the configuration in the main PyCharm window and click462the play button next to it to run the server.463464Now that we have a configuration which runs ``flask run`` from within465PyCharm, we can copy that configuration and alter the *Script* argument

Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f

line 466why, one step back
466to run a different CLI command, e.g. ``flask shell``.