Skip to content

Commit a6a978a

Browse files
committed
Deployed 762b3dc with MkDocs version: 1.6.1
0 parents  commit a6a978a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+8368
-0
lines changed

.nojekyll

Whitespace-only changes.

404.html

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

assets/_markdown_exec_pyodide.css

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
html[data-theme="light"] {
2+
@import "https://cdn.jsdelivr.net/npm/highlightjs-themes@1.0.0/tomorrow.css"
3+
}
4+
5+
html[data-theme="dark"] {
6+
@import "https://cdn.jsdelivr.net/npm/highlightjs-themes@1.0.0/tomorrow-night-blue.min.css"
7+
}
8+
9+
10+
.ace_gutter {
11+
z-index: 1;
12+
}
13+
14+
.pyodide-editor {
15+
width: 100%;
16+
font-size: .85em;
17+
}
18+
19+
.pyodide-editor-bar {
20+
color: var(--md-primary-bg-color);
21+
background-color: var(--md-primary-fg-color);
22+
width: 100%;
23+
font: monospace;
24+
font-size: 0.75em;
25+
padding: 2px 0 2px;
26+
}
27+
28+
.pyodide-bar-item {
29+
padding: 0 18px 0;
30+
display: inline-block;
31+
width: 50%;
32+
}
33+
34+
.pyodide pre {
35+
margin: 0;
36+
}
37+
38+
.pyodide-output {
39+
width: 100%;
40+
margin-bottom: -15px;
41+
min-height: 46px;
42+
max-height: 400px
43+
}
44+
45+
.pyodide-clickable {
46+
cursor: pointer;
47+
text-align: right;
48+
}
49+
50+
/* For themes other than Material. */
51+
.pyodide .twemoji svg {
52+
width: 1rem;
53+
}

assets/_markdown_exec_pyodide.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
var _sessions = {};
2+
3+
function getSession(name, pyodide) {
4+
if (!(name in _sessions)) {
5+
_sessions[name] = pyodide.globals.get("dict")();
6+
}
7+
return _sessions[name];
8+
}
9+
10+
function writeOutput(element, string) {
11+
element.innerHTML += string + '\n';
12+
}
13+
14+
function clearOutput(element) {
15+
element.innerHTML = '';
16+
}
17+
18+
async function evaluatePython(pyodide, editor, output, session) {
19+
pyodide.setStdout({ batched: (string) => { writeOutput(output, new Option(string).innerHTML); } });
20+
let result, code = editor.getValue();
21+
clearOutput(output);
22+
try {
23+
result = await pyodide.runPythonAsync(code, { globals: getSession(session, pyodide) });
24+
} catch (error) {
25+
writeOutput(output, new Option(error.toString()).innerHTML);
26+
}
27+
if (result) writeOutput(output, new Option(result).innerHTML);
28+
hljs.highlightElement(output);
29+
}
30+
31+
async function initPyodide() {
32+
try {
33+
let pyodide = await loadPyodide();
34+
await pyodide.loadPackage("micropip");
35+
return pyodide;
36+
} catch(error) {
37+
return null;
38+
}
39+
}
40+
41+
function getTheme() {
42+
return document.body.getAttribute('data-md-color-scheme');
43+
}
44+
45+
function setTheme(editor, currentTheme, light, dark) {
46+
// https://gist.github.com/RyanNutt/cb8d60997d97905f0b2aea6c3b5c8ee0
47+
if (currentTheme === "default") {
48+
editor.setTheme("ace/theme/" + light);
49+
document.querySelector(`link[title="light"]`).removeAttribute("disabled");
50+
document.querySelector(`link[title="dark"]`).setAttribute("disabled", "disabled");
51+
} else if (currentTheme === "slate") {
52+
editor.setTheme("ace/theme/" + dark);
53+
document.querySelector(`link[title="dark"]`).removeAttribute("disabled");
54+
document.querySelector(`link[title="light"]`).setAttribute("disabled", "disabled");
55+
}
56+
}
57+
58+
function updateTheme(editor, light, dark) {
59+
// Create a new MutationObserver instance
60+
const observer = new MutationObserver((mutations) => {
61+
// Loop through the mutations that occurred
62+
mutations.forEach((mutation) => {
63+
// Check if the mutation was a change to the data-md-color-scheme attribute
64+
if (mutation.attributeName === 'data-md-color-scheme') {
65+
// Get the new value of the attribute
66+
const newColorScheme = mutation.target.getAttribute('data-md-color-scheme');
67+
// Update the editor theme
68+
setTheme(editor, newColorScheme, light, dark);
69+
}
70+
});
71+
});
72+
73+
// Configure the observer to watch for changes to the data-md-color-scheme attribute
74+
observer.observe(document.body, {
75+
attributes: true,
76+
attributeFilter: ['data-md-color-scheme'],
77+
});
78+
}
79+
80+
async function setupPyodide(
81+
idPrefix,
82+
install = null,
83+
themeLight = 'tomorrow',
84+
themeDark = 'tomorrow_night',
85+
session = null,
86+
minLines = 5,
87+
maxLines = 30,
88+
) {
89+
const editor = ace.edit(idPrefix + "editor");
90+
const run = document.getElementById(idPrefix + "run");
91+
const clear = document.getElementById(idPrefix + "clear");
92+
const output = document.getElementById(idPrefix + "output");
93+
94+
updateTheme(editor, themeLight, themeDark);
95+
96+
editor.session.setMode("ace/mode/python");
97+
setTheme(editor, getTheme(), themeLight, themeDark);
98+
99+
editor.setOption("minLines", minLines);
100+
editor.setOption("maxLines", maxLines);
101+
102+
// Force editor to resize after setting options
103+
editor.resize();
104+
105+
writeOutput(output, "Initializing...");
106+
let pyodide = await pyodidePromise;
107+
if (install && install.length) {
108+
try {
109+
micropip = pyodide.pyimport("micropip");
110+
for (const package of install)
111+
await micropip.install(package);
112+
clearOutput(output);
113+
} catch (error) {
114+
clearOutput(output);
115+
writeOutput(output, `Could not install one or more packages: ${install.join(", ")}\n`);
116+
writeOutput(output, new Option(error.toString()).innerHTML);
117+
}
118+
} else {
119+
clearOutput(output);
120+
}
121+
run.onclick = () => evaluatePython(pyodide, editor, output, session);
122+
clear.onclick = () => clearOutput(output);
123+
output.parentElement.parentElement.addEventListener("keydown", (event) => {
124+
if (event.ctrlKey && event.key.toLowerCase() === 'enter') {
125+
event.preventDefault();
126+
run.click();
127+
}
128+
});
129+
}
130+
131+
var pyodidePromise = initPyodide();

0 commit comments

Comments
 (0)