Flask蓝图的使用(七)

  由于我们现在的项目开发是一个相对于来说非常耗时间和精力的一个工程,如果我们将所有的Flask的请求方法都写在同一个文件下的话,非常的不便于我们的代码的管理和后期的功能代码的添加, 就像我们在一个文件中写入多个路由视图,这会使代码维护变得困难。

  flask 蓝图(blueprint)是flask自带的一种框架结构,方便搭建更大的项目结构

现在我们先使用flask 蓝图展示一下他的单蓝图和多蓝图案例:

一、Flask blueprint 的官方案例(使用app启动项目)

1、单蓝图模式
在这里插入图片描述

"""
蓝图的官方案例
注册单一的蓝图实例
使用app加载蓝图
"""
from flask import Blueprint
from flask import Flask

# 创建蓝图
simple_blueprint = Blueprint("simple_page",__name__)

# 蓝图的路由和视图
@simple_blueprint.route("/")
def hello():
    return "hello world"

# 使用flask App方式启动项目
if __name__ == "__main__":
    app = Flask(__name__)
    # 注册蓝图
    app.register_blueprint(simple_blueprint)
    app.run()

2、多蓝图模式
在这里插入图片描述
BluePrintone.py

from flask import Blueprint

# 创建蓝图
simple_blueprint1 = Blueprint("simple_page1",__name__)

# 蓝图的路由和视图
@simple_blueprint1.route("/index1/")
def hello():
    return "hello world1"

BluePrinttwo.py

from flask import Blueprint

# 创建蓝图
simple_blueprint2 = Blueprint("simple_page2",__name__)

# 蓝图的路由和视图
@simple_blueprint2.route("/index2/")
def hello():
    return "hello world2"

manage.py

"""
多蓝图启动文件
"""
from flask import Flask

from muchBluePrint.BluePrintone import simple_blueprint1
from muchBluePrint.BluePrinttwo import simple_blueprint2

app = Flask(__name__)

# 注册蓝图
app.register_blueprint(simple_blueprint1)
app.register_blueprint(simple_blueprint2)

# 使用flask App方式启动项目
if __name__ == "__main__":
    app.run()

二、使用flask-script插件结合蓝图

与bluePrint结合紧密的有两个插件,我们采用blueprint之后,通常会用一种叫做惰性加载的方式,加载app.

flask_scrtip 结合蓝图或者自己可以定义flask项目的命令模式。类似django的python manage.py runserver

由于flask轻量,便于开发,Flask项目和爬虫或者其他的项目结合。

  • 使用flask + celery 驱动爬虫
  • 使用flask + 微信(qq) 进行聊天机器人管理
  • 使用flask + psutil 自动化监控

那么这个时候,就需要我们拓展flask命令 ,类似Django的python manage.py

1、安装flask-script插件

pip install flask-script

在这里插入图片描述

2、官方的单一版本flask-script案例
在该文件中,必须有一个Manager实例,Manager类追踪所有在命令行中调用的命令和处理过程的调用运行情况;

Manager只有一个参数——Flask实例,也可以是一个函数或其他的返回Flask实例;

调用manager.run()启动Manager实例接收命令行中的命令;

flask-script启动方式不是通过直接运行脚本方式,而是通过python 脚本名 runserver启动

# flaskScriptMain.py
"""
flask_script官方案例
"""
from flask import Flask
from flask_script import Manager

app = Flask(__name__)

@app.route('/')
def hello():
    return "hello world"

# 对app进行命令行序列化
manage = Manager(app)


if __name__ == "__main__":
    # 启动flask manage,以manage方式启动app
    manage.run()


# 在命令使用“--python 文件名 runserver--启动

在这里插入图片描述
在这里插入图片描述

3、flask-script添加命令案例

使用Command实例的@command修饰符来给指定函数添加命令功能

# flaskScriptAddCommand.py
"""
给flask_script 添加命令
"""
from flask import Flask
from flask_script import Manager

app = Flask(__name__)

@app.route('/')
def hello():
    return "hello world"

# 对app进行命令行序列化
manage = Manager(app)

@manage.command
def user_register_show(name = "create"):
    """
    :param name: 是命令行可以传递的参数,在命令行以 --name 来传递
    """
    username = input("please enter your username: ")
    email = input("please enter your email: ")
    password = input("please enter your password: ")
    password_confirm = input("please enter your password again: ")
    print("恭喜你执行了%s命令!"%name)
    return "不好意思了,本注册功能逗你玩呢!"

if __name__ == "__main__":
    # 启动flask manage,以manage方式启动app
    manage.run()


# 在命令使用“--python 文件名 runserver--启动

可以发现我们通过@command装饰后,项目运行就会多出一个以装饰函数命名的命令
在这里插入图片描述
以此命令运行该脚本

在这里插入图片描述

被装饰函数中的参数可同–参数名 命令 来指定

在这里插入图片描述

4、flask-script结合蓝图进行flask项目启动

还是用多蓝图的模式,只不过使用了flask-script就是基于命令行的flask多蓝图模式

项目结构:
在这里插入图片描述
BluePrint1.py

"""
多蓝图命令模式 蓝图1
"""
from flask import Blueprint

# 创建蓝图
simple_blueprint1 = Blueprint("simple_page1",__name__)

# 蓝图的路由和视图
@simple_blueprint1.route("/index1/")
def hello():
    return "hello world1"

BluePrint2.py

"""
多蓝图命令模式 蓝图2
"""
from flask import Blueprint

# 创建蓝图
simple_blueprint2 = Blueprint("simple_page2",__name__)

# 蓝图的路由和视图
@simple_blueprint2.route("/index2/")
def hello():
    return "hello world2"

manage.py

"""
多蓝图命令模式 应用
"""
from flask import Flask
from flask_script import Manager

from BluePrint1 import simple_blueprint1
from BluePrint2 import simple_blueprint2

app = Flask(__name__)

# 注册蓝图
app.register_blueprint(simple_blueprint1)
app.register_blueprint(simple_blueprint2)

manage = Manager(app)

# 使用flask App方式启动项目
if __name__ == "__main__":
    manage.run()

启动flask-script命令行方式启动项目
在这里插入图片描述

总结:
flask蓝图和flask-script使用流程:

  • 创建 Flask 应用(app)
  • 搭建蓝图结构
  • Flask-script命令行结构管理
    1、先创建蓝图
    2、App安装蓝图
    3、命令行启动app
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孜孜孜孜不倦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值