Skip to content

Commit

Permalink
Add ScalaR documentation UI support
Browse files Browse the repository at this point in the history
This looks good, possibly better than redocs.
  • Loading branch information
pgjones committed Jan 22, 2024
1 parent 791183e commit ba57153
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
6 changes: 4 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ methods.
The documentation is served by default at ``/openapi.json`` according
to the OpenAPI standard, or at ``/docs`` for a `SwaggerUI
<https://swagger.io/tools/swagger-ui/>`_ interface, or ``/redocs`` for
a `redoc <https://github.com/Redocly/redoc>`_ interface. Note that
there is currently no documentation standard for WebSockets.
a `redoc <https://github.com/Redocly/redoc>`_ interface, or
``/scalar`` for a `ScalaR <https://github.com/ScalaR/ScalaR>`_
interface. Note that there is currently no documentation standard for
WebSockets.

Contributing
------------
Expand Down
34 changes: 34 additions & 0 deletions src/quart_schema/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@
</body>
"""

SCALAR_TEMPLATE = """
<head>
<title>{{ title }}</title>
<style>
body {
margin: 0;
}
</style>
</head>
<body>
<script data-url="{{ openapi_path }}"></script>
<script src="{{ scalar_js_url }}"></script>
</body>
"""

SWAGGER_TEMPLATE = """
<head>
<link type="text/css" rel="stylesheet" href="{{ swagger_css_url }}">
Expand Down Expand Up @@ -159,6 +174,8 @@ def create_app():
to disable documentation.
redoc_ui_path: The path used to serve the documentation UI using
redoc or None to disable redoc documentation.
scalar_ui_path: The path used to serve the documentation UI using
scalar or None to disable scalar documentation.
swagger_ui_path: The path used to serve the documentation UI using
swagger or None to disable swagger documentation.
info: A OpenAPI Info object describing the API.
Expand All @@ -176,6 +193,7 @@ def __init__(
*,
openapi_path: Optional[str] = "/openapi.json",
redoc_ui_path: Optional[str] = "/redocs",
scalar_ui_path: Optional[str] = "/scalar",
swagger_ui_path: Optional[str] = "/docs",
info: Optional[Union[Info, dict]] = None,
tags: Optional[List[Union[Tag, dict]]] = None,
Expand All @@ -188,6 +206,7 @@ def __init__(
) -> None:
self.openapi_path = openapi_path
self.redoc_ui_path = redoc_ui_path
self.scalar_ui_path = scalar_ui_path
self.swagger_ui_path = swagger_ui_path

self.convert_casing = convert_casing
Expand Down Expand Up @@ -263,6 +282,10 @@ def init_app(self, app: Quart) -> None:
"QUART_SCHEMA_REDOC_JS_URL",
"https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js",
)
app.config.setdefault(
"QUART_SCHEMA_SCALAR_JS_URL",
"https://cdn.jsdelivr.net/npm/@scalar/api-reference",
)
app.config.setdefault("QUART_SCHEMA_BY_ALIAS", False)
app.config.setdefault("QUART_SCHEMA_CONVERT_CASING", self.convert_casing)
app.config.setdefault("QUART_SCHEMA_CONVERSION_PREFERENCE", self.conversion_preference)
Expand All @@ -271,6 +294,8 @@ def init_app(self, app: Quart) -> None:
app.add_url_rule(self.openapi_path, "openapi", self.openapi)
if self.redoc_ui_path is not None:
app.add_url_rule(self.redoc_ui_path, "redoc_ui", self.redoc_ui)
if self.scalar_ui_path is not None:
app.add_url_rule(self.scalar_ui_path, "scalar_ui", self.scalar_ui)
if self.swagger_ui_path is not None:
app.add_url_rule(self.swagger_ui_path, "swagger_ui", self.swagger_ui)

Expand Down Expand Up @@ -299,6 +324,15 @@ async def redoc_ui(self) -> str:
redoc_js_url=current_app.config["QUART_SCHEMA_REDOC_JS_URL"],
)

@hide
async def scalar_ui(self) -> str:
return await render_template_string(
SCALAR_TEMPLATE,
title=self.info.title,
openapi_path=self.openapi_path,
scalar_js_url=current_app.config["QUART_SCHEMA_SCALAR_JS_URL"],
)


@click.command("schema")
@click.option(
Expand Down

0 comments on commit ba57153

Please sign in to comment.