Flask Command Line Interface

FlaskFlaskBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, you will learn how to use the Flask Command Line Interface (CLI) to manage your Flask application. The Flask CLI provides a set of commands that can help you run the development server, create custom commands, and more.

Note: You need to create the code file yourself and run it in the environment. You can preview the Flask service status on Web 5000.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL flask(("`Flask`")) -.-> flask/CoreConceptsGroup(["`Core Concepts`"]) flask(("`Flask`")) -.-> flask/DevelopmentToolsGroup(["`Development Tools`"]) flask(("`Flask`")) -.-> flask/DataHandlingGroup(["`Data Handling`"]) flask/CoreConceptsGroup -.-> flask/application_object("`Application Object`") flask/DevelopmentToolsGroup -.-> flask/blueprint_objects("`Blueprint Objects`") flask/DataHandlingGroup -.-> flask/incoming_request_data("`Incoming Request Data`") flask/DataHandlingGroup -.-> flask/response_objects("`Response Objects`") flask/CoreConceptsGroup -.-> flask/sessions("`Sessions`") flask/CoreConceptsGroup -.-> flask/useful_internals("`Useful Internals`") flask/DevelopmentToolsGroup -.-> flask/command_line_interface("`Command Line Interface`") subgraph Lab Skills flask/application_object -.-> lab-136105{{"`Flask Command Line Interface`"}} flask/blueprint_objects -.-> lab-136105{{"`Flask Command Line Interface`"}} flask/incoming_request_data -.-> lab-136105{{"`Flask Command Line Interface`"}} flask/response_objects -.-> lab-136105{{"`Flask Command Line Interface`"}} flask/sessions -.-> lab-136105{{"`Flask Command Line Interface`"}} flask/useful_internals -.-> lab-136105{{"`Flask Command Line Interface`"}} flask/command_line_interface -.-> lab-136105{{"`Flask Command Line Interface`"}} end

Install Flask

Before getting started, make sure you have Flask installed in your Python environment. You can install Flask using pip:

pip install flask

Create a Flask Application

Create a new Python file named app.py and add the following code to create a basic Flask application:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello, Flask!'

if __name__ == '__main__':
    app.run()

Save the file and execute it using the following command in your terminal:

python app.py

Run the Development Server

Instead of manually running the Flask application using python app.py, you can use the Flask CLI to start the development server. Stop the currently running application (if any) and execute the following command:

flask run

You should see the Flask development server start and display the URL where your application is running (usually http://127.0.0.1:5000/). Open that URL in your browser and you should see the "Hello, Flask!" message.

Create a Custom Command

The Flask CLI allows you to create custom commands that can be executed from the command line. Let's create a custom command named greet that takes a name as an argument and prints a greeting message.

Create a new Python file named commands.py and add the following code:

import click

@click.command()
@click.argument('name')
def greet(name):
    click.echo(f'Hello, {name}!')

if __name__ == '__main__':
    greet()

Save the file and execute it using the following command:

python commands.py John

You should see the message "Hello, John!" printed in the terminal.

Register Commands with the Flask Application

To make your custom commands available through the Flask CLI, you need to register them with your Flask application. Open the app.py file and modify it as follows:

from flask import Flask
from commands import greet

app = Flask(__name__)
app.cli.add_command(greet)

@app.route('/')
def hello():
    return 'Hello, Flask!'

if __name__ == '__main__':
    app.run()

Save the file and restart the Flask development server using the flask run command. Now you can execute your custom command greet from the command line:

flask greet John

You should see the message "Hello, John!" printed in the terminal.

Summary

In this lab, you learned how to use the Flask Command Line Interface (CLI) to manage your Flask application. You learned how to run the development server, create custom commands, and register commands with your Flask application. The Flask CLI provides a convenient way to interact with your Flask application from the command line, making it easier to manage and test your application.