cli.rst
This file
Path
docs/cli.rst
468 lines · 14.4 KiB
Last changed by
Merge branch '1.0.x'David Lordbefore adoption2236ba9
History of this fileTake it
The walk stopped after 200 commits: lines older than 62406e6 point at it.
Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 1–2why, one step back1.. currentmodule:: flask2Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
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.Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 12why, one step back12Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 13why, one step back13.. _Click: http://click.pocoo.org/Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
lines 14–15why, one step back1415Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 16–17why, one step back16Application Discovery17---------------------Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
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.Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 22why, one step back22Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 23why, one step back23Unix Bash (Linux, Mac, etc.)::Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
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::Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
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 runMerge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 32why, one step back32Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 33why, one step back33Windows PowerShell::Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
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 runMerge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
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:Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
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.Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
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``).Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 49why, one step back49Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 50why, one step back50----Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
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:Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
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``.Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
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``.Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
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``.Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
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.Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 70why, one step back70Merge branch '1.0.x'David Lordbefore adoption2236ba9
lines 71–73why, one step back71If ``FLASK_APP`` is not set, the command will try to import "app" or72"wsgi" (as a ".py" file, or package) and try to detect an application73instance or factory.Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
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.Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 79why, one step back79Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 80why, one step back80When calling an application factory, if the factory takes an argument namedMerge pull request #2870 from jerr0328/docs-update-cli-scriptinfoDavid Lordbefore adoption93e4b71
lines 81–86why, one step back81``script_info``, then the :class:`~cli.ScriptInfo` instance is passed as a82keyword argument. If the application factory takes only one argument and no83parentheses follow the factory name, the :class:`~cli.ScriptInfo` instance84is passed as a positional argument. If parentheses follow the factory name,85their contents are parsed as Python literals and passes as arguments to the86function. This means that strings must still be in quotes.Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
lines 87–88why, one step back8788Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 89–90why, one step back89Run the Development Server90--------------------------Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 91why, one step back91Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 92–93why, one step back92The :func:`run <cli.run_command>` command will start the development server. It93replaces the :meth:`Flask.run` method in most cases. ::Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 94why, one step back94Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 95–97why, one step back95 $ flask run96 * Serving Flask app "hello"97 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 98why, one step back98Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 99–102why, one step back99.. warning:: Do not use this command to run your application in production.100 Only use the development server during development. The development server101 is provided for convenience, but is not designed to be particularly secure,102 stable, or efficient. See :ref:`deployment` for how to run in production.Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
lines 103–104why, one step back103104Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 105–106why, one step back105Open a Shell106------------Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 107why, one step back107Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 108–110why, one step back108To explore the data in your application, you can start an interactive Python109shell with the :func:`shell <cli.shell_command>` command. An application110context will be active, and the app instance will be imported. ::Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 111why, one step back111Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 112–117why, one step back112 $ flask shell113 Python 3.6.2 (default, Jul 20 2017, 03:52:27)114 [GCC 7.1.1 20170630] on linux115 App: example116 Instance: /home/user/Projects/hello/instance117 >>>Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 118why, one step back118Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 119why, one step back119Use :meth:`~Flask.shell_context_processor` to add other automatic imports.Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 120why, one step back120Merge pull request #2593 from pallets/env-docsDavid Lordbefore adoptionc3a9978
line 121why, one step back121Add Support for FLASK_ENV (#2570)Armin Ronacherbefore adoption2433522
lines 122–123why, one step back122Environments123------------Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 124why, one step back124Add Support for FLASK_ENV (#2570)Armin Ronacherbefore adoption2433522
lines 125–126why, one step back125.. versionadded:: 1.0126Merge pull request #2593 from pallets/env-docsDavid Lordbefore adoptionc3a9978
lines 127–131why, one step back127The environment in which the Flask app runs is set by the128:envvar:`FLASK_ENV` environment variable. If not set it defaults to129``production``. The other recognized environment is ``development``.130Flask and extensions may choose to enable behaviors based on the131environment.Add Support for FLASK_ENV (#2570)Armin Ronacherbefore adoption2433522
line 132why, one step back132Merge pull request #2593 from pallets/env-docsDavid Lordbefore adoptionc3a9978
lines 133–135why, one step back133If the env is set to ``development``, the ``flask`` command will enable134debug mode and ``flask run`` will enable the interactive debugger and135reloader.Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 136why, one step back136Add Support for FLASK_ENV (#2570)Armin Ronacherbefore adoption2433522
lines 137–138why, one step back137::138Merge pull request #2593 from pallets/env-docsDavid Lordbefore adoptionc3a9978
line 139why, one step back139 $ FLASK_ENV=development flask runMerge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 140why, one step back140 * Serving Flask app "hello"Merge pull request #2593 from pallets/env-docsDavid Lordbefore adoptionc3a9978
lines 141–142why, one step back141 * Environment: development142 * Debug mode: onMerge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 143–146why, one step back143 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)144 * Restarting with inotify reloader145 * Debugger is active!146 * Debugger PIN: 223-456-919Merge pull request #2593 from pallets/env-docsDavid Lordbefore adoptionc3a9978
lines 147–154why, one step back147148149Debug Mode150----------151152Debug mode will be enabled when :envvar:`FLASK_ENV` is ``development``,153as described above. If you want to control debug mode separately, use154:envvar:`FLASK_DEBUG`. The value ``1`` enables it, ``0`` disables it.Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
lines 155–156why, one step back155156Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
lines 157–158why, one step back157.. _dotenv:158Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 159–160why, one step back159Environment Variables From dotenv160---------------------------------Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
line 161why, one step back161Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 162–165why, one step back162Rather than setting ``FLASK_APP`` each time you open a new terminal, you can163use Flask's dotenv support to set environment variables automatically.164165If `python-dotenv`_ is installed, running the ``flask`` command will setMerge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
lines 166–175why, one step back166environment variables defined in the files :file:`.env` and :file:`.flaskenv`.167This can be used to avoid having to set ``FLASK_APP`` manually every time you168open a new terminal, and to set configuration using environment variables169similar to how some deployment services work.170171Variables set on the command line are used over those set in :file:`.env`,172which are used over those set in :file:`.flaskenv`. :file:`.flaskenv` should be173used for public variables, such as ``FLASK_APP``, while :file:`.env` should not174be committed to your repository so that it can set private variables.175Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 176why, one step back176Directories are scanned upwards from the directory you call ``flask``Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
lines 177–180why, one step back177from to locate the files. The current working directory will be set to the178location of the file, with the assumption that that is the top level project179directory.180Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 181–183why, one step back181The files are only loaded by the ``flask`` command or calling182:meth:`~Flask.run`. If you would like to load these files when running in183production, you should call :func:`~cli.load_dotenv` manually.Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
lines 184–186why, one step back184185.. _python-dotenv: https://github.com/theskumar/python-dotenv#readme186Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 187why, one step back187document cli option env varsDavid Lordbefore adoption93698b5
lines 188–195why, one step back188Setting Command Options189~~~~~~~~~~~~~~~~~~~~~~~190191Click is configured to load default values for command options from192environment variables. The variables use the pattern193``FLASK_COMMAND_OPTION``. For example, to set the port for the run194command, instead of ``flask run --port 8000``:195Merge pull request #3175 from singingwolfboy/fix-rst-line-too-longDavid Baumgoldbefore adoption819368a
line 196why, one step back196.. code-block:: bashdocument cli option env varsDavid Lordbefore adoption93698b5
line 197why, one step back197Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6
lines 198–199why, one step back198 $ export FLASK_RUN_PORT=8000199 $ flask rundocument cli option env varsDavid Lordbefore adoption93698b5
lines 200–205why, one step back200 * Running on http://127.0.0.1:8000/201202These can be added to the ``.flaskenv`` file just like ``FLASK_APP`` to203control default command options.204205Merge branch '1.0-maintenance'David Lordbefore adoption1fa9185
lines 206–211why, one step back206Disable dotenv207~~~~~~~~~~~~~~208209The ``flask`` command will show a message if it detects dotenv files but210python-dotenv is not installed.211Merge pull request #3175 from singingwolfboy/fix-rst-line-too-longDavid Baumgoldbefore adoption819368a
line 212why, one step back212.. code-block:: bashMerge branch '1.0-maintenance'David Lordbefore adoption1fa9185
line 213why, one step back213Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6
line 214why, one step back214 $ flask runMerge branch '1.0-maintenance'David Lordbefore adoption1fa9185
lines 215–223why, one step back215 * Tip: There are .env files present. Do "pip install python-dotenv" to use them.216217You can tell Flask not to load dotenv files even when python-dotenv is218installed by setting the ``FLASK_SKIP_DOTENV`` environment variable.219This can be useful if you want to load them manually, or if you're using220a project runner that loads them already. Keep in mind that the221environment variables must be set before the app loads or it won't222configure as expected.223Merge pull request #3175 from singingwolfboy/fix-rst-line-too-longDavid Baumgoldbefore adoption819368a
line 224why, one step back224.. code-block:: bashMerge branch '1.0-maintenance'David Lordbefore adoption1fa9185
line 225why, one step back225Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6
lines 226–227why, one step back226 $ export FLASK_SKIP_DOTENV=1227 $ flask runMerge branch '1.0-maintenance'David Lordbefore adoption1fa9185
lines 228–229why, one step back228229Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 230–231why, one step back230Environment Variables From virtualenv231-------------------------------------Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 232why, one step back232Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 233–235why, one step back233If you do not want to install dotenv support, you can still set environment234variables by adding them to the end of the virtualenv's :file:`activate`235script. Activating the virtualenv will set the variables.Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 236why, one step back236Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 237why, one step back237Unix Bash, :file:`venv/bin/activate`::Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 238why, one step back238Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6
line 239why, one step back239 $ export FLASK_APP=helloMerge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 240why, one step back240Merge pull request #2553 from greyli/patch-6David Lordbefore adoptiondb56e38
line 241why, one step back241Windows CMD, :file:`venv\\Scripts\\activate.bat`::Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 242why, one step back242Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6
line 243why, one step back243 > set FLASK_APP=helloMerge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 244why, one step back244Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 245–247why, one step back245It is preferred to use dotenv support over this, since :file:`.flaskenv` can be246committed to the repository so that it works automatically wherever the project247is checked out.Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
lines 248–249why, one step back248249Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 250–251why, one step back250Custom Commands251---------------Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 252why, one step back252Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 253–254why, one step back253The ``flask`` command is implemented using `Click`_. See that project's254documentation for full information about writing commands.Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 255why, one step back255Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 256–257why, one step back256This example adds the command ``create_user`` that takes the argument257``name``. ::Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 258why, one step back258Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 259–260why, one step back259 import click260 from flask import FlaskMerge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 261why, one step back261Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 262why, one step back262 app = Flask(__name__)Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 263why, one step back263Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 264–267why, one step back264 @app.cli.command()265 @click.argument('name')266 def create_user(name):267 ...Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 268why, one step back268Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 269–270why, one step back269::270Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6
line 271why, one step back271 $ flask create_user adminMerge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 272–275why, one step back272273This example adds the same command, but as ``user create``, a command in a274group. This is useful if you want to organize multiple related commands. ::275Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 276why, one step back276 import clickMerge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 277–278why, one step back277 from flask import Flask278 from flask.cli import AppGroupMerge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 279why, one step back279Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 280–281why, one step back280 app = Flask(__name__)281 user_cli = AppGroup('user')Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 282why, one step back282Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 283–286why, one step back283 @user_cli.command('create')284 @click.argument('name')285 def create_user(name):286 ...Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 287why, one step back287Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 288why, one step back288 app.cli.add_command(user_cli)Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 289why, one step back289Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 290why, one step back290::Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 291why, one step back291Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6
line 292why, one step back292 $ flask user create demoMerge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 293why, one step back293Merge pull request #2611 from pallets/cli-test-docsDavid Lordbefore adoption8c2c802
lines 294–296why, one step back294See :ref:`testing-cli` for an overview of how to test your custom295commands.296Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 297why, one step back297Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 298–299why, one step back298Application Context299~~~~~~~~~~~~~~~~~~~Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 300why, one step back300Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 301–306why, one step back301Commands added using the Flask app's :attr:`~Flask.cli`302:meth:`~cli.AppGroup.command` decorator will be executed with an application303context pushed, so your command and extensions have access to the app and its304configuration. If you create a command using the Click :func:`~click.command`305decorator instead of the Flask decorator, you can use306:func:`~cli.with_appcontext` to get the same behavior. ::Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 307why, one step back307Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 308–309why, one step back308 import click309 from flask.cli import with_appcontextMerge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 310why, one step back310Merge pull request #2915 from loomchild/masterDavid Lordbefore adoptionc92001d
line 311why, one step back311 @click.command()Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 312–331why, one step back312 @with_appcontext313 def do_work():314 ...315316 app.cli.add_command(do_work)317318If you're sure a command doesn't need the context, you can disable it::319320 @app.cli.command(with_appcontext=False)321 def do_work():322 ...323324325Plugins326-------327328Flask will automatically load commands specified in the ``flask.commands``329`entry point`_. This is useful for extensions that want to add commands when330they are installed. Entry points are specified in :file:`setup.py` ::331Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
lines 332–335why, one step back332 from setuptools import setup333334 setup(335 name='flask-my-extension',Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 336–341why, one step back336 ...,337 entry_points={338 'flask.commands': [339 'my-command=flask_my_extension.commands:cli'340 ],341 },Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
lines 342–344why, one step back342 )343344Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 345–349why, one step back345.. _entry point: https://packaging.python.org/tutorials/distributing-packages/#entry-points346347Inside :file:`flask_my_extension/commands.py` you can then export a Click348object::349Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
lines 350–353why, one step back350 import click351352 @click.command()353 def cli():Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 354why, one step back354 ...Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 355why, one step back355Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 356–357why, one step back356Once that package is installed in the same virtualenv as your Flask project,357you can run ``flask my-command`` to invoke the command.Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 358why, one step back358Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 359–404why, one step back359360.. _custom-scripts:361362Custom Scripts363--------------364365When you are using the app factory pattern, it may be more convenient to define366your own Click script. Instead of using ``FLASK_APP`` and letting Flask load367your application, you can create your own Click object and export it as a368`console script`_ entry point.369370Create an instance of :class:`~cli.FlaskGroup` and pass it the factory::371372 import click373 from flask import Flask374 from flask.cli import FlaskGroup375376 def create_app():377 app = Flask('wiki')378 # other setup379 return app380381 @click.group(cls=FlaskGroup, create_app=create_app)382 def cli():383 """Management script for the Wiki application."""384385Define the entry point in :file:`setup.py`::386387 from setuptools import setup388389 setup(390 name='flask-my-extension',391 ...,392 entry_points={393 'console_scripts': [394 'wiki=wiki:cli'395 ],396 },397 )398399Install the application in the virtualenv in editable mode and the custom400script is available. Note that you don't need to set ``FLASK_APP``. ::401402 $ pip install -e .403 $ wiki run404Merge pull request #2610 from pallets/cli-syntaxerrorDavid Lordbefore adoption94aaa24
lines 405–413why, one step back405.. admonition:: Errors in Custom Scripts406407 When using a custom script, if you introduce an error in your408 module-level code, the reloader will fail because it can no longer409 load the entry point.410411 The ``flask`` command, being separate from your code, does not have412 this issue and is recommended in most cases.413Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 414–416why, one step back414.. _console script: https://packaging.python.org/tutorials/distributing-packages/#console-scripts415416Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
lines 417–419why, one step back417PyCharm Integration418-------------------419Merge pull request #2610 from pallets/cli-syntaxerrorDavid Lordbefore adoption94aaa24
lines 420–423why, one step back420Prior to PyCharm 2018.1, the Flask CLI features weren't yet fully421integrated into PyCharm. We have to do a few tweaks to get them working422smoothly. These instructions should be similar for any other IDE you423might want to use.Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 424why, one step back424Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21
lines 425–427why, one step back425In PyCharm, with your project open, click on *Run* from the menu bar and426go to *Edit Configurations*. You'll be greeted by a screen similar to427this:Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
lines 428–429why, one step back428429.. image:: _static/pycharm-runconfig.pngMerge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21
lines 430–432why, one step back430 :align: center431 :class: screenshot432 :alt: screenshot of pycharm's run configuration settingsMerge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 433why, one step back433Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21
lines 434–437why, one step back434There's quite a few options to change, but once we've done it for one435command, we can easily copy the entire configuration and make a single436tweak to give us access to other commands, including any custom ones you437may implement yourself.Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 438why, one step back438Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21
lines 439–442why, one step back439Click the + (*Add New Configuration*) button and select *Python*. Give440the configuration a good descriptive name such as "Run Flask Server".441For the ``flask run`` command, check "Single instance only" since you442can't run the server more than once at the same time.Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 443why, one step back443Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21
line 444why, one step back444Select *Module name* from the dropdown (**A**) then input ``flask``.Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 445why, one step back445Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21
lines 446–448why, one step back446The *Parameters* field (**B**) is set to the CLI command to execute447(with any arguments). In this example we use ``run``, which will run448the development server.Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 449why, one step back449Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21
lines 450–453why, one step back450You can skip this next step if you're using :ref:`dotenv`. We need to451add an environment variable (**C**) to identify our application. Click452on the browse button and add an entry with ``FLASK_APP`` on the left and453the Python import or file on the right (``hello`` for example).Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 454why, one step back454Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21
lines 455–456why, one step back455Next we need to set the working directory (**D**) to be the folder where456our application resides.Merge pull request #2352 from davidism/json-object-hookDavid Lordbefore adoption62406e6
line 457why, one step back457Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21
lines 458–467why, one step back458If you have installed your project as a package in your virtualenv, you459may untick the *PYTHONPATH* options (**E**). This will more accurately460match how you deploy the app later.461462Click *Apply* to save the configuration, or *OK* to save and close the463window. Select the configuration in the main PyCharm window and click464the play button next to it to run the server.465466Now that we have a configuration which runs ``flask run`` from within467PyCharm, we can copy that configuration and alter the *Script* argumentMerge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 468why, one step back468to run a different CLI command, e.g. ``flask shell``.