如何在使用蓝图的Flask应用程序中使用Flasgger? [英] How to use Flasgger with Flask applications using Blueprints?
问题描述
我正在使用 Flasgger 将Swagger UI添加到我的Python Flask应用程序中.互联网上最常见的示例是使用 @ 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标记,用于定义标准规格.
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 Blueprint的设置一起使用?
What am I missing? How can I get Flasgger Swagger UI working with Flask Blueprint based setup?
推荐答案
现在
这篇关于如何在使用蓝图的Flask应用程序中使用Flasgger?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!