• 【Python】Flask蓝图(bluePrint)


    在这里插入图片描述

    博主:👍不许代码码上红
    欢迎:🐋点赞、收藏、关注、评论。

    一、蓝图(bluePrint)的概念

    引言

    Flask蓝图提供了模块化管理程序路由的功能,使程序结构清晰、简单易懂。

    定义

    简单来说,Blueprint 是一个存储视图方法的容器,这些操作在这个Blueprint 被注册到一个应用之后就可以被调用,Flask 可以通过Blueprint来组织URL以及处理请求。

    Flask使用Blueprint让应用实现模块化,在Flask中,Blueprint具有如下属性:

    1、一个项目可以具有多个Blueprint
    2、可以将一个Blueprint注册到任何一个未使用的URL下比如 “/”、“/sample”或者子域名
    3、在一个应用中,一个模块可以注册多次
    4、Blueprint可以单独具有自己的模板、静态文件或者其它的通用操作方法,它并不是必须要实现应用的视图和函数的
    5、在一个应用初始化时,就应该要注册需要使用的Blueprint

    注意

    但是一个Blueprint并不是一个完整的应用,它不能独立于应用运行,而必须要注册到某一个应用中。

    Blueprint对象用起来和一个应用/Flask对象差不多,最大的区别在于一个 蓝图对象没有办法独立运行,必须将它注册到一个应用对象上才能生效

    运行机制

    1、蓝图是保存了一组将来可以在应用对象上执行的操作,注册路由就是一种操作
    2、当在app对象上调用 route 装饰器注册路由时,这个操作将修改对象的url_map路由表
    3、然而,蓝图对象根本没有路由表,当我们在蓝图对象上调用route装饰器注册路由时,它只是在内部的一个延迟操作记录列表defered_functions中添加了一个项
    4、当执行app对象的 register_blueprint() 方法时,应用对象将从蓝图对象的 defered_functions 列表中取出每一项,并以自身作为参数执行该匿名函数,即调用应用对象的 add_url_rule() 方法,这将真正的修改应用对象的usr_map路由表

    二、蓝图(bluePrint)的创建

    创建admin文件夹
    在这里插入图片描述
    在admin文件夹下面创建__init__.py和admin_module.py
    ● 其中,空的__init__.py表示这个文件夹是一个python文件夹

    在这里插入图片描述

    admin_module.py

    from flask import Blueprint
    #我们创建了蓝图对象”admin_bp”,
    # 它使用起来类似于Flask应用的app对象,比如,它可以有自己的路由admin_bp.route()。
    # 初始化Blueprint对象的第一个参数admin指定了这个蓝图的名称,第二个参数指定了该蓝图所在的模块名,
    # 这里自然是当前文件。接下来,我们在应用中注册该蓝图。在Flask应用主程序中,
    # 使用app.register_blueprint()方法即可
    admin_bp = Blueprint('admin', __name__)
    
    @admin_bp.route('/')
    def index():
        return '

    Hello, this is a blueprint

    '
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    app.py

    from flask import Flask
    #调用蓝图
    from admin.admin_module import admin_bp
    
    from gevent import pywsgi
    
    app = Flask(__name__)
    app.register_blueprint(admin_bp,url_prefix='/admin')
    
    if __name__ == '__main__':
        app.run(host='0.0.0.0', debug=True)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    运行结果

    在网页中输入:

    http://127.0.0.1:5000/admin/

    在这里插入图片描述

    三、蓝图(bluePrint)的资源

    蓝图有自己的目录,它的所有资源都在其目录下。蓝图的资源目录是由创建Blueprint对象时传入的模块名__name__所在的位置决定的。同时,我们可以指定蓝图自己的模板目录和静态目录:

    admin_bp = Blueprint('admin', __name__,
                         template_folder='templates',
                         static_folder='static')
    
    • 1
    • 2
    • 3

    四、蓝图(bluePrint)资源调用

    1、更具有拓展性的框架

    概述

    随着业务代码的增加,需要为 Flask 程序提供一个具备扩展性的架构。

    根据 Flask 程序的扩展性分为如下三种类型:
    ● 所有的页面逻辑放在同一个文件中

    在这种架构中,程序完全不具备扩展性。在初学 Flask 时,使用的例子都是这种类型。

    ● 使用一个独立的 Python 文件实现蓝图

    在这种架构中,程序具备一定的扩展性:

    程序由主程序和多个蓝图构成;
    每个蓝图对应一个 Python 文件;
    所有的蓝图共享相同的模板文件目录;
    所有的蓝图共享相同的静态文件目录。

    ● 使用一个独立的目录实现蓝图

    在这种架构中,程序的扩展性最好:

    程序由主程序和多个蓝图构成;
    每个蓝图对应一个独立的目录,存储与这个蓝图相关的文件;
    每个蓝图有一个独立的模板文件目录;
    每个蓝图有一个独立的静态文件目录。

    2、例子

    例子的目录结构

    在这里插入图片描述

    app.py

    代码

    from flask import Flask
    import news
    import products
    
    app = Flask(__name__)
    
    app.register_blueprint(news.blueprint)
    app.register_blueprint(products.blueprint)
    if __name__ == '__main__':
        app.run(debug = True)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    import news:导入模块 news.py,在 news.py 中定义了一个蓝图对象 news.blueprint

    import products:导入模块 products.py,在 products.py 中定义了一个蓝图对象 products.blueprint

    app.register_blueprint(news.blueprint):在应用中注册蓝图对象 news.blueprint

    app.register_blueprint(products.blueprint):在应用中注册蓝图对象 products.blueprint

    news.init.py

    代码

    from flask import Blueprint, render_template
    
    blueprint = Blueprint('news', __name__, url_prefix='/news',
                          template_folder='templates', static_folder='static')
    
    @blueprint.route("/society/")
    def society_news():
        return render_template('society.html')
    
    @blueprint.route("/tech/")
    def tech_news():
        return "IT 新闻板块"
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    blueprint = Blueprint(…):创建一个名为 ‘news’ 的蓝图

    url_prefix=‘/news’:蓝图中页面的 URL 前缀为 /news

    template_folder=‘templates’:蓝图的模板目录为 templates,绝对路径为 ‘项目目录 /news/templates’

    static_folder=‘static’:蓝图的静态文件目录为 static,绝对路径为 ‘项目目录 /news/static’

    @blueprint.route(“/society/”):将路径 /society/ 和函数 society_news 关联

    @blueprint.route(“/tech/”):将路径 /tech/ 和函数 tech_news 关联

    return render_template(‘society.html’):调用 render_template (‘society.html’) 渲染模板文件 society.html,根据模板文件的查找规则,最终在 ‘项目目录 /news/templates’ 目录下找到模板文件

    society.html

    注意

    ● 放到news.templates下面

    代码

    <link rel="stylesheet" 
    href="{{ url_for('news.static',filename='news.css')}}">
    <h1>这里是,位于static.society中的sociey.html</h1>
    
    • 1
    • 2
    • 3

    url_for(‘news.static’,filename=‘news.css’):在模板文件中引用了静态文件 news.css。{{url_for (‘news.static’,filename=‘news.css’) }} 的输出为 news/static/news.css,其中 news.static 表示蓝图 news 的 static 目录

    news.css

    代码

    h1 {
        color: red;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    products.init.py

    代码

    from flask import Blueprint
    
    blueprint = Blueprint('products', __name__, url_prefix='/products')
    
    @blueprint.route("/car")
    def car_products():
        return "汽车产品版块"
    
    @blueprint.route("/baby")
    def baby_products():
        return "婴儿产品版块"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    运行结果

    在这里插入图片描述

  • 相关阅读:
    ansible控制windows机器
    python>>numpy包
    如何基于Github Pages + Hexo 的方式,免费搭建个人博客?
    diskGenius专业版使用:windows系统下加载ext4 linux系统分区并备份还原资源(文件的拷贝进、出)
    鸿翼归档:释放企业存储压力 提升数据利用效率
    软设上午题错题知识点4
    利用Django中的缓存系统提升Web应用性能
    创建SpringBoot项目
    利其器(2)——idea常用配置_提高开发效率
    【Elixr】 introduce
  • 原文地址:https://blog.csdn.net/qq_45801904/article/details/126703131