cli.rst
This file
Path
docs/cli.rst
442 lines · 13.5 KiB
Last changed by
document cli option env varsDavid Lordbefore adoption93698b5
History of this fileTake it
The walk stopped after 200 commits: lines older than aaebe9f point at it.
Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 1–2why, one step back1.. currentmodule:: flask2Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
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 #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
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 #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
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 #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
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 #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
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 #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
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 #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
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 #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 32why, one step back32Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 33why, one step back33Windows PowerShell::Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
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 #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
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 #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
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 #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
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 #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 49why, one step back49Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 50why, one step back50----Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
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 #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
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.Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
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.Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
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--------------------------Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
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. ::Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
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)Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
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.Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
lines 101–102why, one step back101102Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 103–104why, one step back103Open a Shell104------------Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
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. ::Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
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 >>>Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
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.Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 118why, one step back118Merge pull request #2593 from pallets/env-docsDavid Lordbefore adoptionc3a9978
line 119why, one step back119Add Support for FLASK_ENV (#2570)Armin Ronacherbefore adoption2433522
lines 120–121why, one step back120Environments121------------Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 122why, one step back122Add Support for FLASK_ENV (#2570)Armin Ronacherbefore adoption2433522
lines 123–124why, one step back123.. versionadded:: 1.0124Merge pull request #2593 from pallets/env-docsDavid Lordbefore adoptionc3a9978
lines 125–129why, one step back125The environment in which the Flask app runs is set by the126:envvar:`FLASK_ENV` environment variable. If not set it defaults to127``production``. The other recognized environment is ``development``.128Flask and extensions may choose to enable behaviors based on the129environment.Add Support for FLASK_ENV (#2570)Armin Ronacherbefore adoption2433522
line 130why, one step back130Merge pull request #2593 from pallets/env-docsDavid Lordbefore adoptionc3a9978
lines 131–133why, one step back131If the env is set to ``development``, the ``flask`` command will enable132debug mode and ``flask run`` will enable the interactive debugger and133reloader.Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 134why, one step back134Add Support for FLASK_ENV (#2570)Armin Ronacherbefore adoption2433522
lines 135–136why, one step back135::136Merge pull request #2593 from pallets/env-docsDavid Lordbefore adoptionc3a9978
line 137why, one step back137 $ FLASK_ENV=development flask runMerge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 138why, one step back138 * Serving Flask app "hello"Merge pull request #2593 from pallets/env-docsDavid Lordbefore adoptionc3a9978
lines 139–140why, one step back139 * Environment: development140 * Debug mode: onMerge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 141–144why, one step back141 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)142 * Restarting with inotify reloader143 * Debugger is active!144 * Debugger PIN: 223-456-919Merge pull request #2593 from pallets/env-docsDavid Lordbefore adoptionc3a9978
lines 145–152why, one step back145146147Debug Mode148----------149150Debug mode will be enabled when :envvar:`FLASK_ENV` is ``development``,151as described above. If you want to control debug mode separately, use152:envvar:`FLASK_DEBUG`. The value ``1`` enables it, ``0`` disables it.Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
lines 153–154why, one step back153154Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
lines 155–156why, one step back155.. _dotenv:156Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 157–158why, one step back157Environment Variables From dotenv158---------------------------------Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
line 159why, one step back159Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 160–163why, one step back160Rather than setting ``FLASK_APP`` each time you open a new terminal, you can161use Flask's dotenv support to set environment variables automatically.162163If `python-dotenv`_ is installed, running the ``flask`` command will setMerge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
lines 164–173why, one step back164environment variables defined in the files :file:`.env` and :file:`.flaskenv`.165This can be used to avoid having to set ``FLASK_APP`` manually every time you166open a new terminal, and to set configuration using environment variables167similar to how some deployment services work.168169Variables set on the command line are used over those set in :file:`.env`,170which are used over those set in :file:`.flaskenv`. :file:`.flaskenv` should be171used for public variables, such as ``FLASK_APP``, while :file:`.env` should not172be committed to your repository so that it can set private variables.173Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 174why, one step back174Directories are scanned upwards from the directory you call ``flask``Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
lines 175–178why, one step back175from to locate the files. The current working directory will be set to the176location of the file, with the assumption that that is the top level project177directory.178Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 179–181why, one step back179The files are only loaded by the ``flask`` command or calling180:meth:`~Flask.run`. If you would like to load these files when running in181production, you should call :func:`~cli.load_dotenv` manually.Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4
lines 182–184why, one step back182183.. _python-dotenv: https://github.com/theskumar/python-dotenv#readme184Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 185why, one step back185document cli option env varsDavid Lordbefore adoption93698b5
lines 186–203why, one step back186Setting Command Options187~~~~~~~~~~~~~~~~~~~~~~~188189Click is configured to load default values for command options from190environment variables. The variables use the pattern191``FLASK_COMMAND_OPTION``. For example, to set the port for the run192command, instead of ``flask run --port 8000``:193194.. code-block:: none195196 export FLASK_RUN_PORT=8000197 flask run198 * Running on http://127.0.0.1:8000/199200These can be added to the ``.flaskenv`` file just like ``FLASK_APP`` to201control default command options.202203Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 204–205why, one step back204Environment Variables From virtualenv205-------------------------------------Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 206why, one step back206Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 207–209why, one step back207If you do not want to install dotenv support, you can still set environment208variables by adding them to the end of the virtualenv's :file:`activate`209script. Activating the virtualenv will set the variables.Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 210why, one step back210Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 211why, one step back211Unix Bash, :file:`venv/bin/activate`::Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 212why, one step back212Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 213why, one step back213 export FLASK_APP=helloMerge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 214why, one step back214Merge pull request #2553 from greyli/patch-6David Lordbefore adoptiondb56e38
line 215why, one step back215Windows CMD, :file:`venv\\Scripts\\activate.bat`::Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 216why, one step back216Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 217why, one step back217 set FLASK_APP=helloMerge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 218why, one step back218Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 219–221why, one step back219It is preferred to use dotenv support over this, since :file:`.flaskenv` can be220committed to the repository so that it works automatically wherever the project221is checked out.Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
lines 222–223why, one step back222223Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 224–225why, one step back224Custom Commands225---------------Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 226why, one step back226Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 227–228why, one step back227The ``flask`` command is implemented using `Click`_. See that project's228documentation for full information about writing commands.Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 229why, one step back229Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 230–231why, one step back230This example adds the command ``create_user`` that takes the argument231``name``. ::Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 232why, one step back232Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 233–234why, one step back233 import click234 from flask import FlaskMerge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 235why, one step back235Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 236why, one step back236 app = Flask(__name__)Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 237why, one step back237Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 238–241why, one step back238 @app.cli.command()239 @click.argument('name')240 def create_user(name):241 ...Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 242why, one step back242Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 243–249why, one step back243::244245 flask create_user admin246247This example adds the same command, but as ``user create``, a command in a248group. This is useful if you want to organize multiple related commands. ::249Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 250why, one step back250 import clickMerge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 251–252why, one step back251 from flask import Flask252 from flask.cli import AppGroupMerge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 253why, one step back253Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 254–255why, one step back254 app = Flask(__name__)255 user_cli = AppGroup('user')Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 256why, one step back256Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 257–260why, one step back257 @user_cli.command('create')258 @click.argument('name')259 def create_user(name):260 ...Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 261why, one step back261Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 262why, one step back262 app.cli.add_command(user_cli)Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 263why, one step back263Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 264why, one step back264::Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 265why, one step back265Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 266why, one step back266 flask user create demoMerge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 267why, one step back267Merge pull request #2611 from pallets/cli-test-docsDavid Lordbefore adoption8c2c802
lines 268–270why, one step back268See :ref:`testing-cli` for an overview of how to test your custom269commands.270Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 271why, one step back271Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 272–273why, one step back272Application Context273~~~~~~~~~~~~~~~~~~~Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 274why, one step back274Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 275–280why, one step back275Commands added using the Flask app's :attr:`~Flask.cli`276:meth:`~cli.AppGroup.command` decorator will be executed with an application277context pushed, so your command and extensions have access to the app and its278configuration. If you create a command using the Click :func:`~click.command`279decorator instead of the Flask decorator, you can use280:func:`~cli.with_appcontext` to get the same behavior. ::Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 281why, one step back281Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 282–283why, one step back282 import click283 from flask.cli import with_appcontextMerge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 284why, one step back284Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 285–305why, one step back285 @click.command286 @with_appcontext287 def do_work():288 ...289290 app.cli.add_command(do_work)291292If you're sure a command doesn't need the context, you can disable it::293294 @app.cli.command(with_appcontext=False)295 def do_work():296 ...297298299Plugins300-------301302Flask will automatically load commands specified in the ``flask.commands``303`entry point`_. This is useful for extensions that want to add commands when304they are installed. Entry points are specified in :file:`setup.py` ::305resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 306why, one step back306 from setuptools import setupMerge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 307why, one step back307resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 308–309why, one step back308 setup(309 name='flask-my-extension',Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 310–315why, one step back310 ...,311 entry_points={312 'flask.commands': [313 'my-command=flask_my_extension.commands:cli'314 ],315 },resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 316why, one step back316 )Merge pull request #1431 from untitaker/flask-errorhandling-amendMarkus Unterwaditzerbefore adoptionaaebe9f
line 317why, one step back317resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 318why, one step back318Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 319–323why, one step back319.. _entry point: https://packaging.python.org/tutorials/distributing-packages/#entry-points320321Inside :file:`flask_my_extension/commands.py` you can then export a Click322object::323resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 324–327why, one step back324 import click325326 @click.command()327 def cli():Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 328why, one step back328 ...resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 329why, one step back329Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 330–331why, one step back330Once that package is installed in the same virtualenv as your Flask project,331you can run ``flask my-command`` to invoke the command.resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 332why, one step back332Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 333–378why, one step back333334.. _custom-scripts:335336Custom Scripts337--------------338339When you are using the app factory pattern, it may be more convenient to define340your own Click script. Instead of using ``FLASK_APP`` and letting Flask load341your application, you can create your own Click object and export it as a342`console script`_ entry point.343344Create an instance of :class:`~cli.FlaskGroup` and pass it the factory::345346 import click347 from flask import Flask348 from flask.cli import FlaskGroup349350 def create_app():351 app = Flask('wiki')352 # other setup353 return app354355 @click.group(cls=FlaskGroup, create_app=create_app)356 def cli():357 """Management script for the Wiki application."""358359Define the entry point in :file:`setup.py`::360361 from setuptools import setup362363 setup(364 name='flask-my-extension',365 ...,366 entry_points={367 'console_scripts': [368 'wiki=wiki:cli'369 ],370 },371 )372373Install the application in the virtualenv in editable mode and the custom374script is available. Note that you don't need to set ``FLASK_APP``. ::375376 $ pip install -e .377 $ wiki run378Merge pull request #2610 from pallets/cli-syntaxerrorDavid Lordbefore adoption94aaa24
lines 379–387why, one step back379.. admonition:: Errors in Custom Scripts380381 When using a custom script, if you introduce an error in your382 module-level code, the reloader will fail because it can no longer383 load the entry point.384385 The ``flask`` command, being separate from your code, does not have386 this issue and is recommended in most cases.387Merge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
lines 388–390why, one step back388.. _console script: https://packaging.python.org/tutorials/distributing-packages/#console-scripts389390resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 391–393why, one step back391PyCharm Integration392-------------------393Merge pull request #2610 from pallets/cli-syntaxerrorDavid Lordbefore adoption94aaa24
lines 394–397why, one step back394Prior to PyCharm 2018.1, the Flask CLI features weren't yet fully395integrated into PyCharm. We have to do a few tweaks to get them working396smoothly. These instructions should be similar for any other IDE you397might want to use.resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 398why, one step back398Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21
lines 399–401why, one step back399In PyCharm, with your project open, click on *Run* from the menu bar and400go to *Edit Configurations*. You'll be greeted by a screen similar to401this:resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 402–403why, one step back402403.. image:: _static/pycharm-runconfig.pngMerge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21
lines 404–406why, one step back404 :align: center405 :class: screenshot406 :alt: screenshot of pycharm's run configuration settingsresolve merge conflictsKenneth Reitzbefore adoption31174fe
line 407why, one step back407Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21
lines 408–411why, one step back408There's quite a few options to change, but once we've done it for one409command, we can easily copy the entire configuration and make a single410tweak to give us access to other commands, including any custom ones you411may implement yourself.resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 412why, one step back412Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21
lines 413–416why, one step back413Click the + (*Add New Configuration*) button and select *Python*. Give414the configuration a good descriptive name such as "Run Flask Server".415For the ``flask run`` command, check "Single instance only" since you416can't run the server more than once at the same time.resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 417why, one step back417Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21
line 418why, one step back418Select *Module name* from the dropdown (**A**) then input ``flask``.resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 419why, one step back419Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21
lines 420–422why, one step back420The *Parameters* field (**B**) is set to the CLI command to execute421(with any arguments). In this example we use ``run``, which will run422the development server.resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 423why, one step back423Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21
lines 424–427why, one step back424You can skip this next step if you're using :ref:`dotenv`. We need to425add an environment variable (**C**) to identify our application. Click426on the browse button and add an entry with ``FLASK_APP`` on the left and427the Python import or file on the right (``hello`` for example).resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 428why, one step back428Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21
lines 429–430why, one step back429Next we need to set the working directory (**D**) to be the folder where430our application resides.resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 431why, one step back431Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21
lines 432–441why, one step back432If you have installed your project as a package in your virtualenv, you433may untick the *PYTHONPATH* options (**E**). This will more accurately434match how you deploy the app later.435436Click *Apply* to save the configuration, or *OK* to save and close the437window. Select the configuration in the main PyCharm window and click438the play button next to it to run the server.439440Now that we have a configuration which runs ``flask run`` from within441PyCharm, we can copy that configuration and alter the *Script* argumentMerge pull request #2490 from davidism/cli-reviewDavid Lordbefore adoption0932d8f
line 442why, one step back442to run a different CLI command, e.g. ``flask shell``.