Skip to content
Myr

cli.rst

This file

Path

docs/cli.rst

383 lines · 12.0 KiB

Last changed by

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

History of this file

Take it

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

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

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

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

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.

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

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/

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

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

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

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.

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

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

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

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

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

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

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

line 32why, one step back
32

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

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

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

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

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

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:

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

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.

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

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

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

line 49why, one step back
49

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

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

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

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:

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

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.

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

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.

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

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

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

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

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

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)

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

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.

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

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

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

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

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

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

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

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.

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

lines 118–119why, one step back
118
119

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

lines 120–121why, one step back
120Debug Mode
121----------

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

line 122why, one step back
122

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

lines 123–126why, one step back
123Set the :envvar:`FLASK_DEBUG` environment variable to override the
124application's :attr:`~Flask.debug` flag. The value ``1`` enables it, ``0``125disables it. Forcing the debug flag on also enables the debugger and reloader126when running the development server. ::

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

line 127why, one step back
127

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

lines 128–134why, one step back
128 $ FLASK_DEBUG=1 flask run
129 * Serving Flask app "hello"130 * Forcing debug mode on131 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)132 * Restarting with inotify reloader133 * Debugger is active!134 * Debugger PIN: 223-456-919

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

lines 135–136why, one step back
135
136

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

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

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

lines 139–140why, one step back
139Environment Variables From dotenv
140---------------------------------

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

line 141why, one step back
141

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

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

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

lines 146–155why, one step back
146environment variables defined in the files :file:`.env` and :file:`.flaskenv`.
147This can be used to avoid having to set ``FLASK_APP`` manually every time you148open a new terminal, and to set configuration using environment variables149similar to how some deployment services work.150151Variables set on the command line are used over those set in :file:`.env`,152which are used over those set in :file:`.flaskenv`. :file:`.flaskenv` should be153used for public variables, such as ``FLASK_APP``, while :file:`.env` should not154be committed to your repository so that it can set private variables.155

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

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

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

lines 157–160why, one step back
157from to locate the files. The current working directory will be set to the
158location of the file, with the assumption that that is the top level project159directory.160

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

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

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

lines 164–166why, one step back
164
165.. _python-dotenv: https://github.com/theskumar/python-dotenv#readme166

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

line 167why, one step back
167

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

lines 168–169why, one step back
168Environment Variables From virtualenv
169-------------------------------------

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

line 170why, one step back
170

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

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

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

line 174why, one step back
174

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

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

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

line 176why, one step back
176

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

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

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

line 178why, one step back
178

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

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

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

line 180why, one step back
180

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

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

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

line 182why, one step back
182

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

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

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

lines 186–187why, one step back
186
187

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

lines 188–189why, one step back
188Custom Commands
189---------------

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

line 190why, one step back
190

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

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

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

line 193why, one step back
193

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

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

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

line 196why, one step back
196

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

lines 197–198why, one step back
197 import click
198 from flask import Flask

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

line 199why, one step back
199

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

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

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

line 201why, one step back
201

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

lines 202–205why, one step back
202 @app.cli.command()
203 @click.argument('name')204 def create_user(name):205 ...

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

line 206why, one step back
206

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

lines 207–213why, one step back
207::
208209 flask create_user admin210211This example adds the same command, but as ``user create``, a command in a212group. This is useful if you want to organize multiple related commands. ::213

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

line 214why, one step back
214 import click

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

lines 215–216why, one step back
215 from flask import Flask
216 from flask.cli import AppGroup

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

line 217why, one step back
217

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

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

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

line 220why, one step back
220

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

lines 221–224why, one step back
221 @user_cli.command('create')
222 @click.argument('name')223 def create_user(name):224 ...

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

line 225why, one step back
225

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

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

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

line 227why, one step back
227

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

line 228why, one step back
228::

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

line 229why, one step back
229

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

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

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

lines 231–232why, one step back
231
232

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

lines 233–234why, one step back
233Application Context
234~~~~~~~~~~~~~~~~~~~

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

line 235why, one step back
235

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

lines 236–241why, one step back
236Commands added using the Flask app's :attr:`~Flask.cli`
237:meth:`~cli.AppGroup.command` decorator will be executed with an application238context pushed, so your command and extensions have access to the app and its239configuration. If you create a command using the Click :func:`~click.command`240decorator instead of the Flask decorator, you can use241:func:`~cli.with_appcontext` to get the same behavior. ::

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

line 242why, one step back
242

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

lines 243–244why, one step back
243 import click
244 from flask.cli import with_appcontext

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

line 245why, one step back
245

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

lines 246–266why, one step back
246 @click.command
247 @with_appcontext248 def do_work():249 ...250251 app.cli.add_command(do_work)252253If you're sure a command doesn't need the context, you can disable it::254255 @app.cli.command(with_appcontext=False)256 def do_work():257 ...258259260Plugins261-------262263Flask will automatically load commands specified in the ``flask.commands``264`entry point`_. This is useful for extensions that want to add commands when265they are installed. Entry points are specified in :file:`setup.py` ::266

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

line 268why, one step back
268

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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

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

lines 271–276why, one step back
271 ...,
272 entry_points={273 'flask.commands': [274 'my-command=flask_my_extension.commands:cli'275 ],276 },

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 277why, one step back
277 )

Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139

line 278why, one step back
278

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 279why, one step back
279

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

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 285–288why, one step back
285 import click
286287 @click.command()288 def cli():

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

line 289why, one step back
289 ...

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 290why, one step back
290

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

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 293why, one step back
293

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

lines 294–342why, one step back
294
295.. _custom-scripts:296297Custom Scripts298--------------299300When you are using the app factory pattern, it may be more convenient to define301your own Click script. Instead of using ``FLASK_APP`` and letting Flask load302your application, you can create your own Click object and export it as a303`console script`_ entry point.304305Create an instance of :class:`~cli.FlaskGroup` and pass it the factory::306307 import click308 from flask import Flask309 from flask.cli import FlaskGroup310311 def create_app():312 app = Flask('wiki')313 # other setup314 return app315316 @click.group(cls=FlaskGroup, create_app=create_app)317 def cli():318 """Management script for the Wiki application."""319320Define the entry point in :file:`setup.py`::321322 from setuptools import setup323324 setup(325 name='flask-my-extension',326 ...,327 entry_points={328 'console_scripts': [329 'wiki=wiki:cli'330 ],331 },332 )333334Install the application in the virtualenv in editable mode and the custom335script is available. Note that you don't need to set ``FLASK_APP``. ::336337 $ pip install -e .338 $ wiki run339340.. _console script: https://packaging.python.org/tutorials/distributing-packages/#console-scripts341342

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 343–345why, one step back
343PyCharm Integration
344-------------------345

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

lines 346–348why, one step back
346The new Flask CLI features aren't yet fully integrated into the PyCharm IDE,
347so we have to do a few tweaks to get them working smoothly. These instructions348should be similar for any other IDE you might want to use.

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 349why, one step back
349

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

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 352–357why, one step back
352
353.. image:: _static/pycharm-runconfig.png354 :align: center355 :class: screenshot356 :alt: screenshot of pycharm's run configuration settings357

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

lines 358–360why, one step back
358There's quite a few options to change, but once we've done it for one command,
359we can easily copy the entire configuration and make a single tweak to give us360access to other commands, including any custom ones you may implement yourself.

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 361why, one step back
361

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

lines 362–364why, one step back
362For the *Script* input (**A**), navigate to your project's virtual environment.
363Within that folder, pick the ``flask`` executable which will reside in the364``bin`` folder, or in the ``Scripts`` on Windows.

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 365why, one step back
365

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

lines 366–367why, one step back
366The *Script Parameter* field (**B**) is set to the CLI command you to execute.
367In this example we use ``run``, which will run the development server.

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 368why, one step back
368

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

lines 369–372why, one step back
369You can skip this next step if you're using :ref:`dotenv`. We need to add an
370environment variable (**C**) to identify our application. Click on the browse371button and add an entry with ``FLASK_APP`` on the left and the name of the372Python file or package on the right (``app.py`` for example).

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 373–374why, one step back
373
374Next we need to set the working directory (**D**) to be the same folder where

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

lines 375–376why, one step back
375our application file or package resides. PyCharm changed it to the directory
376with the ``flask`` executable when we selected it earlier, which is incorrect.

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 377–378why, one step back
377
378Finally, untick the *PYTHONPATH* options (**E**) and give the configuration a

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

line 379why, one step back
379good descriptive name, such as "Run Flask Server", and click *Apply*.

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 380why, one step back
380

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

lines 381–383why, one step back
381Now that we have a configuration which runs ``flask run`` from within PyCharm,
382we can simply copy that configuration and alter the *Script* argument383to run a different CLI command, e.g. ``flask shell``.