|
| 1 | +/** |
| 2 | + * Pyodide-powered "▶ Run" button for code cells. |
| 3 | + * |
| 4 | + * Adds a Run button next to the Copy button on every code-cell block. |
| 5 | + * Clicking Run loads Pyodide (WebAssembly CPython) on first use, then |
| 6 | + * executes the cell and shows output inline. |
| 7 | + */ |
| 8 | +(function () { |
| 9 | + "use strict"; |
| 10 | + |
| 11 | + const PYODIDE_CDN = |
| 12 | + "https://cdn.jsdelivr.net/pyodide/v0.26.4/full/pyodide.js"; |
| 13 | + |
| 14 | + let pyodidePromise = null; |
| 15 | + |
| 16 | + /** Lazy-load Pyodide the first time someone clicks Run. */ |
| 17 | + function loadPyodide() { |
| 18 | + if (pyodidePromise) return pyodidePromise; |
| 19 | + |
| 20 | + pyodidePromise = new Promise((resolve, reject) => { |
| 21 | + const script = document.createElement("script"); |
| 22 | + script.src = PYODIDE_CDN; |
| 23 | + script.onload = async () => { |
| 24 | + try { |
| 25 | + /* global loadPyodide – provided by the CDN script */ |
| 26 | + const py = await globalThis.loadPyodide(); |
| 27 | + resolve(py); |
| 28 | + } catch (err) { |
| 29 | + reject(err); |
| 30 | + } |
| 31 | + }; |
| 32 | + script.onerror = reject; |
| 33 | + document.head.appendChild(script); |
| 34 | + }); |
| 35 | + return pyodidePromise; |
| 36 | + } |
| 37 | + |
| 38 | + /** Run a code string and return { stdout, stderr }. */ |
| 39 | + async function runCode(pyodide, code) { |
| 40 | + // Redirect stdout / stderr so we can capture them. |
| 41 | + pyodide.runPython(` |
| 42 | +import sys, io |
| 43 | +sys.stdout = io.StringIO() |
| 44 | +sys.stderr = io.StringIO() |
| 45 | +`); |
| 46 | + try { |
| 47 | + const result = pyodide.runPython(code); |
| 48 | + const stdout = pyodide.runPython("sys.stdout.getvalue()"); |
| 49 | + const stderr = pyodide.runPython("sys.stderr.getvalue()"); |
| 50 | + let output = stdout; |
| 51 | + if (stderr) output += stderr; |
| 52 | + if (result !== undefined && result !== null && String(result) !== "None") { |
| 53 | + output += String(result); |
| 54 | + } |
| 55 | + return { output: output, error: false }; |
| 56 | + } catch (err) { |
| 57 | + const stderr = pyodide.runPython("sys.stderr.getvalue()"); |
| 58 | + return { output: stderr + "\n" + err.message, error: true }; |
| 59 | + } finally { |
| 60 | + pyodide.runPython(` |
| 61 | +sys.stdout = sys.__stdout__ |
| 62 | +sys.stderr = sys.__stderr__ |
| 63 | +`); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** Create or update the output area below a code block. */ |
| 68 | + function showOutput(container, text, isError) { |
| 69 | + let outputEl = container.querySelector(".pyodide-output"); |
| 70 | + if (!outputEl) { |
| 71 | + outputEl = document.createElement("pre"); |
| 72 | + outputEl.className = "pyodide-output"; |
| 73 | + container.appendChild(outputEl); |
| 74 | + } |
| 75 | + outputEl.textContent = text || "(no output)"; |
| 76 | + outputEl.classList.toggle("pyodide-error", isError); |
| 77 | + outputEl.style.display = "block"; |
| 78 | + } |
| 79 | + |
| 80 | + /** Inject a Run button into a code-cell container. */ |
| 81 | + function addRunButton(container) { |
| 82 | + const btn = document.createElement("button"); |
| 83 | + btn.className = "pyodide-run-btn"; |
| 84 | + btn.title = "Run this code (Pyodide)"; |
| 85 | + btn.textContent = "▶ Run"; |
| 86 | + |
| 87 | + btn.addEventListener("click", async () => { |
| 88 | + const codeEl = container.querySelector("pre code, pre"); |
| 89 | + if (!codeEl) return; |
| 90 | + const code = codeEl.textContent; |
| 91 | + |
| 92 | + btn.disabled = true; |
| 93 | + btn.textContent = "⏳ Loading…"; |
| 94 | + |
| 95 | + try { |
| 96 | + const pyodide = await loadPyodide(); |
| 97 | + btn.textContent = "⏳ Running…"; |
| 98 | + const { output, error } = await runCode(pyodide, code); |
| 99 | + showOutput(container, output, error); |
| 100 | + } catch (err) { |
| 101 | + showOutput(container, "Failed to load Pyodide:\n" + err.message, true); |
| 102 | + } finally { |
| 103 | + btn.disabled = false; |
| 104 | + btn.textContent = "▶ Run"; |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + // Insert at top of the container, next to the copy button area |
| 109 | + const highlight = container.querySelector(".highlight"); |
| 110 | + if (highlight) { |
| 111 | + highlight.style.position = "relative"; |
| 112 | + highlight.appendChild(btn); |
| 113 | + } else { |
| 114 | + container.prepend(btn); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /** Find all code-cell blocks and attach Run buttons. */ |
| 119 | + function init() { |
| 120 | + // myst-nb renders {code-cell} as <div class="cell ..."><div class="cell_input">... |
| 121 | + const cells = document.querySelectorAll(".cell .cell_input"); |
| 122 | + cells.forEach(addRunButton); |
| 123 | + |
| 124 | + // Also target plain highlighted python blocks produced by myst-nb |
| 125 | + if (cells.length === 0) { |
| 126 | + document |
| 127 | + .querySelectorAll('div.highlight-python, div.highlight-default') |
| 128 | + .forEach(function (block) { |
| 129 | + // Skip if already handled |
| 130 | + if (block.closest(".cell_input")) return; |
| 131 | + addRunButton(block); |
| 132 | + }); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + if (document.readyState === "loading") { |
| 137 | + document.addEventListener("DOMContentLoaded", init); |
| 138 | + } else { |
| 139 | + init(); |
| 140 | + } |
| 141 | +})(); |
0 commit comments