From eb74f8aadb760856ff9b36e6b0a0280e553a3ace Mon Sep 17 00:00:00 2001 From: Francesco Fornari Date: Sat, 25 Jun 2022 18:53:39 +0200 Subject: [PATCH] ADD: possibility to configure with custom icon --- example.py | 12 ++++++++++-- flask_swagger_ui/flask_swagger_ui.py | 12 +++++++++++- flask_swagger_ui/templates/index.template.html | 7 ++++--- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/example.py b/example.py index 33676a5..c2474db 100644 --- a/example.py +++ b/example.py @@ -1,7 +1,8 @@ -from flask import Flask +from flask import Flask, url_for from flask_swagger_ui import get_swaggerui_blueprint app = Flask(__name__) +app.config['SERVER_NAME'] = "127.0.0.1:5000" SWAGGER_URL = "/api/docs" # URL for exposing Swagger UI (without trailing '/') @@ -9,6 +10,12 @@ "http://petstore.swagger.io/v2/swagger.json" ) # Our API url (can of course be a local resource) +with app.app_context(): + icons = [{ + "href": url_for("static", filename="favicon.ico"), + "sizes": "32x32" # optional key + }] + # Call factory function to create our blueprint swaggerui_blueprint = get_swaggerui_blueprint( SWAGGER_URL, # Swagger UI static files will be mapped to '{SWAGGER_URL}/dist/' @@ -22,10 +29,11 @@ # 'scopeSeparator': " ", # 'additionalQueryStringParams': {'test': "hello"} # } + # icons=icons ) app.register_blueprint(swaggerui_blueprint) -app.run() +app.run(host="127.0.0.1") # Now point your browser to localhost:5000/api/docs/ diff --git a/flask_swagger_ui/flask_swagger_ui.py b/flask_swagger_ui/flask_swagger_ui.py index 5c0886c..4871760 100644 --- a/flask_swagger_ui/flask_swagger_ui.py +++ b/flask_swagger_ui/flask_swagger_ui.py @@ -4,7 +4,7 @@ def get_swaggerui_blueprint( - base_url, api_url, config=None, oauth_config=None, blueprint_name="swagger_ui" + base_url, api_url, config=None, oauth_config=None, blueprint_name="swagger_ui", icons=None ): swagger_ui = Blueprint( @@ -26,12 +26,22 @@ def get_swaggerui_blueprint( if config: default_config.update(config) + if icons is None: + icons = [{ + "href": f"{base_url}/favicon-32x32.png", + "sizes": "32x32" + }, { + "href": f"{base_url}/favicon-16x16.png", + "sizes": "16x16" + }] + fields = { # Some fields are used directly in template "base_url": base_url, "app_name": default_config.pop("app_name"), # Rest are just serialized into json string for inclusion in the .js file "config_json": json.dumps(default_config), + "icons": icons } if oauth_config: fields["oauth_config_json"] = json.dumps(oauth_config) diff --git a/flask_swagger_ui/templates/index.template.html b/flask_swagger_ui/templates/index.template.html index bc0ec50..6339050 100644 --- a/flask_swagger_ui/templates/index.template.html +++ b/flask_swagger_ui/templates/index.template.html @@ -7,8 +7,9 @@ {{app_name}} - - + {% for icon in icons %} + + {% endfor %} @@ -44,4 +45,4 @@ - \ No newline at end of file +