[英]Move Flask-Restplus Swagger API Docs
I'm trying to use flask-restplus to build a restful API in python. 我正在尝试使用flask-restplus在python中构建一个restful API。 I'd like to have the swagger docs located in a different place than the normal "/".
我希望将swagger文档放在与普通“/”不同的地方。
I'm following the documentation here and have followed the instructions. 我在这里遵循文档,并按照说明进行操作。 I'm using python2.7.3 and have the following code
~/dev/test/app.py
: 我正在使用python2.7.3并具有以下代码
~/dev/test/app.py
:
from flask import Flask
from flask.ext.restplus import Api, apidoc
app = Flask(__name__)
api = Api(app, ui=False)
@api.route('/doc/', endpoint='doc')
def swagger_ui():
return apidoc.ui_for(api)
app.register_blueprint(apidoc.apidoc)
When I try to run this python app.py
I get: 当我尝试运行这个
python app.py
我得到:
Traceback (most recent call last):
File "app.py", line 7 in <module>
@api.route('/doc/', endpoint='doc')
File "/home/logan/.virtualenvs/test/lib/python2.7/site-packages/flask_restplus/api.py", line 191, in wrapper
self.add_resources(cls, *urls, **kwargs)
File "/home/logan/.virtualenvs/test/lib/python2.7/site-packages/flask_restplus/api.py", line 175, in add_resource
super(Api, self).add_resource(resource, *urls, **kwargs)
File "/home/logan/.virtualenvs/test/lib/python2.7/site-packages/flask_restful/__init__.py", line 396, in add_resource
self._register_view(self.app, resource, *urls, **kwargs)
File "/home/logan/.virtualenvs/test/lib/python2.7/site-packages/flask_restful/__init__.py", line 435, in _register_view
resource_func = self.output(resource.as_view(endpoint, *resource_class_args,
AttributeError: 'function' object has no attribute 'as_view'
I'm not really sure what exactly is going wrong, I guess I understand that I haven't inherited from Resource
which is where as_view
would normally come from, but the documentation seems to indicate that this should work. 我不确定到底出了什么问题,我想我明白我没有从
Resource
中继承as_view
通常来自的地方,但文档似乎表明这应该有效。
Any help would be apprecaited. 任何帮助都会得到帮助。
With Flask-Restplus <= 0.8.0 you should write: 使用Flask-Restplus <= 0.8.0,你应该写:
from flask import Flask
from flask.ext.restplus import Api, apidoc
app = Flask(__name__)
api = Api(app, ui=False)
@app.route('/doc/', endpoint='doc')
def swagger_ui():
return apidoc.ui_for(api)
Note the use of a @app
instead of @api
注意使用
@app
而不是@api
Starting from v0.8.1 (soon to be released), you will simply have to write: 从v0.8.1(即将发布)开始,你只需要写:
from flask import Flask
from flask.ext.restplus import Api, apidoc
app = Flask(__name__)
api = Api(app, doc='/doc/')
See: http://flask-restplus.readthedocs.org/en/latest/swagger.html#swagger-ui 请参阅: http : //flask-restplus.readthedocs.org/en/latest/swagger.html#swagger-ui
After recently struggling with this myself, I've had luck with this approach: 在最近自己挣扎之后,我对这种方法很幸运:
from flask import Flask, Blueprint
from flask.ext.restplus import Api, apidoc
app = Flask(__name__)
blueprint = Blueprint('api', __name__)
api = Api(blueprint, ui=False)
@blueprint.route('/doc/', endpoint='doc')
def swagger_ui():
return apidoc.ui_for(api)
app.register_blueprint(blueprint)
It looks like @api will need Resource, so I modified the code a bit to get around the error. 看起来@api需要资源,所以我修改了一些代码来解决错误。 The following will work only at /doc/, not the default root level.
以下内容仅适用于/ doc /,而不是默认的根级别。
from flask import Flask, make_response
from flask.ext.restplus import Api, apidoc, Resource
app = Flask(__name__)
api = Api(app, ui=False)
@api.route('/doc/', endpoint='doc', doc=False)
class ApiDoc(Resource):
def get(self):
return make_response(apidoc.ui_for(api))
app.register_blueprint(apidoc.apidoc)
if __name__ == '__main__':
app.run(debug=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.