21. Command Line Interface

class flask.cli.FlaskGroup(add_default_commands=True, create_app=None, **extra)[source]

Special subclass of the AppGroup group that supports loading more commands from the configured Flask app. Normally a developer does not have to interface with this class but there are some very advanced use cases for which it makes sense to create an instance of this.

For information as of why this is useful see Custom Scripts.

Parameters:
  • add_default_commands – if this is True then the default run and shell commands wil be added.
  • create_app – an optional callback that is passed the script info and returns the loaded app.
class flask.cli.AppGroup(name=None, commands=None, **attrs)[source]

This works similar to a regular click Group but it changes the behavior of the command() decorator so that it automatically wraps the functions in with_appcontext().

Not to be confused with FlaskGroup.

command(*args, **kwargs)[source]

This works exactly like the method of the same name on a regular click.Group but it wraps callbacks in with_appcontext() unless it’s disabled by passing with_appcontext=False.

group(*args, **kwargs)[source]

This works exactly like the method of the same name on a regular click.Group but it defaults the group class to AppGroup.

class flask.cli.ScriptInfo(app_import_path=None, create_app=None)[source]

Help object to deal with Flask applications. This is usually not necessary to interface with as it’s used internally in the dispatching to click. In future versions of Flask this object will most likely play a bigger role. Typically it’s created automatically by the FlaskGroup but you can also manually create it and pass it onwards as click object.

app_import_path = None

Optionally the import path for the Flask application.

create_app = None

Optionally a function that is passed the script info to create the instance of the application.

data = None

A dictionary with arbitrary data that can be associated with this script info.

load_app()[source]

Loads the Flask app (if not yet loaded) and returns it. Calling this multiple times will just result in the already loaded app to be returned.

flask.cli.with_appcontext(f)[source]

Wraps a callback so that it’s guaranteed to be executed with the script’s application context. If callbacks are registered directly to the app.cli object then they are wrapped with this function by default unless it’s disabled.

flask.cli.pass_script_info(f)

Marks a function so that an instance of ScriptInfo is passed as first argument to the click callback.

flask.cli.run_command = <click.core.Command object>

Runs a local development server for the Flask application.

This local server is recommended for development purposes only but it can also be used for simple intranet deployments. By default it will not support any sort of concurrency at all to simplify debugging. This can be changed with the –with-threads option which will enable basic multithreading.

The reloader and debugger are by default enabled if the debug flag of Flask is enabled and disabled otherwise.

flask.cli.shell_command = <click.core.Command object>

Runs an interactive Python shell in the context of a given Flask application. The application will populate the default namespace of this shell according to it’s configuration.

This is useful for executing small snippets of management code without having to manually configuring the application.