Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions example.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
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 '/')
API_URL = (
"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/'
Expand All @@ -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/
12 changes: 11 additions & 1 deletion flask_swagger_ui/flask_swagger_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions flask_swagger_ui/templates/index.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
<title>{{app_name}}</title>
<link rel="stylesheet" type="text/css" href="{{base_url}}/index.css">
<link rel="stylesheet" type="text/css" href="{{base_url}}/swagger-ui.css">
<link rel="icon" type="image/png" href="{{base_url}}/favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="{{base_url}}/favicon-16x16.png" sizes="16x16" />
{% for icon in icons %}
<link rel="icon" type="image/png" href="{{ icon.href }}" {% if 'sizes' in icon %} sizes="{{ icon.sizes }}" {% endif %}/>
{% endfor %}
</head>

<body>
Expand Down Expand Up @@ -44,4 +45,4 @@
</script>
</body>

</html>
</html>