First choice: What is the Flask Blueprint?

Flask runs web applications by instantiating the object app.run(), which defines a lot of view functions in a.py file, which is not convenient for code management, but also bad for later maintenance. Hence the emergence of Blueprints in Flask, which allow programs to be modular, like modules in Django that can be programmed independently. The Blueprint fulfills this function. Simply put, Blueprint is a container that stores methods for operations that can be invoked once the Blueprint is registered with an application. Flask uses Blueprint to organize URLs and process requests. Flask uses Blueprint to modularize applications.

In Flask, Blueprint has the following properties:

An application can have multiple Blueprints. An application can register a Blueprint with any unused URL such as “/”, “/sample”, or subdomain in an application. A module can register Blueprint multiple times. Blueprint can have its own templates, static files, or other common methods of operation. Blueprint does not have to implement the application’s views and functions. However, a Blueprint is not a complete application. It cannot run independently of the application. Instead, it must be registered with an application.

Operation mechanism

A blueprint is a set of operations that can be performed on an application object in the future. Registering a route is an operation that will modify the object’s URL_MAP routing table when the route decorator is called on the application object to register a route. However, the blueprint object has no routing table at all. When we call the Route decorator on the Blueprint object to register the route, it simply adds an entry to the internal defered_functions list of defered_functions. When executing the register_print () method of the application object, The application object will take each entry from the list of defered_functions of the blueprint object and execute the anonymous function with itself as an argument, calling the application object’s add_url_rule() method, which will actually modify the application’s routing table.

The URL prefix of the blueprint

When registering a blueprint on the application object, we can specify a URL_PREFIX keyword parameter (the default is /). In the application’s final routing table URL_MAP, the route URLs registered on the blueprint are automatically prefixed with this prefix. This ensures that the same URL rules can be used across multiple blueprints without ending up in conflict, as long as different blueprints are hooked up to different self-paths when registering the blueprints.

Register a static route

Unlike application objects, Blueprint objects do not register routes for static directories by default when they are created. We need to specify the static_folder parameter at creation time. The following example sets the static_admin directory under the directory where the blueprint is located as a static directory

admin = Blueprint("admin",__name__,static_folder='static_admin')
app.register_blueprint(admin,url_prefix='/admin')

You can now use /admin/static_admin to access static files in the static_admin directory to customize the static directory URL rules: you can use the static_url_path to change the routing of the static directory when creating the blueprint object. The following example sets the route to /lib for the static_admin folder

admin = Blueprint("admin",__name__,static_folder='static_admin',static_url_path='/lib')
app.register_blueprint(admin,url_prefix='/admin')

Set the template directory

The default template directory for Blueprint objects is the system’s template directory. You can set the template directory with the Template_folder keyword parameter when creating Blueprint objects

admin = Blueprint('admin',__name__,template_folder='my_templates')

NOTE: If a file with the same name as “MY_TEMPLATES” exists in templates, then the system will use the file in templates preferentially. If you use a “TEMPLATES” directory with the same name, you will need to use a path, such as:

There are two my_templates. If you use the my_templates directory in the admin directory, you need to register it as follows:

admin = Blueprint('admin',__name__,template_folder='admin/my_templates')