Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 302
Alexis.Rolland Avatar asked Apr 26 '17 11:04

Alexis.Rolland


2 Answers

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.

like image 119
noirbizarre Avatar answered Oct 24 '22 06:10

noirbizarre


you could define it with prefix parameter. I've tried it on version 0.13.0.

from flask import Flask
from flask_restplus import Api

app = Flask(__name__)
api = Api(app, prefix='/api')
like image 37
Agung Wiyono Avatar answered Oct 24 '22 07:10

Agung Wiyono