Skip to content
Myr

Added missing documentation page

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 67e4d3e · Browse files · 1 file+101 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+101 added−0 removed

  • other:+101 added−0 removed

1 file+101 added−0 removed

added:docs/cli.rst+101 added
1.. _cli:23Command Line Interface4======================56.. versionadded:: 1.078.. currentmodule:: flask910One of the nice new features in Flask 1.0 is the built-in integration of11the `click <http://click.pocoo.org/>`_ command line interface. This12enables a wide range of new features for the Flask ecosystem and your own13applications.1415Basic Usage16-----------1718After installation of Flask you will now find a ``flask`` script installed19into your virtualenv. If you don't want to install Flask or you have a20special use-case you can also use ``python -mflask`` to accomplish exactly21the same.2223The way this script works is by providing access to all the commands on24your Flask application's :attr:`Flask.cli` instance as well as some25built-in commands that are always there. Flask extensions can also26register more commands there if they so desire.2728For the ``flask`` script to work, an application needs to be discovered.29The two most common ways are either an environment variable30(``FLASK_APP``) or the ``--app`` / ``-a`` parameter. It should be the31import path for your application or the path to a Python file. In the32latter case Flask will attempt to setup the Python path for you33automatically and discover the module name but that might not always work.3435In that imported file the name of the app needs to be called ``app`` or36optionally be specified after a colon.3738Given a ``hello.py`` file with the application in it named ``app`` this is39how it can be run.4041Environment variables (On Windows use ``set`` instead of ``export``)::4243 export FLASK_APP=hello44 flask run4546Parameters::4748 flask --app=hello run4950File names::5152 flask --app=hello.py run5354Virtualenv Integration55----------------------5657If you are constantly working with a virtualenv you can also put the58``export FLASK_APP`` into your ``activate`` script by adding it to the59bottom of the file. That way every time you activate your virtualenv you60automatically also activate the correct application name.6162Debug Flag63----------6465The ``flask`` script can be run with ``--debug`` or ``--no-debug`` to66automatically flip the debug flag of the application. This can also be67configured by setting ``FLASK_DEBUG`` to ``1`` or ``0``.6869Running a Shell70---------------7172To run an interactive Python shell you can use the ``shell`` command::7374 flask --app=hello shell7576This will start up an interactive Python shell, setup the correct77application context and setup the local variables in the shell. This is78done by invoking the :meth:`Flask.make_shell_context` method of the79application. By default you have access to your ``app`` and :data:`g`.8081Custom Commands82---------------8384If you want to add more commands to the shell script you can do this85easily. Flask uses `click`_ for the command interface which makes86creating custom commands very easy. For instance if you want a shell87command to initialize the database you can do this::8889 from flask import Flask9091 app = Flask(__name__)9293 @app.cli.command()94 def initdb():95 """Initialize the database."""96 print 'Init the db'9798The command will then show up on the command line::99100 $ flask -a hello.py initdb101 Init the db