我正在使用Flasgger将Swagger UI添加到Python Flask应用程序中。Internet上最常见的示例是使用@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
装饰器来定义端点。我在用烧瓶蓝图。如下所示:
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
decorator。我还有两个地方的endpoint=test
匹配。
但我没有得到任何关于招摇过市的用户界面,这都是空白。docs_test_get.yml
文件包含有效的yaml标记来定义swagger规范
我错过了什么?如何让flasger Swagger UI与基于Flask Blueprint的设置一起工作?
现在https://github.com/rochacbruno/flasgger/blob/master/examples/example_blueprint.py中有一个blueprint应用程序示例
swag_from函数在分析文件路径时出错。可以先在get()中使用doc string定义api。 flasgger将解析MethodView()方法,如get、post。
引用端点时,只需添加蓝图的名称。蓝图创建名称空间。下面的例子。有用的提示:使用
app.logger.info(url_for('hello1'))
来调试端点问题-它显示了非常有用的错误消息,如Could not build url for endpoint 'hello1'. Did you mean 'api_bp.hello1' instead?
。相关问题 更多 >
编程相关推荐