Skip to content

Commit

Permalink
More template separation
Browse files Browse the repository at this point in the history
  • Loading branch information
rochacbruno committed Jun 2, 2018
1 parent 46ad75f commit 93be2d5
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 99 deletions.
3 changes: 1 addition & 2 deletions examples/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

app = Flask(__name__)
app.config['SWAGGER'] = {
'title': 'Colors API',
'uiversion': 2
'title': 'Colors API'
}
Swagger(app)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Using UIVERSION = Swagger UI 3.
Using UIVERSION = Swagger UI 2.
"""

from flask import Flask, jsonify
Expand All @@ -8,8 +8,8 @@

app = Flask(__name__)
app.config['SWAGGER'] = {
'title': 'Colors API Using Swagger UI 3',
'uiversion': 3
'title': 'Colors API Using Swagger UI 2 (old)',
'uiversion': 2
}
Swagger(app)

Expand Down
6 changes: 6 additions & 0 deletions flasgger/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from .utils import get_vendor_extension_fields
from .utils import validate
from .utils import LazyString
from . import __version__

NO_SANITIZER = lambda text: text # noqa
BR_SANITIZER = lambda text: text.replace('\n', '<br/>') if text else text # noqa
Expand Down Expand Up @@ -74,6 +75,11 @@ def get(self):
else: # pragma: no cover
data['flasgger_config'] = self.config
data['json'] = json
data['flasgger_version'] = __version__
data['favicon'] = self.config.get(
'favicon',
url_for('flasgger.static', filename='favicon-32x32.png')
)
return render_template(
'flasgger/index.html',
**data
Expand Down
2 changes: 2 additions & 0 deletions flasgger/ui3/templates/flasgger/custom_head.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!-- Include custom css and top js in templates/flasgger/head.html -->
{{flasgger_config.head_text | safe}}
2 changes: 1 addition & 1 deletion flasgger/ui3/templates/flasgger/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<section class="clear">
{{flasgger_config.footer_text | safe}}
<span style="float: right;">
[Powered by <a target="_blank" href="https://github.com/rochacbruno/flasgger">Flasgger</a>]
[Powered by <a target="_blank" href="https://github.com/rochacbruno/flasgger">Flasgger</a> {{ flasgger_version }}]
<br>
</span>
</section>
Expand Down
29 changes: 27 additions & 2 deletions flasgger/ui3/templates/flasgger/head.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,27 @@
<!-- Include custom css and top js in templates/flasgger/head.html -->
{{flasgger_config.head_text | safe}}
<meta charset="UTF-8">
<title>{{ title }}</title>
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,700|Source+Code+Pro:300,600|Titillium+Web:400,600,700" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="{{url_for('flasgger.static', filename='swagger-ui.css')}}" >
<!-- Customize the app.config['SWAGGER']['favicon'] -->
<link rel="icon" type="image/png" href="{{ favicon }}" sizes="64x64 32x32 16x16" />
<style>
html
{
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}

*,
*:before,
*:after
{
box-sizing: inherit;
}

body
{
margin:0;
background: #fafafa;
}
</style>
97 changes: 6 additions & 91 deletions flasgger/ui3/templates/flasgger/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,9 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ title }}</title>
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,700|Source+Code+Pro:300,600|Titillium+Web:400,600,700" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="{{url_for('flasgger.static', filename='swagger-ui.css')}}" >
<link rel="icon" type="image/png" href="{{url_for('flasgger.static', filename='favicon-32x32.png')}}" sizes="32x32" />
<link rel="icon" type="image/png" href="{{url_for('flasgger.static', filename='favicon-16x16.png')}}" sizes="16x16" />
<style>
html
{
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}

*,
*:before,
*:after
{
box-sizing: inherit;
}

body
{
margin:0;
background: #fafafa;
}
</style>
{% include 'flasgger/head.html' %}
<!-- To add custom code here override the template templates/flasgger/custom_head.html -->
{% include 'flasgger/custom_head.html' %}
</head>

<body>
Expand All @@ -53,71 +28,11 @@
<script src="{{url_for('flasgger.static', filename='swagger-ui-bundle.js')}}"> </script>
<script src="{{url_for('flasgger.static', filename='swagger-ui-standalone-preset.js')}}"> </script>
<script src="{{url_for('flasgger.static', filename='lib/jquery.min.js')}}" type='text/javascript'></script>
<script>
window.onload = function() {

{% if config.JWT_AUTH_URL_RULE -%}
// JWT token holder
var jwt_token;
{%- endif %}

// Build a system
const ui = SwaggerUIBundle(
Object.assign(
{

url: "{{ specs[0]['url'] }}",
dom_id: '#swagger-ui',
validatorUrl: null,
deepLinking: true,
jsonEditor: true,
{% if flasgger_config.doc_expansion -%}
docExpansion: "{{flasgger_config.doc_expansion | safe }}",
{%- endif %}
apisSorter: "alpha",
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
{% if config.JWT_AUTH_URL_RULE -%}
requestInterceptor: function(request) {
if (jwt_token) {
request.headers.Authorization = "Bearer " + jwt_token;
}

return request;
},
responseInterceptor: function(response) {
var tokenField = 'jwt-token';
var headers = response.headers;

if (headers.hasOwnProperty(tokenField)) {
jwt_token = headers[tokenField];
}

return response;
},
{%- endif %}
{% if not flasgger_config.hide_top_bar -%}
layout: "StandaloneLayout",
{%- endif %}

},
{{ json.dumps(flasgger_config.get('ui_params', {})) | safe }}
)

)

window.ui = ui

<!-- To customize the script that loads swagger, override templates/flasgger/swagger.html -->
{% include 'flasgger/swagger.html' %}

{% if not flasgger_config.hide_top_bar -%}
$(".topbar-wrapper .link span").replaceWith("<span>{{title}}</span>");
{%- endif %}
}
</script>
<!-- To customize the footer and include custom script on templates/flasgger/footer.html -->
{% include 'flasgger/footer.html' %}
</body>
</html>
65 changes: 65 additions & 0 deletions flasgger/ui3/templates/flasgger/swagger.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<script>
window.onload = function() {

{% if config.JWT_AUTH_URL_RULE -%}
// JWT token holder
var jwt_token;
{%- endif %}

// Build a system
const ui = SwaggerUIBundle(
Object.assign(
{

url: "{{ specs[0]['url'] }}",
dom_id: '#swagger-ui',
validatorUrl: null,
deepLinking: true,
jsonEditor: true,
{% if flasgger_config.doc_expansion -%}
docExpansion: "{{flasgger_config.doc_expansion | safe }}",
{%- endif %}
apisSorter: "alpha",
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
{% if config.JWT_AUTH_URL_RULE -%}
requestInterceptor: function(request) {
if (jwt_token) {
request.headers.Authorization = "Bearer " + jwt_token;
}

return request;
},
responseInterceptor: function(response) {
var tokenField = 'jwt-token';
var headers = response.headers;

if (headers.hasOwnProperty(tokenField)) {
jwt_token = headers[tokenField];
}

return response;
},
{%- endif %}
{% if not flasgger_config.hide_top_bar -%}
layout: "StandaloneLayout",
{%- endif %}

},
{{ json.dumps(flasgger_config.get('ui_params', {})) | safe }}
)

)

window.ui = ui

{% if not flasgger_config.hide_top_bar -%}
$(".topbar-wrapper .link span").replaceWith("<span>{{title}}</span>");
{%- endif %}
}
</script>

0 comments on commit 93be2d5

Please sign in to comment.