Flask CLI Commands
Flask Tip - CLI Commands
The
flask
command is written using Click. Click can be used to create sub-commands for theflask
command.For example, you can create a CLI command for initializing the database:
from click import echo @app.cli.command('init_db') def initialize_database(): """Initialize the SQLite database.""" database.drop_all() database.create_all() click.echo('Initialized the SQLite database!') # in terminal $ flask init_db
Custom CLI commands are automatically included in the help information:
$ flask --help Commands: init_db Initialize the SQLite database. run Run a development server.
The Flask-Migrate package is a great example of using custom CLI commands.