How to render custom swagger ui
I'm using a swagger ui generated from the swagger-ui repo. I've created a static folder, into which I've put the dist css and js, and I've made a templates folder, into which I've copied the index.html, and renamed it as swagger-ui.html.
My main file looks like this:
api_blueprint = Blueprint("api", __name__, url_prefix="/api")
# say documentation should be at /api/doc
api = Api(api_blueprint)
app.register_blueprint(api_blueprint)
# register the blueprint
# initialise all of the api routes
initialise_routes(api)
@api.documentation
def custom_ui():
return apidoc.ui_for(api)
Inside the template, I'm trying to reference specs_url and title, which I'm meant to get from the apidoc._ui_for function. Unfortunately, it can't find those variables, and when I try to print specs_url the error says: 'Attempted to generate a URL without the application context being pushed'.
What I'm trying to do is be able to create my own swagger ui and get flask-restx to use that, rather than the default swagger ui in the library code. The documentation is minimal for doing this, so I'd really appreciate if someone could explain how it can be done.
I faced similar problem. I needed to change CSS. What I did is:
- Downloaded the CSS file from
https://mywebsite.com/swaggerui/swagger-ui.cssand placed it in my project inapp/ui/custom_swagger.css - Changed the CSS file the way I needed it to be.
- Added the following code to my
__init__.pyfile:
@app.route('/swaggerui/swagger-ui.css')
def custom_css_theme():
return send_file(
os.path.join(app.root_path, 'ui/custom_swagger.css')
)
Hope it will be useful for someone.