Flask Command Line Interface (CLI)
Share:
Flask, a popular micro web framework in Python, comes equipped with its own command-line interface, or CLI. This allows you to perform a range of tasks like running your application, initiating database migrations, and handling custom commands, all through the command line. This article offers an in-depth tutorial on Flask Command Line Interface (CLI), complete with narrative explanations and illustrative code snippets featuring characters and features from the movies.
When working in a Flask application, the CLI provides two built-in commands: "run" and "shell". The "run" command starts the local development server, while the "shell" command opens an interactive Python shell on the terminal. When invoked, both commands load the application context, giving you access to your application’s variables.
Let's set up a basic Flask application to illustrate these commands. We will create a simple Flask application named "movie_app". This application will display the list of actors of a particular movie. Let's consider the movie to be "Jurassic Park".
You can initialize the application by making a new script, app.py
.
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/actors')
def actors():
return jsonify({'actors': 'Sam Neill, Laura Dern, Jeff Goldblum'})
To run the app, use the command flask run
in your terminal:
$ export FLASK_APP=app.py
$ flask run
The export FLASK_APP=app.py
command tells Flask where to find your application, while flask run
starts the development server.
When using the flask shell
command, you’re presented with an interactive Python shell where you can interact with your application’s context:
$ flask shell
You can customize the Flask CLI by adding your own commands. This involves creating a new Command
object, which takes two parameters: the command name (string
) and a callback
that executes when the command is run.
Assume we want to add a "credits" command to display the director and producer of the movie. We begin by importing the click
module, which provides functionality for creating command-line interfaces. Then, we add a command to our application's instance through the cli
object.
import click
from flask.cli import with_appcontext
@app.cli.command('credits')
@with_appcontext
def credits_command():
click.echo('Director: Steven Spielberg')
click.echo('Producer: Kathleen Kennedy')
Now, when we go to the terminal, we can access our new command.
$ flask credits
Director: Steven Spielberg
Producer: Kathleen Kennedy
We can also provide arguments with our custom commands. Let's say we want to fetch the details of a particular character from the movie "Jurassic Park". For this, we create a new command named "character" which takes a name argument.
@app.cli.command('character')
@click.argument('name')
@with_appcontext
def character_command(name):
characters = {
'alangrant': 'A paleontologist',
'elliesattler': 'A paleobotanist',
'ianspence': 'A chaotician'
}
character = characters.get(name.lower())
if character:
click.echo(f'The character {name} is {character}.')
else:
click.echo('This character does not exist in Jurassic Park')
For example, if we want to fetch information about the character "AlanGrant", we would do:
$ flask character AlanGrant
The character AlanGrant is A paleontologist.
Flask CLI also provides an Option
decorator for adding options to your command. Suppose we want to fetch the movie's box office earnings, and we have an option for the currency in which the earnings will be shown. We can set the default value of currency to "$".
@app.cli.command('boxoffice')
@click.option('--currency', default='$')
@with_appcontext
def boxoffice_command(currency):
click.echo(f'The movie made {currency}1 Billion at the box office.')
The box office earnings can be fetched by:
$ flask boxoffice
The movie made $1 Billion at the box office.
$ flask boxoffice --currency='£'
The movie made £1 Billion at the box office.
To wrap things up, the Flask Command Line Interface (CLI) provides an efficient, user-friendly way of interacting with your application directly from the command line. The CLI can be extended to suit your needs, giving you the power to define the commands and options you need for your application. It's an integral part of Flask that adds automated functionality and simplicity to your workflow. Whether you're starting the local server, fetching movie character details, or displaying box office numbers, Flask has you covered!
0 Comment
Sign up or Log in to leave a comment