Flask is a lightweight Web framework. Although it is lightweight, but for the components of a large, modular application is also able to achieve , "blueprint" is such an implementation. For the implementation of modular applications in Flask 0.2 version of the design. This article does not make a detailed introduction to the "blueprint", but first from the 0.2 version of the Module class implementation. In fact, the implementation of the "blueprint" and the Module class implementation is very similar.
Why Modular Applications
For large applications, the size of the entire application will increase as the functionality continues to grow. Modularizing different parts of the application according to certain rules not only makes the whole application logical and clear, but also easy to maintain. For example, in Flask, you might want to build a simple project as follows:
/myapplication /__init__.py /views /__init__.py / /
In the above directory structure, we have modified the previous Flask single file into an application package, with all view functions under views and divided into admin and frontend sections according to their functions. In order to realize this modular application construction, in version 0.2 Flask implements the Module class. Instances of this class can be added to a Flask application after it is created by registering it.
The Module class implements a number of methods:
•route(rule, **options)
•add_url_rule(rule, endpoint, view_func=None, **options)
•before_request(f)
•before_app_request(f)
•after_request(f)
•after_app_request(f)
•context_processor(f)
•app_context_processor(f)
•_record(func)
All of the above methods can be used as decorators in their own modules, except for add_url_rule and _record, which all return a function. The function returned by the decorator can be put into _register_events by calling the _record method. When the Flask application is created, this module can be registered with the application by running the function in the _register_events list.
How to register a Module for a Flask application
Below we have an example of how a Flask application can register a Module.
1. Project structure
This simple example project is structured as follows:
/myapplication /__init__.py / /views /__init__.py / /
and the code for the two modules is as follows:
# from flask import Module admin = Module(__name__) @('/') def index(): return "This is admin page!" @('/profile') def profile(): return "This is profile page."
# from flask import Module blog = Module(__name__) @('/') def index(): return "This is my blog!" @('/article/<int:id>') def article(id): return "The article id is %d." % id
In the above two modules, we first created a Module class for each of them and then added some rules for each of them like writing a normal view function. After that, you can register these modules by bringing them in when you create your Flask app.
# from flask import Flask from import admin from import blog app = Flask(__name__) @('/') def index(): return "This is my app." app.register_module(blog, url_prefix='/blog') app.register_module(admin, url_prefix='/admin') if __name__ == '__main__': from import run_simple run_simple('localhost', 5000, app)
In the middle:
-We first introduced the admin and blog Module objects;
-After that, we created a Flask application app and added a view function to this app;
-To register the module, we call the register_module method of the application;
-Finally, from this we call the run_simple method, which is used to create a local server for testing this Flask application.
Following the above steps, we can test the application. With /blog and /admin as URL prefixes, respectively, you can access the blog and admin modules.
2. What happens when you register a Module?
Based on the above example, you can register a Module by simply calling the register_module method of the Flask application. The code about the register_module method is as follows:
def register_module(self, module, **options): """Registers a module with this application. The keyword argument of this function are the same as the ones for the constructor of the :class:`Module` class and will override the values of the module if provided. """ ('url_prefix', module.url_prefix) state = _ModuleSetupState(self, **options) for func in module._register_events: func(state)
This can be found through the above code:
-Can distinguish between different Modules by adding url_prefix, as we have seen when registering admin and blog in the app;
-When registering, we create a class _ModuleSetupState which receives the Flask application and some parameters to generate a state instance. This instance reflects the current state of the Flask application.
-When we talked about the Module class earlier, we said that Module will put some of its own module's function implementations in the _register_events list when it is not registered, and these function implementations are all in the form of functions. When you need to register a module to an application, you only need to pass parameters with information about the application, i.e. the state instance above. This way, by running the function, you can bind some properties to the current application.
Taking the URL binding of the different modules in the above example, through registration, the application app now forms a URL "map" as follows:
>>> app.url_map Map([<Rule '/admin/profile' (HEAD, GET) -> >, <Rule '/admin/' (HEAD, GET) -> >, <Rule '/blog/' (HEAD, GET) -> >, <Rule '/' (HEAD, GET) -> index>, <Rule '/blog/article/<id>' (HEAD, GET) -> >, <Rule '/static/<filename>' (HEAD, GET) -> static>] ) >>> app.url_map._rules_by_endpoint {'': [<Rule '/admin/' (HEAD, GET) -> >], '': [<Rule '/admin/profile' (HEAD, GET) -> >], '': [<Rule '/blog/article/<id>' (HEAD, GET) -> >], '': [<Rule '/blog/' (HEAD, GET) -> >], 'index': [<Rule '/' (HEAD, GET) -> index>], 'static': [<Rule '/static/<filename>' (HEAD, GET) -> static>] } >>> app.view_functions {'': <function >, '': <function >, '': <function >, '': <function >, 'index': <function __main__.index> }
In this way, URL rules for different modules can be put together and correspondences can be formed between endpoint and view functions. For more information on URL handling in Flask applications, see: URL Handling in Flask Applications.