Skip to content

Commit

Permalink
feat: add scalar openapi ui, update swagger version to 5 (#369)
Browse files Browse the repository at this point in the history
* feat: add scalar openapi ui, update swagger version to 5

Signed-off-by: Keming <kemingy94@gmail.com>

* fix lint

Signed-off-by: Keming <kemingy94@gmail.com>

* make mypy happy

Signed-off-by: Keming <kemingy94@gmail.com>

---------

Signed-off-by: Keming <kemingy94@gmail.com>
  • Loading branch information
kemingy authored Jan 26, 2024
1 parent bc22adf commit e1b3cba
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 34 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ publish: package

format:
@ruff format ${SOURCE_FILES}
@ruff check --fix ${PY_SOURCE}

lint:
@ruff check ${SOURCE_FILES}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Check the [examples](examples) folder.
* falcon: `req.context`
* starlette: `request.context`
5. register to the web application `api.register(app)`
6. check the document at URL location `/apidoc/redoc` or `/apidoc/swagger`
6. check the document at URL location `/apidoc/redoc` or `/apidoc/swagger` or `/apidoc/scalar`

If the request doesn't pass the validation, it will return a 422 with a JSON error message(ctx, loc, msg, type).

Expand Down
25 changes: 25 additions & 0 deletions examples/flask_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from flask import Flask, jsonify
from flask.views import MethodView
from pydantic import BaseModel

from spectree import SpecTree

app = Flask(__name__)
spec = SpecTree("flask", annotations=True)


class User(BaseModel):
name: str
token: str


class Login(MethodView):
@spec.validate()
def post(self, json: User):
print(json)
return jsonify({"msg": "success"})


if __name__ == "__main__":
app.add_url_rule("/login", view_func=Login.as_view("login"))
app.run(debug=True)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "spectree"
version = "1.2.8"
version = "1.2.9"
dynamic = []
description = "generate OpenAPI document and validate request&response with Python annotations."
readme = "README.md"
Expand Down
63 changes: 37 additions & 26 deletions spectree/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,37 +36,17 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Swagger UI</title>
<link rel="stylesheet" type="text/css"
href="https://cdn.jsdelivr.net/npm/swagger-ui-dist@4/swagger-ui.css" >
<style>
html
{{
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}}
*,
*:before,
*:after
{{
box-sizing: inherit;
}}
body
{{
margin:0;
background: #fafafa;
}}
</style>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="SwaggerUI"/>
<title>SwaggerUI</title>
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@4.5.0/swagger-ui.css" />
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@4/swagger-ui-bundle.js"></script>
<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@4/swagger-ui-standalone-preset.js"></script>
<script src="https://unpkg.com/swagger-ui-dist@5.0.0/swagger-ui-bundle.js" crossorigin></script>
<script src="https://unpkg.com/swagger-ui-dist@5.0.0/swagger-ui-standalone-preset.js" crossorigin></script>
<script>
window.onload = function() {{
var full = location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : '');
Expand Down Expand Up @@ -177,5 +157,36 @@
}});
</script>
</body>
</html>""",
"scalar": """
<!doctype html>
<html>
<head>
<title>API Reference</title>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1" />
<style>
body {{
margin: 0;
}}
</style>
</head>
<body>
<script
id="api-reference"
data-url="{spec_url}">
</script>
<script>
var configuration = {{
theme: 'purple',
}}
var apiReference = document.getElementById('api-reference')
apiReference.dataset.configuration = JSON.stringify(configuration)
</script>
<script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script>
</body>
</html>""",
}
6 changes: 3 additions & 3 deletions spectree/plugins/flask_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ def parse_func(self, route: Any):
func = current_app.view_functions[route.endpoint]

# view class: https://flask.palletsprojects.com/en/1.1.x/views/
if getattr(func, "view_class", None):
cls = func.view_class
view_cls = getattr(func, "view_class", None)
if view_cls:
for method in route.methods:
view = getattr(cls, method.lower(), None)
view = getattr(view_cls, method.lower(), None)
if view:
yield method, view
else:
Expand Down
6 changes: 3 additions & 3 deletions spectree/plugins/quart_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ def parse_func(self, route: Any):
func = current_app.view_functions[route.endpoint]

# view class: https://flask.palletsprojects.com/en/1.1.x/views/
if getattr(func, "view_class", None):
cls = func.view_class
view_cls = getattr(func, "view_class", None)
if view_cls:
for method in route.methods:
view = getattr(cls, method.lower(), None)
view = getattr(view_cls, method.lower(), None)
if view:
yield method, view
else:
Expand Down

0 comments on commit e1b3cba

Please sign in to comment.