Skip to content
Myr

cli.rst

This file

Path

docs/cli.rst

424 lines · 13.0 KiB

Last changed by

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

History of this file

Take it

The walk stopped after 200 commits: lines older than 818c42c point at it.

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

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

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

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.

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

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/

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

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

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

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.

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

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.)::

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

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::

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

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

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

line 32why, one step back
32

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

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

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

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

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

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:

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

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.

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

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

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

line 49why, one step back
49

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

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

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

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:

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

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.

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

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.

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

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

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

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

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

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)

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

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.

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

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

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

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

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

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

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

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.

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

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

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

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.

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

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.

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

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

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

line 185why, one step back
185

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

lines 186–187why, one step back
186Environment Variables From virtualenv
187-------------------------------------

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

line 188why, one step back
188

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

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

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

line 192why, one step back
192

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

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

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

line 194why, one step back
194

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

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

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

line 196why, one step back
196

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

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

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

line 198why, one step back
198

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

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

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

line 200why, one step back
200

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

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

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

lines 204–205why, one step back
204
205

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

lines 206–207why, one step back
206Custom Commands
207---------------

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

line 208why, one step back
208

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

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

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

line 211why, one step back
211

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

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

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

line 214why, one step back
214

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

lines 215–216why, one step back
215 import click
216 from flask import Flask

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

line 217why, one step back
217

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

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

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

line 219why, one step back
219

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

lines 220–223why, one step back
220 @app.cli.command()
221 @click.argument('name')222 def create_user(name):223 ...

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

line 224why, one step back
224

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

lines 225–231why, one step back
225::
226227 flask create_user admin228229This example adds the same command, but as ``user create``, a command in a230group. This is useful if you want to organize multiple related commands. ::231

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

line 232why, one step back
232 import click

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

lines 233–234why, one step back
233 from flask import Flask
234 from flask.cli import AppGroup

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

line 235why, one step back
235

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

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

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

line 238why, one step back
238

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

lines 239–242why, one step back
239 @user_cli.command('create')
240 @click.argument('name')241 def create_user(name):242 ...

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

line 243why, one step back
243

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

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

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

line 245why, one step back
245

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

line 246why, one step back
246::

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

line 247why, one step back
247

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

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

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

line 249why, one step back
249

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

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

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

line 253why, one step back
253

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

lines 254–255why, one step back
254Application Context
255~~~~~~~~~~~~~~~~~~~

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

line 256why, one step back
256

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

lines 257–262why, one step back
257Commands added using the Flask app's :attr:`~Flask.cli`
258:meth:`~cli.AppGroup.command` decorator will be executed with an application259context pushed, so your command and extensions have access to the app and its260configuration. If you create a command using the Click :func:`~click.command`261decorator instead of the Flask decorator, you can use262:func:`~cli.with_appcontext` to get the same behavior. ::

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

line 263why, one step back
263

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

lines 264–265why, one step back
264 import click
265 from flask.cli import with_appcontext

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

line 266why, one step back
266

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

lines 267–287why, one step back
267 @click.command
268 @with_appcontext269 def do_work():270 ...271272 app.cli.add_command(do_work)273274If you're sure a command doesn't need the context, you can disable it::275276 @app.cli.command(with_appcontext=False)277 def do_work():278 ...279280281Plugins282-------283284Flask will automatically load commands specified in the ``flask.commands``285`entry point`_. This is useful for extensions that want to add commands when286they are installed. Entry points are specified in :file:`setup.py` ::287

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

line 289why, one step back
289

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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

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

lines 292–297why, one step back
292 ...,
293 entry_points={294 'flask.commands': [295 'my-command=flask_my_extension.commands:cli'296 ],297 },

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 298why, one step back
298 )

Add @keyanp to authors, sort listMarkus Unterwaditzerbefore adoption818c42c

line 299why, one step back
299

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 300why, one step back
300

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

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 306–309why, one step back
306 import click
307308 @click.command()309 def cli():

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

line 310why, one step back
310 ...

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 311why, one step back
311

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

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 314why, one step back
314

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

lines 315–360why, one step back
315
316.. _custom-scripts:317318Custom Scripts319--------------320321When you are using the app factory pattern, it may be more convenient to define322your own Click script. Instead of using ``FLASK_APP`` and letting Flask load323your application, you can create your own Click object and export it as a324`console script`_ entry point.325326Create an instance of :class:`~cli.FlaskGroup` and pass it the factory::327328 import click329 from flask import Flask330 from flask.cli import FlaskGroup331332 def create_app():333 app = Flask('wiki')334 # other setup335 return app336337 @click.group(cls=FlaskGroup, create_app=create_app)338 def cli():339 """Management script for the Wiki application."""340341Define the entry point in :file:`setup.py`::342343 from setuptools import setup344345 setup(346 name='flask-my-extension',347 ...,348 entry_points={349 'console_scripts': [350 'wiki=wiki:cli'351 ],352 },353 )354355Install the application in the virtualenv in editable mode and the custom356script is available. Note that you don't need to set ``FLASK_APP``. ::357358 $ pip install -e .359 $ wiki run360

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

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

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

lines 370–372why, one step back
370.. _console script: https://packaging.python.org/tutorials/distributing-packages/#console-scripts
371372

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 373–375why, one step back
373PyCharm Integration
374-------------------375

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

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 380why, one step back
380

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

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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

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

lines 386–388why, one step back
386 :align: center
387 :class: screenshot388 :alt: screenshot of pycharm's run configuration settings

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 389why, one step back
389

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

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 394why, one step back
394

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

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 399why, one step back
399

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

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 401why, one step back
401

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

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 405why, one step back
405

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

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 410why, one step back
410

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

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 413why, one step back
413

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

lines 414–423why, one step back
414If you have installed your project as a package in your virtualenv, you
415may untick the *PYTHONPATH* options (**E**). This will more accurately416match how you deploy the app later.417418Click *Apply* to save the configuration, or *OK* to save and close the419window. Select the configuration in the main PyCharm window and click420the play button next to it to run the server.421422Now that we have a configuration which runs ``flask run`` from within423PyCharm, we can copy that configuration and alter the *Script* argument

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

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