我试图使用flask restplus在python中构建restful api。我想把这些夸张的文件放在一个不同于普通“/”的地方。
我正在遵循文档here并已遵循说明。我正在使用python2.7.3,代码如下:
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)
当我尝试运行这个
~/dev/test/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'
我不确定到底出了什么问题,我想我知道我并没有从
python app.py
继承过来,而Resource
通常是从as_view
继承过来的,但是文档似乎表明这应该是可行的。任何帮助都会被告知。
最佳答案
在烧瓶restplus
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)
注意使用
@app
而不是@api
从v0.8.1(即将发布)开始,您只需编写:
from flask import Flask
from flask.ext.restplus import Api, apidoc
app = Flask(__name__)
api = Api(app, doc='/doc/')
见:http://flask-restplus.readthedocs.org/en/latest/swagger.html#swagger-ui