Search code examples
flaskswagger-uiflask-restplus

How to indicate base url in Flask Restplus documentation


Could you please describe how to indicate the base URL in the documentation automatically generated by Flask Restplus?

I am running the following code but nothing shows up in the swagger UI:

from flask import Flask
from flask_restplus import Api

app = Flask(__name__)
api = Api(app,
    title='Data Quality Framework API',
    version='v0.1',
    doc='/data_quality_framework/api/documentation',
    contact='[email protected]',
    base_url='/test')

enter image description here


Solution

  • By default, when using this pattern (ie. with app as constructor argument), the API is on the root endpoint (ie. /) and the swagger documentation is on the API root (ie. still /).

    You have multiple possibilities:

    Use a blueprint to change the API root

    If you want to keep the documentation on the API root but change the API root, use a blueprint to register you API where you want.

    from flask import Flask, Blueprint
    from flask_restplus import Api
    
    app = Flask(__name__)
    blueprint = Blueprint('api', __name__, url_prefix='/test')
    api = Api(blueprint)
    app.register_blueprint(blueprint)
    
    assert url_for('api.doc') == '/test/'
    

    Only change the documentation location

    If you want to keep the API root at the same place but only move the documentation location, Flask-restplus allows you to specify it with the doc parameter.

    from flask import Flask
    from flask_restplus import Api
    
    app = Flask(__name__)
    api = Api(app, doc='/test/')
    
    assert url_for('doc') == '/test/'
    

    You can also combine both. See http://flask-restplus.readthedocs.io/en/stable/swagger.html#customization for more details on advanced documentation customization.