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 fileTake 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 back1.. currentmodule:: flask2Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
lines 3–7why, one step back3.. _cli:45Command Line Interface6======================7Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 8–11why, one step back8Installing Flask installs the ``flask`` script, a `Click`_ command line9interface, in your virtualenv. Executed from the terminal, this script gives10access to built-in, extension, and application-defined commands. The ``--help``11option will give more information about any commands and options.Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 12why, one step back12Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 13why, one step back13.. _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 back1415Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 16–17why, one step back16Application Discovery17---------------------Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 18why, one step back18Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 19–21why, one step back19The ``flask`` command is installed by Flask, not your application; it must be20told where to find your application in order to use it. The ``FLASK_APP``21environment variable is used to specify how to load the application.Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 22why, one step back22Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 23why, one step back23Unix Bash (Linux, Mac, etc.)::Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 24why, one step back24Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 25–26why, one step back25 $ export FLASK_APP=hello26 $ flask runMerge pull request #2378 from miguelgrinberg/default-flask-appDavid Lordbefore adoption29c055a
line 27why, one step back27Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 28why, one step back28Windows CMD::Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 29why, one step back29Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 30–31why, one step back30 > set FLASK_APP=hello31 > flask runAdded EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 32why, one step back32Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 33why, one step back33Windows PowerShell::Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 34why, one step back34Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 35–36why, one step back35 > $env:FLASK_APP = "hello"36 > flask runAdded EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 37why, one step back37Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 38–39why, one step back38While ``FLASK_APP`` supports a variety of options for specifying your39application, most use cases should be simple. Here are the typical values:Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 40why, one step back40Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 41–44why, one step back41(nothing)42 The file :file:`wsgi.py` is imported, automatically detecting an app43 (``app``). This provides an easy way to create an app from a factory with44 extra arguments.Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 45why, one step back45Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 46–48why, one step back46``FLASK_APP=hello``47 The name is imported, automatically detecting an app (``app``) or factory48 (``create_app``).Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 49why, one step back49Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 50why, one step back50----Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 51why, one step back51Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 52–56why, one step back52``FLASK_APP`` has three parts: an optional path that sets the current working53directory, a Python file or dotted import path, and an optional variable54name of the instance or factory. If the name is a factory, it can optionally55be followed by arguments in parentheses. The following values demonstrate these56parts:Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 57why, one step back57Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 58–59why, one step back58``FLASK_APP=src/hello``59 Sets the current working directory to ``src`` then imports ``hello``.resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 60why, one step back60Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 61–62why, one step back61``FLASK_APP=hello.web``62 Imports the path ``hello.web``.resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 63why, one step back63Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 64–65why, one step back64``FLASK_APP=hello:app2``65 Uses the ``app2`` Flask instance in ``hello``.resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 66why, one step back66Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 67–69why, one step back67``FLASK_APP="hello:create_app('dev')"``68 The ``create_app`` factory in ``hello`` is called with the string ``'dev'``69 as the argument.resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 70why, one step back70Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 71–73why, one step back71If ``FLASK_APP`` is not set, the command will look for a file called72:file:`wsgi.py` or :file:`app.py` and try to detect an application instance or73factory.resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 74why, one step back74Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 75–78why, one step back75Within the given import, the command looks for an application instance named76``app`` or ``application``, then any application instance. If no instance is77found, the command looks for a factory function named ``create_app`` or78``make_app`` that returns an instance.Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 79why, one step back79Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 80–84why, one step back80When calling an application factory, if the factory takes an argument named81``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 back85resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 86why, one step back86Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 87–88why, one step back87Run the Development Server88--------------------------Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 89why, one step back89Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 90–91why, one step back90The :func:`run <cli.run_command>` command will start the development server. It91replaces 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 back92Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 93–95why, one step back93 $ flask run94 * 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 back96Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 97–100why, one step back97.. 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 back101102Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 103–104why, one step back103Open a Shell104------------Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 105why, one step back105Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 106–108why, one step back106To explore the data in your application, you can start an interactive Python107shell 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 back109Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 110–115why, one step back110 $ flask shell111 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 back116Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 117why, one step back117Use :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 back118119Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 120–121why, one step back120Debug Mode121----------Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 122why, one step back122Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 123–126why, one step back123Set the :envvar:`FLASK_DEBUG` environment variable to override the124application'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 back127Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 128–134why, one step back128 $ FLASK_DEBUG=1 flask run129 * 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-919Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
lines 135–136why, one step back135136Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
lines 137–138why, one step back137.. _dotenv:138Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 139–140why, one step back139Environment Variables From dotenv140---------------------------------Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
line 141why, one step back141Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 142–145why, one step back142Rather than setting ``FLASK_APP`` each time you open a new terminal, you can143use Flask's dotenv support to set environment variables automatically.144145If `python-dotenv`_ is installed, running the ``flask`` command will setMerge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
lines 146–155why, one step back146environment 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.155Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 156why, one step back156Directories are scanned upwards from the directory you call ``flask``Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
lines 157–160why, one step back157from to locate the files. The current working directory will be set to the158location of the file, with the assumption that that is the top level project159directory.160Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 161–163why, one step back161The files are only loaded by the ``flask`` command or calling162: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 back164165.. _python-dotenv: https://github.com/theskumar/python-dotenv#readme166Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 167why, one step back167Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 168–169why, one step back168Environment Variables From virtualenv169-------------------------------------Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 170why, one step back170Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 171–173why, one step back171If you do not want to install dotenv support, you can still set environment172variables 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 back174Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 175why, one step back175Unix Bash, :file:`venv/bin/activate`::Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 176why, one step back176Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 177why, one step back177 export FLASK_APP=helloAdded EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 178why, one step back178Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 179why, one step back179Windows 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 back180Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 181why, one step back181 set FLASK_APP=helloAdded EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 182why, one step back182Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 183–185why, one step back183It is preferred to use dotenv support over this, since :file:`.flaskenv` can be184committed 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 back186187Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 188–189why, one step back188Custom Commands189---------------Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 190why, one step back190Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 191–192why, one step back191The ``flask`` command is implemented using `Click`_. See that project's192documentation 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 back193Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 194–195why, one step back194This example adds the command ``create_user`` that takes the argument195``name``. ::Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 196why, one step back196Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 197–198why, one step back197 import click198 from flask import FlaskAdded EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 199why, one step back199Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 200why, one step back200 app = Flask(__name__)Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 201why, one step back201Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 202–205why, one step back202 @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 back206Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 207–213why, one step back207::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. ::213Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 214why, one step back214 import clickMerge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 215–216why, one step back215 from flask import Flask216 from flask.cli import AppGroupAdded EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 217why, one step back217Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 218–219why, one step back218 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 back220Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 221–224why, one step back221 @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 back225Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 226why, one step back226 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 back227Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 228why, one step back228::Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 229why, one step back229Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 230why, one step back230 flask user create demoAdded EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
lines 231–232why, one step back231232Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 233–234why, one step back233Application Context234~~~~~~~~~~~~~~~~~~~Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 235why, one step back235Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 236–241why, one step back236Commands 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 back242Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 243–244why, one step back243 import click244 from flask.cli import with_appcontextAdded EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 245why, one step back245Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 246–266why, one step back246 @click.command247 @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` ::266resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 267why, one step back267 from setuptools import setupAdded EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 268why, one step back268resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 269–270why, one step back269 setup(270 name='flask-my-extension',Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 271–276why, one step back271 ...,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 back277 )Added EXPLAIN_TEMPLATE_LOADING to help people debug templates not being loaded.Armin Ronacherbefore adoptionbafc139
line 278why, one step back278resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 279why, one step back279Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 280–284why, one step back280.. _entry point: https://packaging.python.org/tutorials/distributing-packages/#entry-points281282Inside :file:`flask_my_extension/commands.py` you can then export a Click283object::284resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 285–288why, one step back285 import click286287 @click.command()288 def cli():Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 289why, one step back289 ...resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 290why, one step back290Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 291–292why, one step back291Once 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 back293Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 294–342why, one step back294295.. _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-scripts341342resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 343–345why, one step back343PyCharm Integration344-------------------345Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 346–348why, one step back346The 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 back349Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 350–351why, one step back350In PyCharm, with your project open, click on *Run* from the menu bar and go to351*Edit Configurations*. You'll be greeted by a screen similar to this:resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 352–357why, one step back352353.. image:: _static/pycharm-runconfig.png354 :align: center355 :class: screenshot356 :alt: screenshot of pycharm's run configuration settings357Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 358–360why, one step back358There'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 back361Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 362–364why, one step back362For 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 back365Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 366–367why, one step back366The *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 back368Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 369–372why, one step back369You can skip this next step if you're using :ref:`dotenv`. We need to add an370environment 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 back373374Next we need to set the working directory (**D**) to be the same folder whereMerge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 375–376why, one step back375our application file or package resides. PyCharm changed it to the directory376with the ``flask`` executable when we selected it earlier, which is incorrect.resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 377–378why, one step back377378Finally, untick the *PYTHONPATH* options (**E**) and give the configuration aMerge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 379why, one step back379good descriptive name, such as "Run Flask Server", and click *Apply*.resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 380why, one step back380Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 381–383why, one step back381Now 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``.