Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Flask Command-Line API

Flask Command-Line API

Flask builds its command-line tools on Click.

Use the flask command to run applications and manage tasks.

All CLI classes are in the flask.cli module.


FlaskGroup

Flask's main command group. Used via the flask command.

Parameters Description
add_default_commands=True Whether to add default commands such as run, shell, and routes
create_app=None Application factory function that returns a Flask instance
add_version_option=True Whether to add the --version option
load_dotenv=True Whether to load .env and .flaskenv files
set_debug_flag=True Whether to set the debug flag based on environment variables

AppGroup

Base class for the CLI command groups of applications and blueprints. Add custom commands via app.cli or bp.cli.

Method Description
command(*args, **kwargs) Register a CLI command. Automatically wraps with_appcontext by default
group(*args, **kwargs) Register a subgroup of commands. Uses AppGroup as the group class by default

AppGroup.command() is enabled by default with_appcontext=True, meaning your command function automatically has an application context. If not needed, pass with_appcontext=False。


ScriptInfo

Stores information about the script's runtime for loading the application. Usually no manual operation is needed.

property Description
app_import_path Application import path (the value of the --app option)
create_app Application factory function
data Any additional data dictionary
load_app() Load and return a Flask application instance

Utility functions

function Description
with_appcontext(f) Decorator that ensures the decorated function executes within an application context
pass_script_info(f) Decorator that passes the ScriptInfo instance as the first argument to the function
load_dotenv(path) Load environment variables from the .env file

Default commands

Command Description Common parameters
flask run Start the Dev Server --host, --port, --debug, --reload/--no-reload
flask shell Start an interactive Python shell Automatically load context variables such as app and g
flask routes List all registered routes --sort (endpoint/methods/domain/rule/match), --all-methods

Standard options

Option Description
-A / --app IMPORT Specify the application module and instance, such as "app:create_app()"
--debug / --no-debug Enable/disable debug mode
-e / --env-file FILE Specify the .env file path
--version Display Flask and dependency versions

Code Example

Example

from

flask

import

Flask

app

=

Flask

(

name

)

Registering custom commands

@

app.

cli

.

command

(

"init-db"

)

def

init_db_command

(

)

:

   

"""Initialize the database"""

   

print

(

"Database initialized."

)

Register a command with parameters

@

app.

cli

.

command

(

"greet"

)

@

app.

cli

.

option

(

"--name"

,

"-n"

,

default

=

"RUNOOB"

,

help

=

"greeting object"

)

def

greet_command

(

name

)

:

   

"""Send a greeting"""

   

print

(

f

"Hello, {name}!"

)

Register a command subgroup

user_cli

=

app.

cli

.

group

(

"user"

)

@

user_cli.

command

(

"list"

)

def

list_users

(

)

:

   

"""List all users"""

   

print

(

"Listing users..."

)

@

user_cli.

command

(

"create"

)

@

user_cli.

option

(

"--username"

,

required

=

True

)

def

create_user

(

username

)

:

   

"""Create a new user"""

   

print

(

f

"Creating user: {username}"

)

Usage:

flask init-db

flask greet --name World

flask user list

flask user create --username runoob