cli.rst
This file
Path
docs/cli.rst
720 lines · 20.5 KiB
Last changed by
Merge pull request #4189 from mgu/feat/use-exclude-patterns-reloaderDavid Lordbefore adoption48ee204
History of this fileTake it
The walk stopped after 200 commits: lines older than 4e854ee point at it.
Merge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
lines 1–20why, one step back1.. currentmodule:: flask23Command Line Interface4======================56Installing Flask installs the ``flask`` script, a `Click`_ command line7interface, in your virtualenv. Executed from the terminal, this script gives8access to built-in, extension, and application-defined commands. The ``--help``9option will give more information about any commands and options.1011.. _Click: https://click.palletsprojects.com/121314Application Discovery15---------------------1617The ``flask`` command is installed by Flask, not your application; it must be18told where to find your application in order to use it. The ``FLASK_APP``19environment variable is used to specify how to load the application.20Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
line 21why, one step back21.. tabs::Merge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
line 22why, one step back22Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
line 23why, one step back23 .. group-tab:: BashMerge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
line 24why, one step back24Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
line 25why, one step back25 .. code-block:: textMerge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
line 26why, one step back26Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
lines 27–28why, one step back27 $ export FLASK_APP=hello28 $ flask runMerge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
line 29why, one step back29Merge branch '2.0.x'David Lordbefore adoptione37e871
lines 30–36why, one step back30 .. group-tab:: Fish3132 .. code-block:: text3334 $ set -x FLASK_APP hello35 $ flask run36Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
line 37why, one step back37 .. group-tab:: CMDMerge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
line 38why, one step back38Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
line 39why, one step back39 .. code-block:: textMerge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
line 40why, one step back40Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
lines 41–50why, one step back41 > set FLASK_APP=hello42 > flask run4344 .. group-tab:: Powershell4546 .. code-block:: text4748 > $env:FLASK_APP = "hello"49 > flask run50Merge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
lines 51–54why, one step back51While ``FLASK_APP`` supports a variety of options for specifying your52application, most use cases should be simple. Here are the typical values:5354(nothing)Merge pull request #3999 from greyli/improve-cli-docsDavid Lordbefore adoption28262c3
lines 55–57why, one step back55 The name "app" or "wsgi" is imported (as a ".py" file, or package),56 automatically detecting an app (``app`` or ``application``) or57 factory (``create_app`` or ``make_app``).Merge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
lines 58–59why, one step back5859``FLASK_APP=hello``Merge pull request #3999 from greyli/improve-cli-docsDavid Lordbefore adoption28262c3
lines 60–61why, one step back60 The given name is imported, automatically detecting an app (``app``61 or ``application``) or factory (``create_app`` or ``make_app``).Merge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
lines 62–112why, one step back6263----6465``FLASK_APP`` has three parts: an optional path that sets the current working66directory, a Python file or dotted import path, and an optional variable67name of the instance or factory. If the name is a factory, it can optionally68be followed by arguments in parentheses. The following values demonstrate these69parts:7071``FLASK_APP=src/hello``72 Sets the current working directory to ``src`` then imports ``hello``.7374``FLASK_APP=hello.web``75 Imports the path ``hello.web``.7677``FLASK_APP=hello:app2``78 Uses the ``app2`` Flask instance in ``hello``.7980``FLASK_APP="hello:create_app('dev')"``81 The ``create_app`` factory in ``hello`` is called with the string ``'dev'``82 as the argument.8384If ``FLASK_APP`` is not set, the command will try to import "app" or85"wsgi" (as a ".py" file, or package) and try to detect an application86instance or factory.8788Within the given import, the command looks for an application instance named89``app`` or ``application``, then any application instance. If no instance is90found, the command looks for a factory function named ``create_app`` or91``make_app`` that returns an instance.9293If parentheses follow the factory name, their contents are parsed as94Python literals and passed as arguments and keyword arguments to the95function. This means that strings must still be in quotes.969798Run the Development Server99--------------------------100101The :func:`run <cli.run_command>` command will start the development server. It102replaces the :meth:`Flask.run` method in most cases. ::103104 $ flask run105 * Serving Flask app "hello"106 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)107108.. warning:: Do not use this command to run your application in production.109 Only use the development server during development. The development server110 is provided for convenience, but is not designed to be particularly secure,111 stable, or efficient. See :doc:`/deploying/index` for how to run in production.112Merge branch '2.0.x'David Lordbefore adoptionedac7e3
lines 113–117why, one step back113If another program is already using port 5000, you'll see114``OSError: [Errno 98]`` or ``OSError: [WinError 10013]`` when the115server tries to start. See :ref:`address-already-in-use` for how to116handle that.117Merge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
lines 118–126why, one step back118119Open a Shell120------------121122To explore the data in your application, you can start an interactive Python123shell with the :func:`shell <cli.shell_command>` command. An application124context will be active, and the app instance will be imported. ::125126 $ flask shellMerge pull request #4335 from pallets/drop-python-3.6David Lordbefore adoptionea93a52
lines 127–129why, one step back127 Python 3.10.0 (default, Oct 27 2021, 06:59:51) [GCC 11.1.0] on linux128 App: example [production]129 Instance: /home/david/Projects/pallets/flask/instanceMerge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
lines 130–149why, one step back130 >>>131132Use :meth:`~Flask.shell_context_processor` to add other automatic imports.133134135Environments136------------137138.. versionadded:: 1.0139140The environment in which the Flask app runs is set by the141:envvar:`FLASK_ENV` environment variable. If not set it defaults to142``production``. The other recognized environment is ``development``.143Flask and extensions may choose to enable behaviors based on the144environment.145146If the env is set to ``development``, the ``flask`` command will enable147debug mode and ``flask run`` will enable the interactive debugger and148reloader.149Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
line 150why, one step back150.. tabs::Merge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
line 151why, one step back151Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
line 152why, one step back152 .. group-tab:: BashMerge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
line 153why, one step back153Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
line 154why, one step back154 .. code-block:: textMerge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
line 155why, one step back155Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
lines 156–165why, one step back156 $ export FLASK_ENV=development157 $ flask run158 * Serving Flask app "hello"159 * Environment: development160 * Debug mode: on161 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)162 * Restarting with inotify reloader163 * Debugger is active!164 * Debugger PIN: 223-456-919165Merge branch '2.0.x'David Lordbefore adoptione37e871
lines 166–179why, one step back166 .. group-tab:: Fish167168 .. code-block:: text169170 $ set -x FLASK_ENV development171 $ flask run172 * Serving Flask app "hello"173 * Environment: development174 * Debug mode: on175 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)176 * Restarting with inotify reloader177 * Debugger is active!178 * Debugger PIN: 223-456-919179Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
lines 180–208why, one step back180 .. group-tab:: CMD181182 .. code-block:: text183184 > set FLASK_ENV=development185 > flask run186 * Serving Flask app "hello"187 * Environment: development188 * Debug mode: on189 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)190 * Restarting with inotify reloader191 * Debugger is active!192 * Debugger PIN: 223-456-919193194 .. group-tab:: Powershell195196 .. code-block:: text197198 > $env:FLASK_ENV = "development"199 > flask run200 * Serving Flask app "hello"201 * Environment: development202 * Debug mode: on203 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)204 * Restarting with inotify reloader205 * Debugger is active!206 * Debugger PIN: 223-456-919207208Merge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
lines 209–217why, one step back209Watch Extra Files with the Reloader210~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~211212When using development mode, the reloader will trigger whenever your213Python code or imported modules change. The reloader can watch214additional files with the ``--extra-files`` option, or the215``FLASK_RUN_EXTRA_FILES`` environment variable. Multiple paths are216separated with ``:``, or ``;`` on Windows.217Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
line 218why, one step back218.. tabs::Merge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
line 219why, one step back219Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
line 220why, one step back220 .. group-tab:: BashMerge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
line 221why, one step back221Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
line 222why, one step back222 .. code-block:: textMerge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
line 223why, one step back223Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
lines 224–230why, one step back224 $ flask run --extra-files file1:dirA/file2:dirB/225 # or226 $ export FLASK_RUN_EXTRA_FILES=file1:dirA/file2:dirB/227 $ flask run228 * Running on http://127.0.0.1:8000/229 * Detected change in '/path/to/file1', reloading230Merge branch '2.0.x'David Lordbefore adoptione37e871
lines 231–241why, one step back231 .. group-tab:: Fish232233 .. code-block:: text234235 $ flask run --extra-files file1:dirA/file2:dirB/236 # or237 $ set -x FLASK_RUN_EXTRA_FILES file1 dirA/file2 dirB/238 $ flask run239 * Running on http://127.0.0.1:8000/240 * Detected change in '/path/to/file1', reloading241Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
lines 242–264why, one step back242 .. group-tab:: CMD243244 .. code-block:: text245246 > flask run --extra-files file1:dirA/file2:dirB/247 # or248 > set FLASK_RUN_EXTRA_FILES=file1:dirA/file2:dirB/249 > flask run250 * Running on http://127.0.0.1:8000/251 * Detected change in '/path/to/file1', reloading252253 .. group-tab:: Powershell254255 .. code-block:: text256257 > flask run --extra-files file1:dirA/file2:dirB/258 # or259 > $env:FLASK_RUN_EXTRA_FILES = "file1:dirA/file2:dirB/"260 > flask run261 * Running on http://127.0.0.1:8000/262 * Detected change in '/path/to/file1', reloading263264Merge pull request #4189 from mgu/feat/use-exclude-patterns-reloaderDavid Lordbefore adoption48ee204
lines 265–273why, one step back265Ignore files with the Reloader266~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~267268The reloader can also ignore files using :mod:`fnmatch` patterns with269the ``--exclude-patterns`` option, or the ``FLASK_RUN_EXCLUDE_PATTERNS``270environment variable. Multiple patterns are separated with ``:``, or271``;`` on Windows.272273Merge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
lines 274–320why, one step back274Debug Mode275----------276277Debug mode will be enabled when :envvar:`FLASK_ENV` is ``development``,278as described above. If you want to control debug mode separately, use279:envvar:`FLASK_DEBUG`. The value ``1`` enables it, ``0`` disables it.280281282.. _dotenv:283284Environment Variables From dotenv285---------------------------------286287Rather than setting ``FLASK_APP`` each time you open a new terminal, you can288use Flask's dotenv support to set environment variables automatically.289290If `python-dotenv`_ is installed, running the ``flask`` command will set291environment variables defined in the files :file:`.env` and :file:`.flaskenv`.292This can be used to avoid having to set ``FLASK_APP`` manually every time you293open a new terminal, and to set configuration using environment variables294similar to how some deployment services work.295296Variables set on the command line are used over those set in :file:`.env`,297which are used over those set in :file:`.flaskenv`. :file:`.flaskenv` should be298used for public variables, such as ``FLASK_APP``, while :file:`.env` should not299be committed to your repository so that it can set private variables.300301Directories are scanned upwards from the directory you call ``flask``302from to locate the files. The current working directory will be set to the303location of the file, with the assumption that that is the top level project304directory.305306The files are only loaded by the ``flask`` command or calling307:meth:`~Flask.run`. If you would like to load these files when running in308production, you should call :func:`~cli.load_dotenv` manually.309310.. _python-dotenv: https://github.com/theskumar/python-dotenv#readme311312313Setting Command Options314~~~~~~~~~~~~~~~~~~~~~~~315316Click is configured to load default values for command options from317environment variables. The variables use the pattern318``FLASK_COMMAND_OPTION``. For example, to set the port for the run319command, instead of ``flask run --port 8000``:320Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
line 321why, one step back321.. tabs::Merge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
line 322why, one step back322Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
line 323why, one step back323 .. group-tab:: BashMerge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
line 324why, one step back324Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
lines 325–330why, one step back325 .. code-block:: text326327 $ export FLASK_RUN_PORT=8000328 $ flask run329 * Running on http://127.0.0.1:8000/330Merge branch '2.0.x'David Lordbefore adoptione37e871
lines 331–338why, one step back331 .. group-tab:: Fish332333 .. code-block:: text334335 $ set -x FLASK_RUN_PORT 8000336 $ flask run337 * Running on http://127.0.0.1:8000/338Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
lines 339–354why, one step back339 .. group-tab:: CMD340341 .. code-block:: text342343 > set FLASK_RUN_PORT=8000344 > flask run345 * Running on http://127.0.0.1:8000/346347 .. group-tab:: Powershell348349 .. code-block:: text350351 > $env:FLASK_RUN_PORT = 8000352 > flask run353 * Running on http://127.0.0.1:8000/354Merge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
lines 355–376why, one step back355These can be added to the ``.flaskenv`` file just like ``FLASK_APP`` to356control default command options.357358359Disable dotenv360~~~~~~~~~~~~~~361362The ``flask`` command will show a message if it detects dotenv files but363python-dotenv is not installed.364365.. code-block:: bash366367 $ flask run368 * Tip: There are .env files present. Do "pip install python-dotenv" to use them.369370You can tell Flask not to load dotenv files even when python-dotenv is371installed by setting the ``FLASK_SKIP_DOTENV`` environment variable.372This can be useful if you want to load them manually, or if you're using373a project runner that loads them already. Keep in mind that the374environment variables must be set before the app loads or it won't375configure as expected.376Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
line 377why, one step back377.. tabs::Merge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
line 378why, one step back378Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
line 379why, one step back379 .. group-tab:: BashMerge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
line 380why, one step back380Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
line 381why, one step back381 .. code-block:: textMerge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
line 382why, one step back382Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
lines 383–385why, one step back383 $ export FLASK_SKIP_DOTENV=1384 $ flask run385Merge branch '2.0.x'David Lordbefore adoptione37e871
lines 386–392why, one step back386 .. group-tab:: Fish387388 .. code-block:: text389390 $ set -x FLASK_SKIP_DOTENV 1391 $ flask run392Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
lines 393–407why, one step back393 .. group-tab:: CMD394395 .. code-block:: text396397 > set FLASK_SKIP_DOTENV=1398 > flask run399400 .. group-tab:: Powershell401402 .. code-block:: text403404 > $env:FLASK_SKIP_DOTENV = 1405 > flask run406407Merge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
lines 408–414why, one step back408Environment Variables From virtualenv409-------------------------------------410411If you do not want to install dotenv support, you can still set environment412variables by adding them to the end of the virtualenv's :file:`activate`413script. Activating the virtualenv will set the variables.414Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
line 415why, one step back415.. tabs::Merge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
line 416why, one step back416Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
line 417why, one step back417 .. group-tab:: BashMerge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
line 418why, one step back418Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
line 419why, one step back419 Unix Bash, :file:`venv/bin/activate`::Merge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
line 420why, one step back420Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
line 421why, one step back421 $ export FLASK_APP=helloMerge branch '2.0.x'David Lordbefore adoptione37e871
lines 422–427why, one step back422423 .. group-tab:: Fish424425 Fish, :file:`venv/bin/activate.fish`::426427 $ set -x FLASK_APP helloAdd command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
lines 428–439why, one step back428429 .. group-tab:: CMD430431 Windows CMD, :file:`venv\\Scripts\\activate.bat`::432433 > set FLASK_APP=hello434435 .. group-tab:: Powershell436437 Windows Powershell, :file:`venv\\Scripts\\activate.ps1`::438439 > $env:FLASK_APP = "hello"Merge pull request #3681 from pallets/fix-typoDavid Lordbefore adoption4e854ee
lines 440–720why, one step back440441It is preferred to use dotenv support over this, since :file:`.flaskenv` can be442committed to the repository so that it works automatically wherever the project443is checked out.444445446Custom Commands447---------------448449The ``flask`` command is implemented using `Click`_. See that project's450documentation for full information about writing commands.451452This example adds the command ``create-user`` that takes the argument453``name``. ::454455 import click456 from flask import Flask457458 app = Flask(__name__)459460 @app.cli.command("create-user")461 @click.argument("name")462 def create_user(name):463 ...464465::466467 $ flask create-user admin468469This example adds the same command, but as ``user create``, a command in a470group. This is useful if you want to organize multiple related commands. ::471472 import click473 from flask import Flask474 from flask.cli import AppGroup475476 app = Flask(__name__)477 user_cli = AppGroup('user')478479 @user_cli.command('create')480 @click.argument('name')481 def create_user(name):482 ...483484 app.cli.add_command(user_cli)485486::487488 $ flask user create demo489490See :ref:`testing-cli` for an overview of how to test your custom491commands.492493494Registering Commands with Blueprints495~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~496497If your application uses blueprints, you can optionally register CLI498commands directly onto them. When your blueprint is registered onto your499application, the associated commands will be available to the ``flask``500command. By default, those commands will be nested in a group matching501the name of the blueprint.502503.. code-block:: python504505 from flask import Blueprint506507 bp = Blueprint('students', __name__)508509 @bp.cli.command('create')510 @click.argument('name')511 def create(name):512 ...513514 app.register_blueprint(bp)515516.. code-block:: text517518 $ flask students create alice519520You can alter the group name by specifying the ``cli_group`` parameter521when creating the :class:`Blueprint` object, or later with522:meth:`app.register_blueprint(bp, cli_group='...') <Flask.register_blueprint>`.523The following are equivalent:524525.. code-block:: python526527 bp = Blueprint('students', __name__, cli_group='other')528 # or529 app.register_blueprint(bp, cli_group='other')530531.. code-block:: text532533 $ flask other create alice534535Specifying ``cli_group=None`` will remove the nesting and merge the536commands directly to the application's level:537538.. code-block:: python539540 bp = Blueprint('students', __name__, cli_group=None)541 # or542 app.register_blueprint(bp, cli_group=None)543544.. code-block:: text545546 $ flask create alice547548549Application Context550~~~~~~~~~~~~~~~~~~~551552Commands added using the Flask app's :attr:`~Flask.cli`553:meth:`~cli.AppGroup.command` decorator will be executed with an application554context pushed, so your command and extensions have access to the app and its555configuration. If you create a command using the Click :func:`~click.command`556decorator instead of the Flask decorator, you can use557:func:`~cli.with_appcontext` to get the same behavior. ::558559 import click560 from flask.cli import with_appcontext561562 @click.command()563 @with_appcontext564 def do_work():565 ...566567 app.cli.add_command(do_work)568569If you're sure a command doesn't need the context, you can disable it::570571 @app.cli.command(with_appcontext=False)572 def do_work():573 ...574575576Plugins577-------578579Flask will automatically load commands specified in the ``flask.commands``580`entry point`_. This is useful for extensions that want to add commands when581they are installed. Entry points are specified in :file:`setup.py` ::582583 from setuptools import setup584585 setup(586 name='flask-my-extension',587 ...,588 entry_points={589 'flask.commands': [590 'my-command=flask_my_extension.commands:cli'591 ],592 },593 )594595596.. _entry point: https://packaging.python.org/tutorials/packaging-projects/#entry-points597598Inside :file:`flask_my_extension/commands.py` you can then export a Click599object::600601 import click602603 @click.command()604 def cli():605 ...606607Once that package is installed in the same virtualenv as your Flask project,608you can run ``flask my-command`` to invoke the command.609610611.. _custom-scripts:612613Custom Scripts614--------------615616When you are using the app factory pattern, it may be more convenient to define617your own Click script. Instead of using ``FLASK_APP`` and letting Flask load618your application, you can create your own Click object and export it as a619`console script`_ entry point.620621Create an instance of :class:`~cli.FlaskGroup` and pass it the factory::622623 import click624 from flask import Flask625 from flask.cli import FlaskGroup626627 def create_app():628 app = Flask('wiki')629 # other setup630 return app631632 @click.group(cls=FlaskGroup, create_app=create_app)633 def cli():634 """Management script for the Wiki application."""635636Define the entry point in :file:`setup.py`::637638 from setuptools import setup639640 setup(641 name='flask-my-extension',642 ...,643 entry_points={644 'console_scripts': [645 'wiki=wiki:cli'646 ],647 },648 )649650Install the application in the virtualenv in editable mode and the custom651script is available. Note that you don't need to set ``FLASK_APP``. ::652653 $ pip install -e .654 $ wiki run655656.. admonition:: Errors in Custom Scripts657658 When using a custom script, if you introduce an error in your659 module-level code, the reloader will fail because it can no longer660 load the entry point.661662 The ``flask`` command, being separate from your code, does not have663 this issue and is recommended in most cases.664665.. _console script: https://packaging.python.org/tutorials/packaging-projects/#console-scripts666667668PyCharm Integration669-------------------670671PyCharm Professional provides a special Flask run configuration. For672the Community Edition, we need to configure it to call the ``flask run``673CLI command with the correct environment variables. These instructions674should be similar for any other IDE you might want to use.675676In PyCharm, with your project open, click on *Run* from the menu bar and677go to *Edit Configurations*. You'll be greeted by a screen similar to678this:679680.. image:: _static/pycharm-runconfig.png681 :align: center682 :class: screenshot683 :alt: Screenshot of PyCharms's run configuration settings.684685There's quite a few options to change, but once we've done it for one686command, we can easily copy the entire configuration and make a single687tweak to give us access to other commands, including any custom ones you688may implement yourself.689690Click the + (*Add New Configuration*) button and select *Python*. Give691the configuration a name such as "flask run". For the ``flask run``692command, check "Single instance only" since you can't run the server693more than once at the same time.694695Select *Module name* from the dropdown (**A**) then input ``flask``.696697The *Parameters* field (**B**) is set to the CLI command to execute698(with any arguments). In this example we use ``run``, which will run699the development server.700701You can skip this next step if you're using :ref:`dotenv`. We need to702add an environment variable (**C**) to identify our application. Click703on the browse button and add an entry with ``FLASK_APP`` on the left and704the Python import or file on the right (``hello`` for example). Add an705entry with ``FLASK_ENV`` and set it to ``development``.706707Next we need to set the working directory (**D**) to be the folder where708our application resides.709710If you have installed your project as a package in your virtualenv, you711may untick the *PYTHONPATH* options (**E**). This will more accurately712match how you deploy the app later.713714Click *Apply* to save the configuration, or *OK* to save and close the715window. Select the configuration in the main PyCharm window and click716the play button next to it to run the server.717718Now that we have a configuration which runs ``flask run`` from within719PyCharm, we can copy that configuration and alter the *Script* argument720to run a different CLI command, e.g. ``flask shell``.