问题
I'm trying to use flask-restplus to build a restful API in python. I'd like to have the swagger docs located in a different place than the normal "/".
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
:
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:
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.
Any help would be apprecaited.
回答1:
With Flask-Restplus <= 0.8.0 you should write:
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
Starting from v0.8.1 (soon to be released), you will simply have to write:
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
回答2:
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)
回答3:
It looks like @api will need Resource, so I modified the code a bit to get around the error. The following will work only at /doc/, not the default root level.
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)
来源:https://stackoverflow.com/questions/31570096/move-flask-restplus-swagger-api-docs