@app.route(), 是调用了flask.app.py文件里面的Flask类的route方法,route方法所做的事情和add_url_rule类似,是用来为一个URL注册一个视图函数,但是我们知道route方法是以装饰器的方式使用的

    def route(self, rule, **options):
"""sage::
@app.route('/')
def index():
return 'Hello World'
:param rule: the URL rule as string
:param endpoint: the endpoint for the registered URL rule. Flask
itself assumes the name of the view function as
endpoint
:param options: the options to be forwarded to the underlying
:class:`~werkzeug.routing.Rule` object. A change
to Werkzeug is handling of method options. methods
is a list of methods this rule should be limited
to (``GET``, ``POST`` etc.). By default a rule
just listens for ``GET`` (and implicitly ``HEAD``).
Starting with Flask 0.6, ``OPTIONS`` is implicitly
added and handled by the standard request handling.
"""
def decorator(f):
endpoint = options.pop('endpoint', None)
self.add_url_rule(rule, endpoint, f, **options)
return f
return decorator

源代码

参数解析

  • rule:  一个字符串格式的url规则,如:"/login"
  • endpont: 被注册的url的名字,一般用来反向生成url的时候使用,默认把视图函数的名字作为endpoint,如:endpoint="login"
  • **options: 这个options是跟随:class:`~werkzeug.routing.Rule` object定义的,后面会分析这个对象中的具体参数,但有一个methods参数默认是只监听get方法。

函数体解析

# 根据route的装饰器使用方法,我们可以知道f参数就是视图函数。
def decorator(f):
# 如果options参数中有endpoint则弹出endpoint,并把值赋值给endpoint变量,如果没有则赋值为None
endpoint = options.pop('endpoint', None)
# 调用add_url_rule方法,并把rule,endpont,f,**options传递进来,并执行这个方法,详见add_url_rule方法源码分析
self.add_url_rule(rule, endpoint, f, **options)
# 返回了返回f
return f

@app.route源码流程分析的更多相关文章

  1. Flask源码流程分析(一)

    Flask源码流程分析: 1.项目启动: 1.实例化Flask对象 1. 重要的加载项: * url_rule_class = Rule * url_map_class = Map * session ...

  2. DRF视图的使用及源码流程分析

    django rest framework中对于APIView.GenericAPIView.ModelViewSet.mixins扩展类的分析. APIView 示例 根据实际程序来分析: urls ...

  3. Spring事件监听ApplicationListener源码流程分析

    spring的事件机制是基于观察者设计模式的,ApplicationListener#onApplicationEvent(Event)方法,用于对事件的处理 .在容器初始化的时候执行注册到容器中的L ...

  4. MyBatis源码流程分析

    mybatis核心流程三大阶段 Mybatis的初始化  建造者模式 建造者模式(Builder Pattern)使用多个简单的对象一步一步构建成一个复杂的对象.这种类型的设计模式属于创建型模式,它提 ...

  5. u-boot的SPL源码流程分析

    上次梳理了一下SPL的基本概念和代码总体思路,这次就针对代码跑的流程做个梳理.SPL中,入口在u-boot-spl.lds中 ENTRY(_start) SECTIONS { .text : { __ ...

  6. Flask启动原理,源码流程分析

    1.执行Flask的实例对象.run()方法 from flask import Flask,request,session app = Flask(__name__) app.secret_key ...

  7. requireJS源码流程分析

  8. Django-Rest-Framework部分源码流程分析

    class TestView(APIView): ''' 调用这个函数的时候,会自动触发authentication_classes的运行,所以会先执行上边的类 ''' authentication_ ...

  9. 不会DRF?源码都分析透了确定不来看?

    目录 不会DRF?源码都分析透了确定不来看? 快速使用DRF写出接口 序列化和反序列化 drf快速使用 views.py serializer.py urls.py 在settings的app中注册 ...

随机推荐

  1. 从0开始学爬虫11之使用requests库下载图片

    从0开始学爬虫11之使用requests库下载图片 # coding=utf-8 import requests def download_imgage(): ''' demo: 下载图片 ''' h ...

  2. docker build doris-0.11.20-release source code

    1. pull doris dev docker image sudo docker pull apachedoris/doris-dev:build-env-1.1 2. dowload doris ...

  3. 使用wkhtmltopdf将多个html批量转成pdf

    相关工具:wkhtmltopdf 场景:比如笔者有 ognl中文文档,全部是html,现在想把它转成pdf,放到ipad阅读,文件如下: 下载好wkhtmltox(本地安装目录 D:\develop\ ...

  4. [转]Xmind 8 pro 软件破解版

    链接地址:https://blog.csdn.net/qq_16093323/article/details/80967867 作者博客:http://www.carrotchou.blog/

  5. kubectl exec 执行 容器命令

    格式如下: kubectl exec -it podName -c containerName -n namespace -- shell comand 1 创建目录 kubectl exec -it ...

  6. 利用卷积神经网络处理cifar图像分类

    这是一个图像分类的比赛CIFAR( CIFAR-10 - Object Recognition in Images ) 首先我们需要下载数据文件,地址: http://www.cs.toronto.e ...

  7. 走进热修复框架AndFix的世界

    关于阿里的开源热修复框架AndFix引起了广泛共鸣,受到了很多人的青睐.那今天就跟随我的步伐来详细了解一下AndFix的详细信息和使用方法.1.什么是AndFix? AndFix是阿里巴巴出的一个专门 ...

  8. 2019.10.28 IDEA入门指南(很多人问补充一篇)

    Idea快速入门指南 1.安装 1.1.安装 我们使用的是最新的2017.3.4版本: 双击打开, 选择一个目录,最好不要中文和空格: 然后选择桌面快捷方式,请选择64位: 然后选择安装: 开始安装: ...

  9. spark kmer计算

    输入文件:fa格式的文件 输出结果:kmer的频数和对应的kmer类型和计数 1.将fq.gz的文件转换成fa文件: #!/usr/bin/python env # -*- coding:utf-8 ...

  10. English Learning -- 0611--When Burnout Is a Sign You Should Leave Your Job

    I like the following article from Harvard Business Review, as I ever experienced burnout at work. Ve ...