Flask CLI Commands

Flask Tip - CLI Commands

The flask command is written using Click. Click can be used to create sub-commands for the flask command.

For example, you can create a CLI command for initializing the database:

from click import echo

def initialize_database():
    """Initialize the SQLite database."""
    click.echo('Initialized the SQLite database!')

# in terminal
$ flask init_db

Custom CLI commands are automatically included in the help information:

$ flask --help

  init_db  Initialize the SQLite database.
  run      Run a development server.

The Flask-Migrate package is a great example of using custom CLI commands.