Skip to content
Myr

cli.rst

This file

Path

docs/cli.rst

468 lines · 14.4 KiB

Last changed by

Merge pull request #2915 from loomchild/masterDavid Lordbefore adoptionc92001d

History of this file

Take it

The walk stopped after 200 commits: lines older than 62406e6 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 #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

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 #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

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 #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

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 #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

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 #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

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 #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

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 #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

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 #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

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 #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

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 #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

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 #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

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 #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

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 #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

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 #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

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 #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

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``.

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

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``.

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

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``.

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

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.

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

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.

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

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 #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 79why, one step back
79

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

line 80why, one step back
80When calling an application factory, if the factory takes an argument named

Merge pull request #2870 from jerr0328/docs-update-cli-scriptinfoDavid Lordbefore adoption93e4b71

lines 81–86why, one step back
81``script_info``, then the :class:`~cli.ScriptInfo` instance is passed as a
82keyword 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.

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

lines 87–88why, one step back
87
88

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

lines 89–90why, one step back
89Run the Development Server
90--------------------------

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 91why, one step back
91

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 94why, one step back
94

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 98why, one step back
98

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

lines 99–102why, one step back
99.. 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.

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

lines 103–104why, one step back
103
104

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

lines 105–106why, one step back
105Open a Shell
106------------

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 107why, one step back
107

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 111why, one step back
111

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

lines 112–117why, one step back
112 $ flask shell
113 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 >>>

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 118why, one step back
118

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 120why, one step back
120

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

line 121why, one step back
121

Add Support for FLASK_ENV (#2570)Armin Ronacherbefore adoption2433522

lines 122–123why, one step back
122Environments
123------------

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 124why, one step back
124

Add Support for FLASK_ENV (#2570)Armin Ronacherbefore adoption2433522

lines 125–126why, one step back
125.. versionadded:: 1.0
126

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

lines 127–131why, one step back
127The environment in which the Flask app runs is set by the
128: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 back
132

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 136why, one step back
136

Add Support for FLASK_ENV (#2570)Armin Ronacherbefore adoption2433522

lines 137–138why, one step back
137::
138

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

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

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

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

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

lines 141–142why, one step back
141 * Environment: development
142 * Debug mode: on

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

lines 143–146why, one step back
143 * 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-919

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

lines 147–154why, one step back
147
148149Debug Mode150----------151152Debug mode will be enabled when :envvar:`FLASK_ENV` is ``development``,153as described above. If you want to control debug mode separately, use154:envvar:`FLASK_DEBUG`. The value ``1`` enables it, ``0`` disables it.

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

lines 155–156why, one step back
155
156

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

lines 157–158why, one step back
157.. _dotenv:
158

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

lines 159–160why, one step back
159Environment Variables From dotenv
160---------------------------------

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

line 161why, one step back
161

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

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

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

lines 166–175why, one step back
166environment variables defined in the files :file:`.env` and :file:`.flaskenv`.
167This can be used to avoid having to set ``FLASK_APP`` manually every time you168open a new terminal, and to set configuration using environment variables169similar to how some deployment services work.170171Variables set on the command line are used over those set in :file:`.env`,172which are used over those set in :file:`.flaskenv`. :file:`.flaskenv` should be173used for public variables, such as ``FLASK_APP``, while :file:`.env` should not174be committed to your repository so that it can set private variables.175

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

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

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

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

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

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

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

lines 184–186why, one step back
184
185.. _python-dotenv: https://github.com/theskumar/python-dotenv#readme186

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 187why, one step back
187

document cli option env varsDavid Lordbefore adoption93698b5

lines 188–197why, one step back
188Setting Command Options
189~~~~~~~~~~~~~~~~~~~~~~~190191Click is configured to load default values for command options from192environment variables. The variables use the pattern193``FLASK_COMMAND_OPTION``. For example, to set the port for the run194command, instead of ``flask run --port 8000``:195196.. code-block:: none197

Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6

lines 198–199why, one step back
198 $ export FLASK_RUN_PORT=8000
199 $ flask run

document cli option env varsDavid Lordbefore adoption93698b5

lines 200–205why, one step back
200 * Running on http://127.0.0.1:8000/
201202These can be added to the ``.flaskenv`` file just like ``FLASK_APP`` to203control default command options.204205

Merge branch '1.0-maintenance'David Lordbefore adoption1fa9185

lines 206–213why, one step back
206Disable dotenv
207~~~~~~~~~~~~~~208209The ``flask`` command will show a message if it detects dotenv files but210python-dotenv is not installed.211212.. code-block:: none213

Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6

line 214why, one step back
214 $ flask run

Merge branch '1.0-maintenance'David Lordbefore adoption1fa9185

lines 215–225why, one step back
215 * Tip: There are .env files present. Do "pip install python-dotenv" to use them.
216217You can tell Flask not to load dotenv files even when python-dotenv is218installed by setting the ``FLASK_SKIP_DOTENV`` environment variable.219This can be useful if you want to load them manually, or if you're using220a project runner that loads them already. Keep in mind that the221environment variables must be set before the app loads or it won't222configure as expected.223224.. code-block:: none225

Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6

lines 226–227why, one step back
226 $ export FLASK_SKIP_DOTENV=1
227 $ flask run

Merge branch '1.0-maintenance'David Lordbefore adoption1fa9185

lines 228–229why, one step back
228
229

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

lines 230–231why, one step back
230Environment Variables From virtualenv
231-------------------------------------

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 232why, one step back
232

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 236why, one step back
236

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 238why, one step back
238

Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6

line 239why, one step back
239 $ export FLASK_APP=hello

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 240why, one step back
240

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 242why, one step back
242

Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6

line 243why, one step back
243 > set FLASK_APP=hello

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 244why, one step back
244

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

lines 248–249why, one step back
248
249

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

lines 250–251why, one step back
250Custom Commands
251---------------

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 252why, one step back
252

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 255why, one step back
255

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 258why, one step back
258

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

lines 259–260why, one step back
259 import click
260 from flask import Flask

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 261why, one step back
261

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 263why, one step back
263

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 268why, one step back
268

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

lines 269–270why, one step back
269::
270

Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6

line 271why, one step back
271 $ flask create_user admin

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

lines 272–275why, one step back
272
273This example adds the same command, but as ``user create``, a command in a274group. This is useful if you want to organize multiple related commands. ::275

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 276why, one step back
276 import click

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

lines 277–278why, one step back
277 from flask import Flask
278 from flask.cli import AppGroup

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 279why, one step back
279

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 282why, one step back
282

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 287why, one step back
287

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 289why, one step back
289

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

line 290why, one step back
290::

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 291why, one step back
291

Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6

line 292why, one step back
292 $ flask user create demo

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 293why, one step back
293

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 297why, one step back
297

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

lines 298–299why, one step back
298Application Context
299~~~~~~~~~~~~~~~~~~~

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 300why, one step back
300

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

lines 301–306why, one step back
301Commands added using the Flask app's :attr:`~Flask.cli`
302:meth:`~cli.AppGroup.command` decorator will be executed with an application303context pushed, so your command and extensions have access to the app and its304configuration. If you create a command using the Click :func:`~click.command`305decorator instead of the Flask decorator, you can use306:func:`~cli.with_appcontext` to get the same behavior. ::

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 307why, one step back
307

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

lines 308–309why, one step back
308 import click
309 from flask.cli import with_appcontext

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 310why, one step back
310

Merge pull request #2915 from loomchild/masterDavid Lordbefore adoptionc92001d

line 311why, one step back
311 @click.command()

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

lines 312–331why, one step back
312 @with_appcontext
313 def do_work():314 ...315316 app.cli.add_command(do_work)317318If you're sure a command doesn't need the context, you can disable it::319320 @app.cli.command(with_appcontext=False)321 def do_work():322 ...323324325Plugins326-------327328Flask will automatically load commands specified in the ``flask.commands``329`entry point`_. This is useful for extensions that want to add commands when330they are installed. Entry points are specified in :file:`setup.py` ::331

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

lines 332–335why, one step back
332 from setuptools import setup
333334 setup(335 name='flask-my-extension',

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

lines 342–344why, one step back
342 )
343344

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

lines 350–353why, one step back
350 import click
351352 @click.command()353 def cli():

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

line 354why, one step back
354 ...

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 355why, one step back
355

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 358why, one step back
358

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

lines 359–404why, one step back
359
360.. _custom-scripts:361362Custom Scripts363--------------364365When you are using the app factory pattern, it may be more convenient to define366your own Click script. Instead of using ``FLASK_APP`` and letting Flask load367your application, you can create your own Click object and export it as a368`console script`_ entry point.369370Create an instance of :class:`~cli.FlaskGroup` and pass it the factory::371372 import click373 from flask import Flask374 from flask.cli import FlaskGroup375376 def create_app():377 app = Flask('wiki')378 # other setup379 return app380381 @click.group(cls=FlaskGroup, create_app=create_app)382 def cli():383 """Management script for the Wiki application."""384385Define the entry point in :file:`setup.py`::386387 from setuptools import setup388389 setup(390 name='flask-my-extension',391 ...,392 entry_points={393 'console_scripts': [394 'wiki=wiki:cli'395 ],396 },397 )398399Install the application in the virtualenv in editable mode and the custom400script is available. Note that you don't need to set ``FLASK_APP``. ::401402 $ pip install -e .403 $ wiki run404

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

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

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

lines 417–419why, one step back
417PyCharm Integration
418-------------------419

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 424why, one step back
424

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

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

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

lines 430–432why, one step back
430 :align: center
431 :class: screenshot432 :alt: screenshot of pycharm's run configuration settings

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 433why, one step back
433

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 438why, one step back
438

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 443why, one step back
443

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 445why, one step back
445

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 449why, one step back
449

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 454why, one step back
454

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

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

Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6

line 457why, one step back
457

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

lines 458–467why, one step back
458If you have installed your project as a package in your virtualenv, you
459may untick the *PYTHONPATH* options (**E**). This will more accurately460match how you deploy the app later.461462Click *Apply* to save the configuration, or *OK* to save and close the463window. Select the configuration in the main PyCharm window and click464the play button next to it to run the server.465466Now that we have a configuration which runs ``flask run`` from within467PyCharm, we can copy that configuration and alter the *Script* argument

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

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