Flask Command Line API
Flask builds command line tools based on Click.
Run applications and manage tasks through the flask command.
All CLI classes are in the flask.cli module.
FlaskGroup
The main command group for Flask. Used through the flask command.
| Parameter | Description |
|---|---|
| add_default_commands=True | Whether to add default commands like run, shell, routes |
| create_app=None | Application factory function, returns Flask instance |
| add_version_option=True | Whether to add --version option |
| load_dotenv=True | Whether to load .env and .flaskenv files |
| set_debug_flag=True | Whether to set debug flag based on environment variable |
AppGroup
The base class for CLI command groups of applications and blueprints. Add custom commands through app.cli or bp.cli.
| Method | Description |
|---|---|
| command(*args, **kwargs) | Register CLI command. Automatically wraps with appcontext by default |
| group(*args, **kwargs) | Register subcommand group. Uses AppGroup as group class by default |
AppGroup.command() enables the with_appcontext decorator by default. If the command does not need application context, you can disable it with with_appcontext=False.
Example - Register command:
@app.cli.command('create-db')
def create_db_command():
"""Creates the database."""
db.create_all()
click.echo('Database created.')
Example - Register command group:
@app.cli.group()
def translate():
"""Translation and localization commands."""
pass
@translate.command()
def hello():
"""Says hello in English."""
click.echo('Hello from Flask.')
ScriptInfo
Stores script information. Contains application factory function path, debug mode, etc.
| Attribute | Description |
|---|---|
| app_factory | Application factory function path |
| debug | Whether debug mode is enabled |
| use_debugger | Whether to use debugger |
| use_reloader | Whether to use reloader |
| extra_files | Additional files to watch for changes |
load_dotenv
Load environment variables from .env and .flaskenv files.
def load_dotenv():
"""Load :file:`.env` and :file:`.flaskenv` files."""
# Implementation...
run_command
The default command to start the development server.
@click.command('run')
@click.option('--host', default='127.0.0.1', help='Host to listen on.')
@click.option('--port', default=5000, help='Port to listen on.')
@click.option('--reload', is_flag=True, help='Enable auto-reload.')
@click.option('--debug', is_flag=True, help='Enable debug mode.')
@click.option('--threaded', is_flag=True, help='Enable threaded mode.')
@click.option('--processes', default=1, help='Number of processes.')
def run_command(host, port, reload, debug, threaded, processes):
"""Runs the local development server."""
# Implementation...
shell_command
The command to start an interactive Python shell.
@click.command('shell')
@click.option('--browser', '-b', is_flag=True,
help='Open a browser after starting the shell.')
@click.option('--make-context', '-m', is_flag=True,
help='Create a context automatically.')
def shell_command(browser, make_context):
"""Runs an interactive Python shell in the context of the app."""
# Implementation...
routes_command
The command to display all registered routes.
@click.command('routes')
@click.optio
YouTip