cli.rst
This file
Path
docs/cli.rst
130 lines · 4.1 KiB
Last changed by
Updated documentation once more for new cli.Armin Ronacherbefore adoptiona3ad540
History of this fileTake it
Added missing documentation pageArmin Ronacherbefore adoption9841420
lines 1–101why, one step back1.. _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 dbUpdated documentation once more for new cli.Armin Ronacherbefore adoptiona3ad540
lines 102–130why, one step back102103Factory Functions104-----------------105106In case you are using factory functions to create your application (see107:ref:`app-factories`) you will discover that the ``flask`` command cannot108work with them directly. Flask won't be able to figure out how to109instanciate your application properly by itself. Because of this reason110the recommendation is to create a separate file that instanciates111applications.112113For instance if you have a factory function that creates an application114from a filename you could make a separate file that creates such an115application from an environment variable.116117For instance this could be a file named ``autoapp.py`` with these118contents::119120 import os121 from yourapplication import create_app122 app = create_app(os.environ['YOURAPPLICATION_CONFIG'])123124Once this has happened you can make the flask command automatically pick125it up::126127 export YOURAPPLICATION_CONFIG=/path/to/config.cfg128 export FLASK_APP=/path/to/autoapp.py129130From this point onwards ``flask`` will find your application.