当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
教你更好的使用 idea 2021.2.3
BIM技术多牛逼?BIM技术在建筑工程行业的四大发展趋势
Native JDBC operation database
练习电影卡片、过渡、动画、变形、旋转,练习时钟、立方体、缩放
【机器学习】降维代码练习
Unity 3D模型展示框架篇之资源打包、加载、热更(二)
LeetCode·每日一题·761.特殊的二进制序列·分治
Selenium测试案例一步步学之(2)Selenium自动测试脚本模块化(下)
浅谈Flask_script
可能导致Loadrunner检查点中savecount为0的分析
Shell--常用小工具(sort、uniq、tr、cut)
安全的Md5加密:两次加密(加盐)
【机器学习】随机森林、GBDT、XGBoost、LightGBM等集成学习代码练习
世界顶尖3D Web端渲染引擎:HOOPS Communicator技术介绍(一)
配置本地yum源仓库
mysql事务(详解)
一文搞懂 条件编译和预处理指令 #define、#undef、#ifdef、#ifndef、#if、#elif、#else、#endif、defined 详解
Anaconda 更换默认虚拟环境
3D软件开发工具HOOPS全套产品开发介绍 | HOOPS Exchange、HOOPS Communicator
LVM与磁盘配额