Created
January 25, 2019 09:48
-
-
Save bigfang/3ec27027ee9b3bd99dcecd8598bb3ea6 to your computer and use it in GitHub Desktop.
swagger json to html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
""" | |
Usage: | |
python swagger-json-to-html.py < /path/to/api.json > doc.html | |
curl http://localhost:5000/spec-json | python swagger-json-to-html.py > doc.html | |
""" | |
import json, sys | |
TEMPLATE = """ | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Swagger UI</title> | |
<link rel="stylesheet" type="text/css" href="https://cdn.bootcss.com/swagger-ui/3.20.5/swagger-ui.css" > | |
<link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" /> | |
<link rel="icon" type="image/png" href="./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> | |
</head> | |
<body> | |
<div id="swagger-ui"></div> | |
<script src="https://cdn.bootcss.com/swagger-ui/3.20.5/swagger-ui-bundle.js"> </script> | |
<script src="https://cdn.bootcss.com/swagger-ui/3.20.5/swagger-ui-standalone-preset.js"> </script> | |
<script> | |
window.onload = function() { | |
// Begin Swagger UI call region | |
var spec = %s | |
const ui = SwaggerUIBundle({ | |
spec: spec, | |
dom_id: '#swagger-ui', | |
deepLinking: true, | |
presets: [ | |
SwaggerUIBundle.presets.apis, | |
SwaggerUIStandalonePreset | |
], | |
plugins: [ | |
SwaggerUIBundle.plugins.DownloadUrl | |
], | |
layout: "BaseLayout" | |
}) | |
// End Swagger UI call region | |
window.ui = ui | |
} | |
</script> | |
</body> | |
</html> | |
""" | |
spec = json.load(sys.stdin) | |
sys.stdout.write(TEMPLATE % json.dumps(spec)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment