Skip to content

Commit 6f40a13

Browse files
committed
docs: use anywidget
Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
1 parent ec03f06 commit 6f40a13

5 files changed

Lines changed: 111 additions & 4 deletions

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* AnyWidget wrapper for repo-review app
3+
* Provides the render() interface for MyST's anywidget directive
4+
*/
5+
6+
export async function render({ model, el }) {
7+
// Create a root container for the React app
8+
const root = document.createElement("div");
9+
root.id = "root";
10+
root.style.width = "100%";
11+
root.style.minHeight = "600px";
12+
13+
// Add Material Design fonts if not already present
14+
ensureFonts();
15+
16+
// Append root to the target element
17+
el.appendChild(root);
18+
19+
// Determine the correct path to the app
20+
// The widget will be in /build/ and the app is in /assets/js/
21+
const baseUrl = new URL(import.meta.url);
22+
const appUrl = new URL("../../assets/js/repo-review-app.min.js", baseUrl).href;
23+
24+
// Load the app via dynamic import
25+
try {
26+
const appModule = await import(appUrl);
27+
28+
// The app should export a mountApp function
29+
if (appModule.mountApp) {
30+
appModule.mountApp({
31+
header: true,
32+
deps: [
33+
"repo-review~=1.0.0",
34+
"sp-repo-review==2026.04.04",
35+
"validate-pyproject[all]~=0.25.0",
36+
"validate-pyproject-schema-store==2026.03.29",
37+
],
38+
});
39+
} else {
40+
console.warn(
41+
"repo-review-app.min.js does not export mountApp, attempting alternative load"
42+
);
43+
// Try loading as a script tag instead
44+
loadAppAsScript(appUrl);
45+
}
46+
} catch (error) {
47+
console.error("Failed to load repo-review app via import:", error);
48+
// Fallback: load as a regular script
49+
const appSrc = appUrl.replace(/\.mjs$/, ".js").replace(/\.js$/, ".min.js");
50+
loadAppAsScript(appSrc);
51+
}
52+
53+
function loadAppAsScript(src) {
54+
const script = document.createElement("script");
55+
script.type = "module";
56+
script.textContent = `import('./repo-review-app.min.js').then(m => m.mountApp ? m.mountApp({
57+
header: true,
58+
deps: [
59+
"repo-review~=1.0.0",
60+
"sp-repo-review==2026.04.04",
61+
"validate-pyproject[all]~=0.25.0",
62+
"validate-pyproject-schema-store==2026.03.29",
63+
],
64+
}) : console.error("mountApp not found")).catch(e => {
65+
root.innerHTML = "<p style='color: red; padding: 20px;'>Failed to load repo-review app. Please check the browser console for details.</p>";
66+
console.error("Failed to load repo-review app:", e);
67+
});`;
68+
document.head.appendChild(script);
69+
}
70+
}
71+
72+
/**
73+
* Ensure Material Design fonts are loaded
74+
*/
75+
function ensureFonts() {
76+
// Check if fonts are already loaded
77+
if (document.querySelector('link[href*="fonts.googleapis.com"]')) {
78+
return;
79+
}
80+
81+
// Add Roboto font for Material Design
82+
const robotoLink = document.createElement("link");
83+
robotoLink.rel = "stylesheet";
84+
robotoLink.href =
85+
"https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap";
86+
document.head.appendChild(robotoLink);
87+
88+
// Add Material Icons
89+
const iconsLink = document.createElement("link");
90+
iconsLink.rel = "stylesheet";
91+
iconsLink.href = "https://fonts.googleapis.com/icon?family=Material+Icons";
92+
document.head.appendChild(iconsLink);
93+
}

docs/myst.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@ project:
4343
- file: pages/guides/repo_review.md
4444

4545
site:
46+
template: book-theme
4647
nav:
4748
- title: Scientific Python
4849
url: https://scientific-python.org
4950
- title: Learn
5051
url: https://learn.scientific-python.org
51-
- title: Cookie
52+
actions:
53+
- title: src
5254
url: https://github.com/scientific-python/cookie
5355
options:
5456
favicon: assets/favicon.ico

docs/pages/guides/repo_review.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,5 @@ pipx run 'sp-repo-review[cli]' <path to repo>
2121

2222
---
2323

24-
:::{iframe} <https://scientific-python.github.io/repo-review/>
25-
:width: 100%
24+
:::{anywidget} /assets/js/repo-review-widget.mjs
2625
:::

helpers/copy-assets.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# Copy assets to both build outputs
5+
for build_dir in docs/_build/html docs/_build/site/public; do
6+
if [ -d "$build_dir" ]; then
7+
mkdir -p "$build_dir/assets/js"
8+
cp -v docs/assets/js/repo-review-app.min.js "$build_dir/assets/js/"
9+
cp -v docs/assets/js/repo-review-app.min.js.map "$build_dir/assets/js/"
10+
fi
11+
done
12+
13+
echo "✓ Assets copied successfully"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"private": true,
44
"type": "module",
55
"scripts": {
6-
"build": "cd docs && myst build --html",
6+
"build": "cd docs && myst build --html && cd .. && bash helpers/copy-assets.sh",
77
"serve": "cd docs && myst start",
88
"clean": "rm -rf docs/_build"
99
},

0 commit comments

Comments
 (0)