简体   繁体   English

在 Flask-Restful 中使用蓝图有什么好处?

[英]What is the benefit of using Blueprint in Flask-Restful?

I'm new to Flask and the Blueprint concept.我是 Flask 和Blueprint概念的新手。 I understand the benefit of using Blueprint in a normal Flask application.我了解在普通 Flask 应用程序中使用Blueprint的好处。 However, after reading many posts/blogs/documentation, the benefit of using Blueprint with Flask-Restful is still unclear to me.但是,在阅读了许多帖子/博客/文档之后,我仍然不清楚将Blueprint与 Flask-Restful 结合使用的好处。

Let's consider two examples from the Flask-Restful documentation .让我们考虑Flask-Restful 文档中的两个示例。 The first one:第一个:

from flask import Flask
from flask_restful import Api
from myapi.resources.foo import Foo
from myapi.resources.bar import Bar
from myapi.resources.baz import Baz

app = Flask(__name__)
api = Api(app)

api.add_resource(Foo, '/Foo', '/Foo/<string:id>')
api.add_resource(Bar, '/Bar', '/Bar/<string:id>')
api.add_resource(Baz, '/Baz', '/Baz/<string:id>')

We have 3 resources.我们有 3 个资源。 We register them and start the project.我们注册它们并启动项目。 Everything is clear at this point.在这一点上一切都清楚了。 I would be happy with this approach.我会对这种方法感到满意。

Then, they use blueprint:然后,他们使用蓝图:

from flask import Flask, Blueprint
from flask_restful import Api, Resource, url_for

app = Flask(__name__)
api_bp = Blueprint('api', __name__)
api = Api(api_bp)

class TodoItem(Resource):
    def get(self, id):
        return {'task': 'Say "Hello, World!"'}

api.add_resource(TodoItem, '/todos/<int:id>')
app.register_blueprint(api_bp)

As far as I can see, the code is getting more complicated:据我所知,代码变得越来越复杂:

  • More steps required: create the API instance with the blueprint, register the blueprint with the app,...需要更多步骤:使用蓝图创建 API 实例,向应用程序注册蓝图,...
  • If I have another resource, eg user , I have to repeat all of those steps (please correct me if I'm wrong):如果我有其他资源,例如user ,我必须重复所有这些步骤(如果我错了,请纠正我):
user_bp = Blueprint('user', __name__)
user_api = Api(user_bp)
user_api.add_resource(User, '/user/<int:id>')
app.register_blueprint(user_bp)

So, what is the benefit of using Blueprint here?那么,在这里使用蓝图有什么好处呢?

A blueprint in Flask is an object to structure a Flask application into subsets. Flask中的blueprint是将 Flask 应用程序构造成子集的 object。 This helps in organizing code and separating functionality.这有助于组织代码和分离功能。

For Flask-RESTful this can be used to create an application subset for your api.对于Flask-RESTful ,这可用于为您的 api 创建应用程序子集。 So for example you have a core blueprint, an auth blueprint and besides that an api blueprint.例如,您有一个核心蓝图、一个身份验证蓝图以及一个 api 蓝图。 It can also be useful when you create a new version of your api that breaks backwards compatibility with the first version.当您创建 api 的新版本时,它也很有用,它破坏了与第一个版本的向后兼容性。 In this case you can create a second blueprint for the new version.在这种情况下,您可以为新版本创建第二个蓝图。

You don't have to create a new blueprint for each api endpoint that you create, you can reuse the api blueprint for each endpoint like:您不必为您创建的每个 api 端点创建新蓝图,您可以为每个端点重用 api 蓝图,例如:

from flask import Flask, Blueprint
from flask_restful import Api, Resource, url_for

app = Flask(__name__)
core_bp = Blueprint('core', __name__)
api_bp = Blueprint('api', __name__)
api = Api(api_bp)

@core_bp.route('/')
def index():
    return 'Index'

class TodoItem(Resource):
    def get(self, id):
        return {'task': 'Say "Hello, World!"'}

api.add_resource(TodoItem, '/todos/<int:id>')
api.add_resource(User, '/user/<int:id>')

app.register_blueprint(core_bp)
app.register_blueprint(api_bp, url_prefix='/api')

Like this your core application is accessible via '/' and the api via '/api'.像这样,您的核心应用程序可以通过“/”访问,api 通过“/api”访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM