In this article, let's take a look at the Blueprint blueprint in Flask, what blueprint..It is just an extension of sub-modules, which is used to divide different business module apis into different python files, which is similar to @RequestMapping("/") at the class level of Spring mvc..
Why introduce Blueprints?Because if the API interface is written into a py file by default, the file will become more and more complicated, and it will become more and more bloated and unmaintainable. Therefore, according to the normal development logic, it is necessary to divide the modules, so it is necessary to introduce Blueprint
You need to use pip to install the Blueprint extension in the environment first
pip install Blueprint
from flask import Flaskapp = Flask(__name__)@app.route('/')def helloworld():return 'Hello world python flask'# The new version should not support this startup# if __name__ == '__main__':# app.run()
According to the business, you can create a new module and then register the route through Blueprint
from flask import Blueprintuser = Blueprint('user',__name__)@user.route('/user/username')def username():return 'get username : johnny'
Register the newly created Blueprint in the entry file
from flask import Flask#Introduce the blueprint just createdfrom user import userapp = Flask(__name__)#Register to flask appapp.register_blueprint(user)@app.route('/')def helloworld():return 'Hello world python flask'# if __name__ == '__main__':# app.run()
Access: /user/username gets the following:
Access: / Get as follows:
Using Blueprint is easy follow the process below
pip install Blueprint
Create a new module file and create a Blueprint object in it, such as:
# Blueprint two parameters ('blueprint name', blueprint location')user = Blueprint('user',__name__)
Blueprint is registered to the Flask app
#Introduce the blueprint just createdfrom user import userapp = Flask(__name__)app.register_blueprint(user)
Blueprint can be understood as Spring mvc's Class-level @RequestMapping("/user") is similar..
Welcome to my personal blogJohnny's house
Welcome to follow my personal accountp>