当前位置:网站首页>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:passWhen 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
apphas aview_functionswhich is a dictionary storingendpoint-view_funckey-value pair.The first function ofadd_url_ruleis to add key-value pairs toview_functions(this happens in the applicationrunalready done before) - Each application
apphas aurl_map, which is aMapclass (implemented inwerkzeug/routing.py), which contains a list whose elements are instances ofRole(inwerkzeug/routing.py).The second role ofadd_url_ruleis to add an instance ofRoletourl_map(it is also in the application>runis 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.
边栏推荐
猜你喜欢
随机推荐
3D精彩案例,清软英泰建成综合轻量化显示平台!
图像处理(一)图像基础
(三)、时间序列预测
Collection 接口 & List 接口
传输层协议介绍
练习电影卡片、过渡、动画、变形、旋转,练习时钟、立方体、缩放
yolov5 detects the number of labels in the dataset
账户和权限管理2
C language: detailed explanation of soda bottle
“互联网+”大学生创新创业大赛经历
收藏!Solidworks从设计到制造流程解决方案 2022来了!
工信部等四部门推动绿色智能家居产品下乡
网络布线及数制转换
【机器学习】中国大学慕课《机器学习》课后习题(二)(回归)
Luogu P1110 report statistics multiset stl good question
定时任务组件Quartz
74HC595芯片引脚说明
Solidworks 2022 Inspection新增功能:光学字符识别、可自定义的检查报告
【机器学习】降维代码练习
接口测试概念









