<i id='pQPux'><tr id='pQPux'><dt id='pQPux'><q id='pQPux'><span id='pQPux'><b id='pQPux'><form id='pQPux'><ins id='pQPux'></ins><ul id='pQPux'></ul><sub id='pQPux'></sub></form><legend id='pQPux'></legend><bdo id='pQPux'><pre id='pQPux'><center id='pQPux'></center></pre></bdo></b><th id='pQPux'></th></span></q></dt></tr></i><div id='pQPux'><tfoot id='pQPux'></tfoot><dl id='pQPux'><fieldset id='pQPux'></fieldset></dl></div>

      <small id='pQPux'></small><noframes id='pQPux'>

      • <bdo id='pQPux'></bdo><ul id='pQPux'></ul>

    1. <tfoot id='pQPux'></tfoot>
      <legend id='pQPux'><style id='pQPux'><dir id='pQPux'><q id='pQPux'></q></dir></style></legend>

      如何在使用蓝图的 Flask 应用程序中使用 Flasgger?

      时间:2023-09-28
      <tfoot id='bfcjC'></tfoot>

    2. <small id='bfcjC'></small><noframes id='bfcjC'>

    3. <i id='bfcjC'><tr id='bfcjC'><dt id='bfcjC'><q id='bfcjC'><span id='bfcjC'><b id='bfcjC'><form id='bfcjC'><ins id='bfcjC'></ins><ul id='bfcjC'></ul><sub id='bfcjC'></sub></form><legend id='bfcjC'></legend><bdo id='bfcjC'><pre id='bfcjC'><center id='bfcjC'></center></pre></bdo></b><th id='bfcjC'></th></span></q></dt></tr></i><div id='bfcjC'><tfoot id='bfcjC'></tfoot><dl id='bfcjC'><fieldset id='bfcjC'></fieldset></dl></div>
        <tbody id='bfcjC'></tbody>
              <legend id='bfcjC'><style id='bfcjC'><dir id='bfcjC'><q id='bfcjC'></q></dir></style></legend>
              • <bdo id='bfcjC'></bdo><ul id='bfcjC'></ul>
                本文介绍了如何在使用蓝图的 Flask 应用程序中使用 Flasgger?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用 Flasgger 将 Swagger UI 添加到我的 Python Flask 应用程序中.Internet 上最常见的示例是使用 @app.route 的基本 Flask 样式:

                I am adding Swagger UI to my Python Flask application using Flasgger. Most common examples on the Internet are for the basic Flask style using @app.route:

                from flasgger.utils import swag_from
                
                @app.route('/api/<string:username>')
                @swag_from('path/to/external_file.yml')
                def get(username):
                    return jsonify({'username': username})
                

                这行得通.

                然而,在我的应用程序中,我没有使用 @app.route 装饰器来定义端点.我正在使用烧瓶蓝图.如下:

                In my application however, I am not using @app.route decorators to define the endpoints. I am using flask Blueprints. Like following:

                from flask import Flask, Blueprint
                from flask_restful import Api, Resource
                from flasgger.utils import swag_from
                ...
                
                class TestResourceClass(Resource):
                
                      @swag_from('docs_test_get.yml', endpoint='test')   
                      def get() :
                         print "This is the get method for GET /1.0/myapi/test endpoint"
                
                app = Flask(__name__)
                my_api_blueprint = Blueprint('my_api', __name__)
                my_api = Api(my_api_blueprint)
                
                app.register_blueprint(my_api_blueprint, url_prefix='/1.0/myapi/')
                
                my_api.add_resource(TestResourceClass, '/test/'
                                        endpoint='test',
                                        methods=['GET', 'POST', 'PUT', 'PATCH', 'DELETE'])
                ....
                

                如上所示,我在绑定到 GET 方法端点的 TestResourceClass.get() 方法上使用了 @swag_from 装饰器.我在这两个地方也有 endpoint=test 匹配.

                As seen above, I used @swag_from decorator on the TestResourceClass.get() method which is bound to the GET method endpoint. I also have the endpoint=test matching in the two places.

                但是我在 Swagger UI 上什么都没有,全是空白的.docs_test_get.yml 文件确实包含有效的 yaml 标记来定义 swagger 规范.

                But I am not getting anything on the Swagger UI, it is all blank. The docs_test_get.yml file does contain the valid yaml markup to define the swagger spec.

                我错过了什么?如何让 Flasgger Swagger UI 与基于 Flask 蓝图的设置一起使用?

                What am I missing? How can I get Flasgger Swagger UI working with Flask Blueprint based setup?

                推荐答案

                现在https://github.com/rochacbruno/flasgger/blob/master/examples/example_blueprint.py

                """
                A test to ensure routes from Blueprints are swagged as expected.
                """
                from flask import Blueprint, Flask, jsonify
                
                from flasgger import Swagger
                from flasgger.utils import swag_from
                
                app = Flask(__name__)
                
                example_blueprint = Blueprint("example_blueprint", __name__)
                
                
                @example_blueprint.route('/usernames/<username>', methods=['GET', 'POST'])
                @swag_from('username_specs.yml', methods=['GET'])
                @swag_from('username_specs.yml', methods=['POST'])
                def usernames(username):
                    return jsonify({'username': username})
                
                
                @example_blueprint.route('/usernames2/<username>', methods=['GET', 'POST'])
                def usernames2(username):
                    """
                    This is the summary defined in yaml file
                    First line is the summary
                    All following lines until the hyphens is added to description
                    the format of the first lines until 3 hyphens will be not yaml compliant
                    but everything below the 3 hyphens should be.
                    ---
                    tags:
                      - users
                    parameters:
                      - in: path
                        name: username
                        type: string
                        required: true
                    responses:
                      200:
                        description: A single user item
                        schema:
                          id: rec_username
                          properties:
                            username:
                              type: string
                              description: The name of the user
                              default: 'steve-harris'
                    """
                    return jsonify({'username': username})
                
                
                app.register_blueprint(example_blueprint)
                
                swag = Swagger(app)
                
                if __name__ == "__main__":
                    app.run(debug=True)
                

                这篇关于如何在使用蓝图的 Flask 应用程序中使用 Flasgger?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:django-rest-swagger 是否不适用于模型序列化器? 下一篇:使用 flask restplus 进行 api 记录的特定时间格式

                相关文章

              • <legend id='YTsUs'><style id='YTsUs'><dir id='YTsUs'><q id='YTsUs'></q></dir></style></legend>

                    <bdo id='YTsUs'></bdo><ul id='YTsUs'></ul>
                  <i id='YTsUs'><tr id='YTsUs'><dt id='YTsUs'><q id='YTsUs'><span id='YTsUs'><b id='YTsUs'><form id='YTsUs'><ins id='YTsUs'></ins><ul id='YTsUs'></ul><sub id='YTsUs'></sub></form><legend id='YTsUs'></legend><bdo id='YTsUs'><pre id='YTsUs'><center id='YTsUs'></center></pre></bdo></b><th id='YTsUs'></th></span></q></dt></tr></i><div id='YTsUs'><tfoot id='YTsUs'></tfoot><dl id='YTsUs'><fieldset id='YTsUs'></fieldset></dl></div>

                  <small id='YTsUs'></small><noframes id='YTsUs'>

                  1. <tfoot id='YTsUs'></tfoot>