Skip to content

Commit d22a8da

Browse files
authored
documentation: redoc -> scalar (#559)
* use scalar instead of redoc for the docs
1 parent b171205 commit d22a8da

File tree

2 files changed

+82
-22
lines changed

2 files changed

+82
-22
lines changed

.github/workflows/build-test-doc_upload.yaml

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,22 @@ on:
55
branches:
66
- main
77
pull_request:
8+
types: [opened, reopened, synchronize, ready_for_review]
9+
10+
concurrency:
11+
group: ${{ github.workflow }}-${{ github.ref }}
12+
cancel-in-progress: true
13+
814
permissions:
915
contents: write
1016
jobs:
1117
build-test-doc:
1218
runs-on: ubuntu-latest
1319
timeout-minutes: 20
14-
steps:
1520

21+
if: github.repository_owner == 'fritz-marshal' && github.event.pull_request.draft == false
22+
23+
steps:
1624
- uses: actions/checkout@v4
1725
with:
1826
submodules: recursive
@@ -25,18 +33,38 @@ jobs:
2533
with:
2634
node-version: 20
2735

28-
- uses: actions/cache@v2
36+
- uses: actions/cache@v4
2937
with:
3038
path: |
3139
~/.npm
3240
key: ${{ runner.os }}-npm-${{ hashFiles('package.json') }}
3341

34-
- uses: actions/cache@v2
42+
- uses: actions/cache@v4
3543
with:
3644
path: |
3745
~/.cache/pip
3846
key: ${{ runner.os }}-${{ hashFiles('**/requirements*.txt') }}
3947

48+
# caching dustmap & sncosmo files is dependent on their corresponding
49+
# python package versions, so we use that as the cache key
50+
- uses: actions/cache@v4
51+
with:
52+
path: |
53+
persistentdata/dustmap/sfd
54+
key: ${{ runner.os }}-dustmap-${{ hashFiles('**/requirements*.txt') }}
55+
restore-keys: |
56+
${{ runner.os }}-dustmap-${{ hashFiles('**/requirements*.txt') }}
57+
${{ runner.os }}-dustmap-
58+
59+
- uses: actions/cache@v4
60+
with:
61+
path: |
62+
~/.astropy/cache/sncosmo/bandpasses
63+
key: ${{ runner.os }}-sncosmo-${{ hashFiles('**/requirements*.txt') }}
64+
restore-keys: |
65+
${{ runner.os }}-sncosmo-${{ hashFiles('**/requirements*.txt') }}
66+
${{ runner.os }}-sncosmo-
67+
4068
- name: Install system dependencies
4169
run: |
4270
sudo apt update -y && sudo apt install -y libcurl4-gnutls-dev libgnutls28-dev

launcher/commands/doc.py

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,58 @@
11
import subprocess
22
import os
33
import json
4+
import jinja2
45

56
from launcher.config import check_config
67
from launcher.skyportal import patch as patch_skyportal
78

89

10+
def patch_api_doc_template():
11+
"""Patch the API documentation template with the OpenAPI spec.
12+
13+
This function reads the OpenAPI specification from the openapi.json file,
14+
populates it with server information from the configuration, and renders
15+
a HTML documentation page from https://github.com/scalar/scalar
16+
using a Jinja2 template.
17+
18+
Raises:
19+
ValueError: If the server information is not valid.
20+
"""
21+
# open the openapi.json file generated by build-spec.py
22+
from baselayer.app.env import load_env
23+
24+
_, cfg = load_env()
25+
26+
with open("openapi.json") as f:
27+
openapi_spec = json.load(f)
28+
29+
# populate the OpenAPI spec with (optional) server information
30+
servers = cfg.get("docs.servers", [])
31+
if servers is None:
32+
servers = []
33+
if not isinstance(servers, list):
34+
raise ValueError("API servers must be a list.")
35+
36+
for server in servers:
37+
if not all(k in server for k in ("url", "description")):
38+
raise ValueError("Each server must have 'url' and 'description' keys.")
39+
openapi_spec["servers"] = servers
40+
41+
# Create a Jinja2 template environment
42+
template_env = jinja2.Environment(loader=jinja2.FileSystemLoader(searchpath="./"))
43+
44+
# Load the template file
45+
template = template_env.get_template("skyportal/doc/openapi.html.template")
46+
47+
# Render the template with the OpenAPI spec
48+
output = template.render(openapi_spec=json.dumps(openapi_spec, indent=2))
49+
50+
# Write the output to a new HTML file
51+
os.makedirs("./doc/_build/html", exist_ok=True)
52+
with open("./doc/_build/html/api.html", "w") as f:
53+
f.write(output)
54+
55+
956
def doc(yes: bool = False, upload: bool = False):
1057
"""Build the documentation
1158
@@ -43,27 +90,12 @@ def doc(yes: bool = False, upload: bool = False):
4390
"servers": [{"url": "https://fritz.science"}],
4491
},
4592
)
46-
with open("skyportal/openapi.json", "w") as f:
93+
with open("openapi.json", "w") as f:
4794
json.dump(spec.to_dict(), f)
4895

49-
subprocess.run(
50-
[
51-
"npx",
52-
"redoc-cli@0.13.21",
53-
"bundle",
54-
"openapi.json",
55-
"--title",
56-
"Fritz API docs",
57-
"--cdn",
58-
"--options.theme.logo.gutter",
59-
"2rem",
60-
"-o",
61-
"../doc/_build/html/api.html",
62-
],
63-
check=True,
64-
cwd="skyportal",
65-
)
66-
os.remove("skyportal/openapi.json")
96+
patch_api_doc_template()
97+
98+
os.remove("openapi.json")
6799

68100
if upload:
69101
subprocess.run(

0 commit comments

Comments
 (0)