Skip to content

Commit 0a987b5

Browse files
authored
Implement Scalar API docs page at /scalar (#87)
On this branch, I added an endpoint at `/scalar`, which returns Scalar API docs (which is an alternative to Swagger UI—the two coexist). --------- Co-authored-by: eecavanna <eecavanna@users.noreply.github.com>
1 parent f6e137c commit 0a987b5

4 files changed

Lines changed: 27 additions & 1 deletion

File tree

mongodb/ingest_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def main():
198198
parser.add_argument("--db-name", default="bertron", help="MongoDB database name")
199199
parser.add_argument(
200200
"--schema-path",
201-
default="https://raw.githubusercontent.com/ber-data/bertron-schema/refs/heads/main/src/schema/jsonschema/bertron_schema.json",
201+
default="https://raw.githubusercontent.com/ber-data/bertron-schema/96cbe257717d44137440be369a8414d153579926/src/schema/jsonschema/bertron_schema.json",
202202
help="Path or URL to the BERtron schema JSON file",
203203
)
204204
parser.add_argument(

pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ dependencies = [
3232
"nmdc-api-utilities>=0.3.9",
3333
"pydantic-settings>=2.10.1",
3434
"pymongo>=4.13.1",
35+
# Note: We use `scalar-fastapi` to integrate Scalar API documentation with FastAPI.
36+
# Note: We use a Git URL to install the `scalar-fastapi` package, because the Scalar
37+
# maintainers have not published recent versions of the package to PyPI yet.
38+
# Reference: https://github.com/scalar/scalar/issues/5337#issuecomment-2781011096
39+
"scalar-fastapi @ git+https://github.com/scalar/scalar/#subdirectory=integrations/fastapi",
3540
"uvicorn>=0.34.3",
3641
]
3742

src/server.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from fastapi import FastAPI, HTTPException, Query
55
from fastapi.responses import RedirectResponse
66
from pymongo import MongoClient
7+
from scalar_fastapi import get_scalar_api_reference
78
from schema.datamodel.bertron_schema_pydantic import Entity
89
import uvicorn
910

@@ -38,6 +39,19 @@
3839
)
3940

4041

42+
@app.get("/scalar", include_in_schema=False)
43+
async def get_scalar_html():
44+
r"""
45+
Returns the HTML markup for an interactive API docs web page powered by Scalar.
46+
47+
Note: This can coexist with FastAPI's built-in Swagger UI page.
48+
"""
49+
return get_scalar_api_reference(
50+
openapi_url=app.openapi_url,
51+
title="BERtron API",
52+
)
53+
54+
4155
@app.get("/", include_in_schema=False)
4256
def get_root():
4357
r"""Redirect to the API documentation page."""

uv.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)