Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/customizable UI config #27

Closed
Closed
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ Only support Python3.
api_doc(app, config_url='https://petstore.swagger.io/v2/swagger.json', url_prefix='/api/doc', title='API doc')
```

Setting addtional [SwaggerUI configuration parameters](https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/) (simple values only, functions and variables are not supported)

```python
api_doc(app, config_path='/config/test.yaml', title='API doc', doc_config={ "validationUrl": None })
```

And suport config file editor

```python
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def load_requirements():
if __name__ == '__main__':
setup(
name='swagger-ui-py',
version='0.3.0',
version='0.4.0-dev',
description=DESCRIPTION,
long_description=readme(),
long_description_content_type='text/markdown',
Expand Down
9 changes: 7 additions & 2 deletions swagger_ui/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
class Interface(object):

def __init__(self, app, app_type=None, config_path=None, config_url=None,
url_prefix='/api/doc', title='API doc', editor=False):
url_prefix='/api/doc', title='API doc', editor=False,
doc_config=None):

self._app = app
self._title = title
self._url_prefix = url_prefix.rstrip('/')
self._config_url = config_url
self._config_path = config_path
self._editor = editor
self._doc_config = doc_config

assert self._config_url or self._config_path, 'config_url or config_path is required!'

Expand All @@ -41,7 +43,10 @@ def static_dir(self):
@property
def doc_html(self):
return self._env.get_template('doc.html').render(
url_prefix=self._url_prefix, title=self._title, config_url=self._uri('/swagger.json')
url_prefix=self._url_prefix,
title=self._title,
config_url=self._uri('/swagger.json'),
doc_config_json=json.dumps(self._doc_config)
)

@property
Expand Down
14 changes: 7 additions & 7 deletions swagger_ui/templates/doc.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<meta charset="UTF-8">
<title> {{ title }} </title>
<link rel="stylesheet" type="text/css" href="{{ url_prefix }}/swagger-ui.css" >
<link rel="stylesheet" type="text/css" href="{{ url_prefix }}/swagger-ui.css" />
<link rel="icon" type="image/png" href="{{ url_prefix }}/favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="{{ url_prefix }}/favicon-16x16.png" sizes="16x16" />
<style>
Expand Down Expand Up @@ -33,8 +33,8 @@
<body>
<div id="swagger-ui"></div>

<script src="{{ url_prefix }}/swagger-ui-bundle.js"> </script>
<script src="{{ url_prefix }}/swagger-ui-standalone-preset.js"> </script>
<script src="{{ url_prefix }}/swagger-ui-bundle.js" charset="UTF-8"> </script>
<script src="{{ url_prefix }}/swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
<script>
window.onload = function() {
// Begin Swagger UI call region
Expand All @@ -49,12 +49,12 @@
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
})
layout: "StandaloneLayout", <% if config_json != "null" %>...{{ config_json | safe }}<% endif %>
});
// End Swagger UI call region

window.ui = ui
}
window.ui = ui;
};
</script>
</body>
</html>
1 change: 1 addition & 0 deletions tools/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def replace_html_content():
index_content = re.sub('href="\\.', 'href="{{ url_prefix }}', index_content)
index_content = re.sub('https://petstore.swagger.io/v[1-9]/swagger.json',
'{{ config_url }}', index_content)
index_content = re.sub('layout: "StandaloneLayout"', 'layout: "StandaloneLayout", <% if config_json != "null" %>...{{ config_json | safe }}<% endif %>', index_content)

with html_path.open('w') as html_file:
html_file.write(index_content)
Expand Down