How to indicate base url in Flask Restplus documentation
Asked Answered
B

4

8

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

Bissau answered 26/4, 2017 at 11:19 Comment(1)
C
24

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.

Ciapha answered 30/5, 2017 at 9:18 Comment(3)
Thank you, as discussed here I was able to achieve this by removing the url_for itself and putting the documentation url directly in api = Api(..., doc='/documentation')Bissau
Hello Alexis and Axel, your conversation is little bit misleading because its incomplete, even in the conversation on github link. in simple words all that is needed is to have something like url_prefix="/api" in the creation of blueprint object and say /doc in doc parmater of api object creation time. so the final url becomes /api/doc and Base URL will be /api totally make sense. the documentation link added by Axel explains much better.Jewfish
Yeah for me the assert line wasn't necessary: api = Api(app, doc='/api/v1', prefix="/api/v1")Appendant
G
5

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')
Gebhardt answered 30/1, 2020 at 0:2 Comment(0)
P
0

In order to reset the Swagger base path (aka. basePath attribute):

@property
def base_path(self):
    return "/base/path"


Api.base_path = base_path

api = Api(
  # [...]
)

swagger result

notice: https://github.com/noirbizarre/flask-restplus/issues/770

source: https://github.com/noirbizarre/flask-restplus/issues/517

Preach answered 29/7 at 10:44 Comment(0)
M
-3

add it line

from flask import url_for
Marbles answered 23/12, 2019 at 6:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.