#从flask这个包中导入Flask这个类
#Flask这个类是项目的核心,以后很多操作都是基于这个类的对象
#注册url、注册蓝图等都是基于这个类的对象
from flask import Flask #创建一个Flask对象,传递__name__参数进去
#__name__参数的作用:
#1.可以规定模板和静态文件的查找路劲
#2.以后一些Flask插件,比如Flask-migrate、Flask-SQLAlchemy如果报错了,
#那么Flask可以通过这个参数找到具体的报错位置
app = Flask(__name__) #@app.route:是一个装饰器
#@app。route(“/”)就是将url中的/映射到hello_world这个视图函数上面
#以后你访问我这个网站的/目录的时候,会执行hello_world这个函数,然后将这个
#返回值返回给浏览器
@app.route('/')
def hello_world():
return 'Hello World' if __name__ == '__main__':
#app.run():Flask中的一个测试应用服务
# while True: run相当于
# listen()
# input()
app.run()

看下 route(‘/’)的源码

先看下一般我们使用装饰器怎么用

无参装饰器

User = None

def decorater(func):
def wapper(*args,**kwargs):
if User:
return func(*args,**kwargs)
else:
#就执行相应逻辑
pass
return wapper

有参装饰器

def func(fun,*args,**kwargs):
def decorater(f):
def wapper(*args, **kwargs):
if User:
return func(*args, **kwargs)
else:
# 就执行相应逻辑
pass
return wapper
return decorater ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————-_
下面看下flask里面是怎么处理的
def route(self, rule, **options):
"""A decorator that is used to register a view function for a
given URL rule. This does the same thing as :meth:`add_url_rule`
but is intended for decorator usage:: @app.route('/')
def index():
return 'Hello World' For more information refer to :ref:`url-route-registrations`. :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): #f就是那个注册函数但是这里没有执行
endpoint = options.pop('endpoint', None)
self.add_url_rule(rule, endpoint, f, **options) #而是将注册url传入进了 add_url_rule()这个函数,路由规则和注册函数进行绑定
return f
return decorator
————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
看下 add_url_rule
@setupmethod
def add_url_rule(self, rule, endpoint=None, view_func=None,
provide_automatic_options=None, **options):
"""Connects a URL rule. Works exactly like the :meth:`route`
decorator. If a view_func is provided it will be registered with the
endpoint. Basically this example:: @app.route('/')
def index():
pass Is equivalent to the following:: def index():
pass
app.add_url_rule('/', 'index', index) If the view_func is not provided you will need to connect the endpoint
to a view function like so:: app.view_functions['index'] = index Internally :meth:`route` invokes :meth:`add_url_rule` so if you want
to customize the behavior via subclassing you only need to change
this method. For more information refer to :ref:`url-route-registrations`. .. versionchanged:: 0.2
`view_func` parameter added. .. versionchanged:: 0.6
``OPTIONS`` is added automatically as method. :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 view_func: the function to call when serving a request to the
provided endpoint
:param provide_automatic_options: controls whether the ``OPTIONS``
method should be added automatically. This can also be controlled
by setting the ``view_func.provide_automatic_options = False``
before adding the rule.
: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.
"""
if endpoint is None:
endpoint = _endpoint_from_view_func(view_func)#这个是注册函数的名字
options['endpoint'] = endpoint #将字典的形式 将函数名字 绑定到 options这个字典上 键值对:
methods = options.pop('methods', None) #这里用了字典的pop方法,pop()这个方法有三个参数一个是self就是字典zij,
第二参数就是关键字,第三个就是没有找到这个关键字的默认参数
# if the methods are not given and the view_func object knows its
# methods we can use that instead. If neither exists, we go with
# a tuple of only ``GET`` as default.
  

如果没有给出方法,并且view_func对象知道它的方法

方法,我们可以用它来代替。如果两者都不存在,我们就一起去

只有' GET '作为defaul的元组

    if methods is None:
methods = getattr(view_func, 'methods', None) or ('GET',)#这个用自省中动态的获取里面的方法,methods有值就是直接获取,没有就是None,None就不执行,执行
后面的默认参数元祖
if isinstance(methods, string_types):#这里methods是(‘GET’,)
raise TypeError('Allowed methods have to be iterables of strings, '
'for example: @app.route(..., methods=["POST"])')
methods = set(item.upper() for item in methods) # Methods that should always be added
required_methods = set(getattr(view_func, 'required_methods', ())) # starting with Flask 0.8 the view_func object can disable and
# force-enable the automatic options handling.
if provide_automatic_options is None:
provide_automatic_options = getattr(view_func,
'provide_automatic_options', None) if provide_automatic_options is None:
if 'OPTIONS' not in methods:
provide_automatic_options = True
required_methods.add('OPTIONS')
else:
provide_automatic_options = False # Add the required methods now.
methods |= required_methods rule = self.url_rule_class(rule, methods=methods, **options)
rule.provide_automatic_options = provide_automatic_options self.url_map.add(rule)
if view_func is not None:
old_func = self.view_functions.get(endpoint)
if old_func is not None and old_func != view_func:
raise AssertionError('View function mapping is overwriting an '
'existing endpoint function: %s' % endpoint)
self.view_functions[endpoint] = view_func

flask第一级的更多相关文章

  1. openshift云计算平台diy模式安装Python2.7+Flask

    主要翻译了链接1)的教程,加上一些个人研究,步骤如下: 1) 在openshift.redhat.com申请账号,安装git for windows,然后安装gem install rhc,这些比较容 ...

  2. flask开发restful api系列(7)-蓝图与项目结构

    如果有几个原因可以让你爱上flask这个极其灵活的库,我想蓝图绝对应该算上一个,部署蓝图以后,你会发现整个程序结构非常清晰,模块之间相互不影响.蓝图对restful api的最明显效果就是版本控制:而 ...

  3. flask开发restful api

    flask开发restful api 如果有几个原因可以让你爱上flask这个极其灵活的库,我想蓝图绝对应该算上一个,部署蓝图以后,你会发现整个程序结构非常清晰,模块之间相互不影响.蓝图对restfu ...

  4. 简单物联网:外网访问内网路由器下树莓派Flask服务器

    最近做一个小东西,大概过程就是想在教室,宿舍控制实验室的一些设备. 已经在树莓上搭了一个轻量的flask服务器,在实验室的路由器下,任何设备都是可以访问的:但是有一些限制条件,比如我想在宿舍控制我种花 ...

  5. Python基于Flask框架配置依赖包信息的项目迁移部署小技巧

    一般在本机上完成基于Flask框架的代码编写后,如果有接口或者数据操作方面需求需要把代码部署到指定服务器上. 一般情况下,使用Flask框架开发者大多数都是选择Python虚拟环境来运行项目,不同的虚 ...

  6. 一篇博客带你入门Flask

    一. Python 现阶段三大主流Web框架 Django Tornado Flask 对比 1.Django 主要特点是大而全,集成了很多组件,例如: Models Admin Form 等等, 不 ...

  7. flask(三)之Flask-SQLAlchemy

    01-介绍 Flask-SQLAlchemy是一个Flask扩展,简化了在Flask应用中使用SQLAlchemy的操作.SQLAlchemy提供了高层ORM,也提供了使用数据库原生SQL的低层功能. ...

  8. Flask初识

    一.Flask初识 1.Flask介绍 Flask是一个使用 Python 编写的轻量级 Web 应用框架.其 WSGI 工具箱采用 Werkzeug服务 ,模板引擎则使用 Jinja2 .Flask ...

  9. 第三篇 Flask 中的 request

    第三篇 Flask 中的 request   每个框架中都有处理请求的机制(request),但是每个框架的处理方式和机制是不同的 为了了解Flask的request中都有什么东西,首先我们要写一个前 ...

随机推荐

  1. js/html 判断ie浏览器版本

    1.html判断浏览器:<!--[if !IE]><!-->除ie外都可以识别<!--<![endif]--><!--[if IE]>所有ie可以 ...

  2. Java基础——集合框架(待整理)

    ArrayList 和 和 Vector 的区别 从代码的最终的操作形式上可以发现,代码的输出结果与之前是一样的,而且没有区别,但是两者的区别还在于其内部的组成上. No. 区别点 Vector Ve ...

  3. 23.git简单使用

    git:我主要是为了收集命令,学习请去看廖雪峰的博客,内容很详细 git客户端下载: Git是GitHub开源社区的版本管理系统: 下载地址:https://git-scm.com/download/ ...

  4. win10 1903 更改文字大小

    标题栏 - 菜单 - 消息框 - 调色板标题11- 图标 - 工具提示 - Caption 标题 的 宽/高 - ; 14的宽高 - 菜单 的 宽/高 - ; 的宽高 -; 设置 注册表 HKEY_C ...

  5. Git 删除本地保存的账号和密码

    使用git在本地拉过一次代码时候git会自动将用户名密码保存到本地. 导致想用别的用户名和密码拉代码时没有权限,这时需要删除或者修改git在本地保存的账户名和密码. 具体办法如下: 1.控制面板--& ...

  6. 什么是JavaScript循环结构?

    ㈠什么是循环结构 ⑴什么是循环? 反复一遍又一遍的做着相同(相似)的事情 ⑵循环中的两大要素 ①循环条件:什么时候开始,什么时候结束 ②循环操作:循环体,循环过程中,干了什么 ㈡循环结构—while循 ...

  7. Spring MVC 的 multipartResolver 不能同iWebOffice2006 共同使用

    转:http://jamesby.iteye.com/blog/57381 项目使用iWebOffice2006,本来可以正常使用,但是系统有文件上传需求,故定义了一个multipartResolve ...

  8. (转)window.parent和window.opener区别

    下面一段代码是关于window.parent和window.opener区别 来讲的,我们如果要用到iframe的值传到另一框架就要用到window.opener.document.getElemen ...

  9. jquery target属性 语法

    jquery target属性 语法 作用:target 属性规定哪个 DOM 元素触发了该事件.大理石平台精度等级 语法:event.targe 参数: 参数 描述 event     必需.规定需 ...

  10. jquery enabled选择器 语法

    jquery enabled选择器 语法 作用::enabled 选择器选取所有启用的表单元素.大理石平台精度等级 语法:$(":enabled") jquery enabled选 ...