|
| 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, string); } }); |
| 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, error); |
| 26 | + } |
| 27 | + if (result) writeOutput(output, result); |
| 28 | + hljs.highlightElement(output); |
| 29 | +} |
| 30 | + |
| 31 | +async function initPyodide() { |
| 32 | + let pyodide = await loadPyodide(); |
| 33 | + await pyodide.loadPackage("micropip"); |
| 34 | + return pyodide; |
| 35 | +} |
| 36 | + |
| 37 | +function getTheme() { |
| 38 | + return document.body.getAttribute('data-md-color-scheme'); |
| 39 | +} |
| 40 | + |
| 41 | +function setTheme(editor, currentTheme, light, dark) { |
| 42 | + // https://gist.github.com/RyanNutt/cb8d60997d97905f0b2aea6c3b5c8ee0 |
| 43 | + if (currentTheme === "default") { |
| 44 | + editor.setTheme("ace/theme/" + light); |
| 45 | + document.querySelector(`link[title="light"]`).removeAttribute("disabled"); |
| 46 | + document.querySelector(`link[title="dark"]`).setAttribute("disabled", "disabled"); |
| 47 | + } else if (currentTheme === "slate") { |
| 48 | + editor.setTheme("ace/theme/" + dark); |
| 49 | + document.querySelector(`link[title="dark"]`).removeAttribute("disabled"); |
| 50 | + document.querySelector(`link[title="light"]`).setAttribute("disabled", "disabled"); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +function updateTheme(editor, light, dark) { |
| 55 | + // Create a new MutationObserver instance |
| 56 | + const observer = new MutationObserver((mutations) => { |
| 57 | + // Loop through the mutations that occurred |
| 58 | + mutations.forEach((mutation) => { |
| 59 | + // Check if the mutation was a change to the data-md-color-scheme attribute |
| 60 | + if (mutation.attributeName === 'data-md-color-scheme') { |
| 61 | + // Get the new value of the attribute |
| 62 | + const newColorScheme = mutation.target.getAttribute('data-md-color-scheme'); |
| 63 | + // Update the editor theme |
| 64 | + setTheme(editor, newColorScheme, light, dark); |
| 65 | + } |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + // Configure the observer to watch for changes to the data-md-color-scheme attribute |
| 70 | + observer.observe(document.body, { |
| 71 | + attributes: true, |
| 72 | + attributeFilter: ['data-md-color-scheme'], |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +async function setupPyodide(idPrefix, install = null, themeLight = 'tomorrow', themeDark = 'tomorrow_night', session = null) { |
| 77 | + const editor = ace.edit(idPrefix + "editor"); |
| 78 | + const run = document.getElementById(idPrefix + "run"); |
| 79 | + const clear = document.getElementById(idPrefix + "clear"); |
| 80 | + const output = document.getElementById(idPrefix + "output"); |
| 81 | + |
| 82 | + updateTheme(editor, themeLight, themeDark); |
| 83 | + |
| 84 | + editor.session.setMode("ace/mode/python"); |
| 85 | + setTheme(editor, getTheme(), themeLight, themeDark); |
| 86 | + |
| 87 | + writeOutput(output, "Initializing..."); |
| 88 | + let pyodide = await pyodidePromise; |
| 89 | + if (install && install.length) { |
| 90 | + micropip = pyodide.pyimport("micropip"); |
| 91 | + for (const package of install) |
| 92 | + await micropip.install(package); |
| 93 | + } |
| 94 | + clearOutput(output); |
| 95 | + run.onclick = () => evaluatePython(pyodide, editor, output, session); |
| 96 | + clear.onclick = () => clearOutput(output); |
| 97 | + output.parentElement.parentElement.addEventListener("keydown", (event) => { |
| 98 | + if (event.ctrlKey && event.key.toLowerCase() === 'enter') { |
| 99 | + event.preventDefault(); |
| 100 | + run.click(); |
| 101 | + } |
| 102 | + }); |
| 103 | +} |
| 104 | + |
| 105 | +var pyodidePromise = initPyodide(); |
0 commit comments