当前位置:网站首页>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 aview_functions
which is a dictionary storingendpoint-view_func
key-value pair.The first function ofadd_url_rule
is to add key-value pairs toview_functions
(this happens in the applicationrun
already done before) - Each application
app
has aurl_map
, which is aMap
class (implemented inwerkzeug/routing.py
), which contains a list whose elements are instances ofRole
(inwerkzeug/routing.py
).The second role ofadd_url_rule
is to add an instance ofRole
tourl_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.
边栏推荐
猜你喜欢
随机推荐
pc端ncnn搭建与测试
弹性盒样式、移动端、VW适配、响应式布局
LeetCode·每日一题·761.特殊的二进制序列·分治
主键id,Snowflake雪花算法,优点:生成有顺序的id,提高数据库的性能
Web 3D渲染引擎HOOPS Communicator动画编辑器的使用 | HOOPS教程
(二)、灰色预测模型(GM1,1)
Forest Program DFS + tanjar cactus
web基本概念
C语言笔记 学习预处理 学习宏定义
引导过程与服务控制
resourcemanager启动失败,别的节点成功
毕业我选择了保家卫国,退伍我选择了华为外包
3D软件开发工具HOOPS全套产品开发介绍 | HOOPS Exchange、HOOPS Communicator
如何生成dll文件 采用VS2017生成dll文件(动态库文件)和lib文件(静态库文件)以C语言为例
74HC595 chip pin description
SQL存储过程
.net(一)WebService创建
ncnn 推理猫狗识别
【机器学习】随机森林、GBDT、XGBoost、LightGBM等集成学习代码练习
【Rust指南】快速入门|开发环境|hello world