当前位置:网站首页>Introduction to the Endpoint

Introduction to the Endpoint

2022-08-09 08:02:00 scwMason

When I first learned that EndPoint was learning Flask-restful, after reading some articles, I felt that I still had to understand this from the pointing process between flask's url and function. I always thought:

@app.route("/index")def index:pass

When we entered /index, we thought that we would find the index function directly and execute the content inside, but it was not.

In the source code we can find:

  • Each application app has a view_functions which is a dictionary storing endpoint-view_funckey-value pair.The first function of add_url_rule is to add key-value pairs to view_functions (this happens in the application runalready done before)
  • Each application app has a url_map, which is a Map class (implemented inwerkzeug/routing.py), which contains a list whose elements are instances of Role (in werkzeug/routing.py).The second role of add_url_rule is to add an instance of Role to url_map (it is also in the application >run is done before)
    We can see it with an example
from flask import Flaskapp=Flask(__name__)@app.route('/test', endpoint='Test')def test():[email protected]('/', endpoint='index')def hello_world():return 'Hello World!'if __name__ == '__main__':print(app.view_functions)print(app.url_map)app.run()

We found that url_map corresponds to the mapping between url and endpoint

So the endpoint is like the id of a url, and this id can represent the url

Why not just use the view function directly?

The question now is: "Why do we need this extra layer (endpoint)? Why map paths to endpoints, and then map endpoints to view functions? Why not skip this intermediate step?

The reason is because this way is more powerful.For example, Flask Blueprints allow you to split your application into different parts.I might put all admin-side resources in a blueprint called "admin" and all user-level resources in an endpoint of "user".

Blueprints allow you to separate these into namespaces.For example...
main.py:

from flask import Flask, Blueprintfrom admin import adminfrom user import userapp = Flask(__name__)app.register_blueprint(admin, url_prefix='admin')app.register_blueprint(user, url_prefix='user')

admin.py:

admin = Blueprint('admin', __name__)@admin.route('/greeting')def greeting():return 'Hello, administrative user!'

user.py:

user = Blueprint('user', __name__)@user.route('/greeting')def greeting():return 'Hello, lowly normal user!'

Note that in both blueprints, the "/greeting" route is a function called "greeting".If I want to refer to admin's "greeting" function, we need to use "admin.greeting".endpoint allows us to specify the name of the blueprint as part of the endpoint, thus implementing a kind of namespace.So, we can use the following approach:

print url_for('admin.greeting') # Prints '/admin/greeting'print url_for('user.greeting') # Prints '/user/greeting'

Reference article: https://www.cnblogs.com/eric-nirnava/p/endpoint.html

        https://www.jianshu.com/p/0611f044efc4

By Alex_Dj
Link: https://www.jianshu.com/p/0611f044efc4
Source: Jianshu
The copyright of Jianshu belongs to the author. For any form of reprint, please contact the author for authorization and indicate the source.

原网站

版权声明
本文为[scwMason]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208090757055143.html