Search code examples
pythonflask-restplus

Flask-Restplus / route


I'm trying to use Flask-Restplus to make an api and document it with swagger.

This is what I have so far and it works fine except I do not know how to add a root route.

from flask import Flask, Blueprint
from flask_restplus import Api, Resource, apidoc

app = Flask('__name__')
blueprint = Blueprint('v1', __name__, url_prefix='/rest/v1')
api = Api(blueprint, ui=False, version='1.0')

@blueprint.route('/apidoc/', endpoint='apidoc')
def swagger_ui():
   return apidoc.ui_for(api)

@blueprint.route('/', endpoint='rootres')
  def root():
     return ''

app.register_blueprint(blueprint)


ns = api.namespace('test', description='desc')

@ns.route('/', endpoint='rootresource')
class RootResource(Resource)
   def get(self):
       ...

while /rest/v1/test works fine, /rest/v1 gives me Page not found.

if I modify like this:

@blueprint.route('/aaa', endpoint='rootres')
   def root():
      return ''

then /rest/v1/aaa works.

Question: how can I make @blueprint.route('/') work?


Solution

  • When you wrote ui=False you disabled the /rest/v1/ path.

    In the next coming release (the 0.8.1 for the end of this week), you should be able to write:

    from flask import Flask, Blueprint
    from flask_restplus import Api, Resource
    
    app = Flask('__name__')
    blueprint = Blueprint('v1', __name__, url_prefix='/rest/v1')
    api = Api(blueprint, doc='/apidoc/', version='1.0')
    
    @blueprint.route('/', endpoint='rootres')
    def root():
        return ''
    
    ns = api.namespace('test', description='desc')
    
    @ns.route('/', endpoint='rootresource')
    class RootResource(Resource)
        def get(self):
            ...
    
    app.register_blueprint(blueprint)
    

    No need anymore to register a specific view for the documentation

    For the `blueprint.route('/'), I think this is fixed by 0.8.1 too.

    Note: register the blueprint later, after namespaces import/declaration.