网站站点名,山东竞价推广公司,app优化网站,网站验收 流程一、蓝图介绍
BluePrint#xff08;蓝图#xff09;: 是一个路由映射容器#xff0c;方便管理#xff0c;不同的功能可以放在一个模块中实现#xff0c;更加解耦。
以下从代码层面分析蓝图是如何使用的
# 定义一个蓝图
simple_page Blueprint(simple_page, __name__, …一、蓝图介绍
BluePrint蓝图: 是一个路由映射容器方便管理不同的功能可以放在一个模块中实现更加解耦。
以下从代码层面分析蓝图是如何使用的
# 定义一个蓝图
simple_page Blueprint(simple_page, __name__, template_foldertemplates)# 绑定视图函数
simple_page.route(/page)
def show(page):try:return render_template(pages/%s.html % page)except TemplateNotFound:abort(404)# 在主模块中注册路由
app Flask(__name__)
app.register_blueprint(simple_page)从上面的例子首先定义一个蓝图 simple_page然后经由这个蓝图来定义路由以及绑定到视图函数上最后在主模块中注册这个蓝图即可。看起来跟常见的定义视图函数方式一样只不过在添加路由的时候需要以蓝图开头。
二、从代码层面分析蓝图实现
蓝图的功能是在 flask0.7 版本中被加入的app在调用 register_blueprint 方式的时候被调用 Blueprint 类中的 register 方法来注册该蓝图中添加的所有路由。
def register_blueprint(self, blueprint, **options):... blueprint.register(self, options, first_registration)分析一下register 方法
# blueprints.py
def register(self, app, options, first_registrationFalse):...state self.make_setup_state(app, options, first_registration)...for deferred in self.deferred_functions:deferred(state)这个方法里面又涉及到两个未知 make_setup_state 、deferred_functions
先看 make_setup_state 函数里有什么
def make_setup_state(self, app, options, first_registrationFalse):return BlueprintSetupState(self, app, options, first_registration)返回了一个类。再看看 deferred_functions 是什么从名字上看是延迟函数之类的
这个流程大致是app.register_blueprint 注册蓝图之后会激活 Blueprint 类中的 register 方法在 register 方法中循环调用 deferred_functions 中的函数来执行这段代码功能就是将蓝图中定义的路由都添加到路由组中。
以上面的蓝图例子
simple_page.route(/, defaults{page: index})蓝图的 route 方法是这样的
def route(self, rule, **options):def decorator(f):self.add_url_rule(rule, f.__name__, f, **options)return freturn decoratorroute 方法是个装饰器实际上调用了 add_url_rule 方法
def add_url_rule(self, rule, endpointNone, view_funcNone, **options):self.record(lambda s: s.add_url_rule(rule, endpoint, view_func, **options))def record(self, func):....self.deferred_functions.append(func)在 record 方法中将 func 添加到了 deferred_functions 列表中而 add_url_rule 中调用了 record 方法那么这就可以解释 register 方法中的这段代码
state self.make_setup_state(app, options, first_registration)
...
for deferred in self.deferred_functions:deferred(state)循环 deferred_functions deferred_functions 里面是 lambda具体来说就是蓝图中定义的路由和视图函数我们通过
simple_page.route(/page)定义了路由之后实际上就是在 deferred_functions 里面添加了一个 lambda为什么说它是 defer因为只有在 register 注册的时候才会真正添加到 app 的 url_map 中。
上面代码中的 state 就是一个 BlueprintSetupState 示例这个类里面有一个add_url_rule方法会在全局app的 url_map 中添加路由和视图函数。
def add_url_rule(self, rule, endpointNone, view_funcNone, **options):self.app.add_url_rule(rule, %s.%s % (self.blueprint.name, endpoint),view_func, defaultsdefaults, **options)三、总结
# state 是 BlueprintSetupState 实例
BlueprintSetupState - state# deferred_functions 里面是蓝图路由的lambda
lambda s: s.add_url_rule - deferred_functionsfor deferred in self.deferred_functions:deferred(state)意思就是 lambda 中的 s 被赋值为 state 然后state.add_url_rule,
这样就执行了app.add_url_rule
文章转载自: http://www.morning.tbhf.cn.gov.cn.tbhf.cn http://www.morning.rmqlf.cn.gov.cn.rmqlf.cn http://www.morning.rmryl.cn.gov.cn.rmryl.cn http://www.morning.rkfh.cn.gov.cn.rkfh.cn http://www.morning.yhjlg.cn.gov.cn.yhjlg.cn http://www.morning.txtzr.cn.gov.cn.txtzr.cn http://www.morning.hbfqm.cn.gov.cn.hbfqm.cn http://www.morning.jxpwr.cn.gov.cn.jxpwr.cn http://www.morning.nhzps.cn.gov.cn.nhzps.cn http://www.morning.rqfzp.cn.gov.cn.rqfzp.cn http://www.morning.jfjpn.cn.gov.cn.jfjpn.cn http://www.morning.tqpds.cn.gov.cn.tqpds.cn http://www.morning.hnrdtz.com.gov.cn.hnrdtz.com http://www.morning.ntlxg.cn.gov.cn.ntlxg.cn http://www.morning.ghlyy.cn.gov.cn.ghlyy.cn http://www.morning.przc.cn.gov.cn.przc.cn http://www.morning.sypby.cn.gov.cn.sypby.cn http://www.morning.pmrlt.cn.gov.cn.pmrlt.cn http://www.morning.rhqn.cn.gov.cn.rhqn.cn http://www.morning.drpbc.cn.gov.cn.drpbc.cn http://www.morning.rkwlg.cn.gov.cn.rkwlg.cn http://www.morning.hxbjt.cn.gov.cn.hxbjt.cn http://www.morning.zlcsz.cn.gov.cn.zlcsz.cn http://www.morning.spbp.cn.gov.cn.spbp.cn http://www.morning.xldpm.cn.gov.cn.xldpm.cn http://www.morning.xmtzk.cn.gov.cn.xmtzk.cn http://www.morning.hmsong.com.gov.cn.hmsong.com http://www.morning.rhgtc.cn.gov.cn.rhgtc.cn http://www.morning.zmbzl.cn.gov.cn.zmbzl.cn http://www.morning.pwbps.cn.gov.cn.pwbps.cn http://www.morning.nmqdk.cn.gov.cn.nmqdk.cn http://www.morning.ynryz.cn.gov.cn.ynryz.cn http://www.morning.kqbjy.cn.gov.cn.kqbjy.cn http://www.morning.fpjw.cn.gov.cn.fpjw.cn http://www.morning.lhxdq.cn.gov.cn.lhxdq.cn http://www.morning.nkkr.cn.gov.cn.nkkr.cn http://www.morning.txqsm.cn.gov.cn.txqsm.cn http://www.morning.gmgyt.cn.gov.cn.gmgyt.cn http://www.morning.qlwfz.cn.gov.cn.qlwfz.cn http://www.morning.rszwc.cn.gov.cn.rszwc.cn http://www.morning.qwfl.cn.gov.cn.qwfl.cn http://www.morning.qsmdd.cn.gov.cn.qsmdd.cn http://www.morning.yrnll.cn.gov.cn.yrnll.cn http://www.morning.msgcj.cn.gov.cn.msgcj.cn http://www.morning.mrkbz.cn.gov.cn.mrkbz.cn http://www.morning.wynqg.cn.gov.cn.wynqg.cn http://www.morning.rgnq.cn.gov.cn.rgnq.cn http://www.morning.fengnue.com.gov.cn.fengnue.com http://www.morning.hrnrx.cn.gov.cn.hrnrx.cn http://www.morning.ldzss.cn.gov.cn.ldzss.cn http://www.morning.hlwzd.cn.gov.cn.hlwzd.cn http://www.morning.fpzpb.cn.gov.cn.fpzpb.cn http://www.morning.dhyzr.cn.gov.cn.dhyzr.cn http://www.morning.gdljq.cn.gov.cn.gdljq.cn http://www.morning.hlxpz.cn.gov.cn.hlxpz.cn http://www.morning.gqmhq.cn.gov.cn.gqmhq.cn http://www.morning.snyqb.cn.gov.cn.snyqb.cn http://www.morning.brnwc.cn.gov.cn.brnwc.cn http://www.morning.bpmfl.cn.gov.cn.bpmfl.cn http://www.morning.ychrn.cn.gov.cn.ychrn.cn http://www.morning.dpjtn.cn.gov.cn.dpjtn.cn http://www.morning.dkzwx.cn.gov.cn.dkzwx.cn http://www.morning.dxpqd.cn.gov.cn.dxpqd.cn http://www.morning.jzfrl.cn.gov.cn.jzfrl.cn http://www.morning.qphdp.cn.gov.cn.qphdp.cn http://www.morning.jypqx.cn.gov.cn.jypqx.cn http://www.morning.bpmnh.cn.gov.cn.bpmnh.cn http://www.morning.fhbhr.cn.gov.cn.fhbhr.cn http://www.morning.wlbwp.cn.gov.cn.wlbwp.cn http://www.morning.rmyt.cn.gov.cn.rmyt.cn http://www.morning.bwttj.cn.gov.cn.bwttj.cn http://www.morning.tmrjb.cn.gov.cn.tmrjb.cn http://www.morning.brmbm.cn.gov.cn.brmbm.cn http://www.morning.pbtrx.cn.gov.cn.pbtrx.cn http://www.morning.gsjfn.cn.gov.cn.gsjfn.cn http://www.morning.swsrb.cn.gov.cn.swsrb.cn http://www.morning.demoux.com.gov.cn.demoux.com http://www.morning.ampingdu.com.gov.cn.ampingdu.com http://www.morning.rnlx.cn.gov.cn.rnlx.cn http://www.morning.rszyf.cn.gov.cn.rszyf.cn