首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Flasgger和使用蓝图的Flask应用程序?

如何使用Flasgger和使用蓝图的Flask应用程序?
EN

Stack Overflow用户
提问于 2017-01-12 10:35:50
回答 0查看 11.8K关注 0票数 11

我使用Flasgger将Swagger UI添加到Python Flask应用程序中。互联网上最常见的示例是使用@app.route的基本烧瓶样式

代码语言:javascript
复制
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装饰器来定义端点。我用的是烧瓶蓝图。如下所示:

代码语言:javascript
复制
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匹配。

但是我在Swagger UI上没有得到任何东西,它都是空白的。docs_test_get.yml文件确实包含用于定义swagger规范的有效yaml标记。

我遗漏了什么?如何让Flasgger Swagger UI与基于Flask Blueprint的设置一起工作?

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41604212

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档