Python Flask Blueprint 蓝图

Python Flask Blueprint 蓝图

本篇来了解一下 Flask 中 Blueprint 蓝图,什么蓝图 。。就是一个分模块的扩展而已,用来让不同的 业务模块api 分到不同的python文件中 而已,和 Spring mvc 的 class 级别 的@RequestMapping("/") 差不多。。

image-20220719153956741

前因:

为什么要引入 Blueprint 蓝图呢? 因为如果默认把API接口都写到 一个py 文件里面 那么这个文件会越来越复杂,会越来越臃肿 导致 无法维护 ,所以按照正常的开发逻辑 也需要进行模块的划分 ,那么就需要引入 Blueprint

1.安装 Blueprint

需要先在环境中 使用 pip 安装一下 Blueprint 扩展

pip install Blueprint

2.编写Flask 入口 py文件

from flask import Flask


app = Flask(__name__)

@app.route('/')
def helloworld():
    return 'Hello world python flask'

# 新版本应该是 不支持这样启动了
# if __name__ == '__main__':
#     app.run()

3.编写User模块 user.py文件

根据业务 可以创建新的模块 然后通过Blueprint 去注册路由

from flask import Blueprint

user = Blueprint('user',__name__)

@user.route('/user/username')
def username():
    return 'get username : johnny'

4.Blueprint注册到 Flask app上

在入口文件中 把刚刚创建的 Blueprint 注册上去

from flask import Flask

#引入 刚刚创建的 blueprint
from user import user

app = Flask(__name__)
#注册到 flask app上
app.register_blueprint(user)

@app.route('/')
def helloworld():
    return 'Hello world python flask'

# if __name__ == '__main__':
#     app.run()

5.验证

访问:/user/username 得到如下:

image-20220719153413899

访问: / 得到如下:

image-20220719153444663

总结:

使用 Blueprint很简单 按照下面的流程

  1. pip install Blueprint

  2. 创建 新模块文件,在其中创建Blueprint对象 如:

    # Blueprint两个参数('蓝图名字',蓝图所在位置')
    user = Blueprint('user',__name__) 
  1. Blueprint注册到 Flask app上

    #引入 刚刚创建的 blueprint
    from user import user
    app = Flask(__name__)
    
    app.register_blueprint(user)

可以把Blueprint 理解为 Spring mvc 的 Class 级别 的@RequestMapping("/user") 差不多。。

欢迎大家访问 个人博客 Johnny小屋
欢迎关注个人公众号

欢迎关注个人公众号

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
How to build useful, real-world applications in the Python programming language Key Features Deliver scalable and high-performing applications in Python. Delve into the great ecosystem of Python frameworks and libraries through projects that you will build with this book. This comprehensive guide will help you demonstrate the power of Python by building practical projects. Book Description Python is a very powerful, high-level, object-oriented programming language. It's known for its simplicity and huge community support. Python Programming Blueprints will help you build useful, real-world applications using Python. In this book, we will cover some of the most common tasks that Python developers face on a daily basis, including performance optimization and making web applications more secure. We will familiarize ourselves with the associated software stack and master asynchronous features in Python. We will build a weather application using command-line parsing. We will then move on to create a Spotify remote control where we'll use OAuth and the Spotify Web API. The next project will cover reactive extensions by teaching you how to cast votes on Twitter the Python way. We will also focus on web development by using the famous Django framework to create an online game store. We will then create a web-based messenger using the new Nameko microservice framework. We will cover topics like authenticating users and, storing messages in Redis. By the end of the book, you will have gained hands-on experience in coding with Python. What you will learn Learn object-oriented and functional programming concepts while developing projects The dos and don'ts of storing passwords in a database Develop a fully functional website using the popular Django framework Use the Beautiful Soup library to perform web scrapping Get started with cloud computing by building microservice and serverless applications in AWS Develop scalable and cohesive microservices using the Nameko framework Create service dependencies for Redis and PostgreSQL Who This Book Is For This book is for software developers who are familiar with Python and want to gain hands-on experience with web and software development projects. A basic knowledge of Python programming is required. Table of Contents Implementing the Weather Application Creating a Remote-Control Application with Spotify Casting Votes on Twitter Exchange Rates and the Currency Conversion Tool Building a Web Messenger with Microservices Extending TempMessenger with a User Authentication Microservice Online Video Game Store with Django Order Microservice Notification Serverless Application
Python Flask 蓝图是一种组织 Flask 应用程序的有效方式。它允许您将应用程序分解为可重用的模块,并使应用程序更易于管理和扩展。蓝图可以定义路由、视图、静态文件和模板等 Flask 应用程序中的各种组件。 以下是 Python Flask 蓝图的用法: 1. 创建蓝图 要创建蓝图,您需要使用 Flask 实例的 Blueprint() 函数。Blueprint() 函数需要两个参数:蓝图名称和蓝图文件的位置。 例如,以下代码创建了一个名为 'main' 的蓝图,并将其保存在 main.py 文件中: ``` from flask import Blueprint main_blueprint = Blueprint('main', __name__) ``` 2. 定义路由 要在蓝图中定义路由,您需要使用 Blueprint 对象的 route() 方法。该方法与 Flask 实例的 route() 方法类似。 例如,以下代码在 'main' 蓝图中定义了一个路由: ``` @main_blueprint.route('/') def index(): return 'Hello, World!' ``` 3. 注册蓝图 要在 Flask 应用程序中使用蓝图,您需要将其注册到应用程序中。可以使用 Flask 实例的 register_blueprint() 方法将蓝图注册到应用程序中。 例如,以下代码将 'main' 蓝图注册到 Flask 应用程序中: ``` from flask import Flask from main import main_blueprint app = Flask(__name__) app.register_blueprint(main_blueprint) ``` 4. 使用蓝图的静态文件和模板 蓝图也可以定义其自己的静态文件和模板。要使用蓝图的静态文件和模板,您需要在蓝图的文件夹中创建一个名为 'static' 和 'templates' 的文件夹,并将静态文件和模板放在这些文件夹中。 例如,以下代码显示了如何在蓝图中使用静态文件和模板: ``` main_blueprint = Blueprint('main', __name__, template_folder='templates', static_folder='static') @main_blueprint.route('/') def index(): return render_template('index.html') ``` 以上就是 Python Flask 蓝图的用法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值