2

I am trying to deploy an app flask using flask-restx package and I am stuck on how to display properly the documentation on the prod server. It works well locally without any prefix, but on the server, the files url need to be prefixed by my-prefix and I am struggling to make the swaggerui files accessible from the url : url/my-prefix

Here is my code :

app = Flask(__name__)
api = Api(
          app,
          version='3.0',
          title='My API',
          description='My API',
          doc="/my-prefix/docs/",
          prefix="/my-prefix",
          base_url="/my-prefix"
)

With this I get the swagger.json at the good url but not with the right reference in the code source of the documentation page. The swaggerui files are not well referenced in the code source oof the documentation page and not at the accessible from url/my-prefix/swaggerui/files.

So I have added this :

@apidoc.apidoc.add_app_template_global
def swagger_static(filename):
     return f"/my-prefix/swaggerui/{filename}"


api = Api(
          app,
          version='3.0',
          title='My API',
          description='My API',
          doc="/my-prefix/docs/",
          prefix="/my-prefix",
          base_url="/my-prefix"
)


@api.documentation
def custom_ui():
     return render_template("swagger-ui.html", title=api.title, specs_url="/my-prefix/swagger.json")

With this, the swaggerui files and the swagger.json file are conveniently referenced in the code source of the documentation page (url/my-prefix/swaggerui/myfile and url/my-prefix/swagger.json) but the swaggerui files are not reachable from this url. Do you know what I should add to make it accessible at this url : url/my-prefix/swaggerui/myfile

kiki
  • 563
  • 4
  • 17

1 Answers1

1

It can be done through Apidoc static_url_path override

from flask import Flask
from flask_restx import Api
from flask_restx.apidoc import apidoc


app = Flask(__name__)
api = Api(
          app,
          version='3.0',
          title='My API',
          description='My API',
          doc="/my-prefix/docs/""
)

apidoc.static_url_path = "/my-prefix/swaggerui"
Imelstorm
  • 11
  • 1