@pyscript/core 0.3.11 → 0.3.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core-30w338Df.js +3 -0
- package/dist/core-30w338Df.js.map +1 -0
- package/dist/core.js +1 -1
- package/dist/{deprecations-manager-WuZTBZG0.js → deprecations-manager-Jepjjhu6.js} +2 -2
- package/dist/{deprecations-manager-WuZTBZG0.js.map → deprecations-manager-Jepjjhu6.js.map} +1 -1
- package/dist/{error-6r_xlA-Y.js → error-KetuWjDJ.js} +2 -2
- package/dist/{error-6r_xlA-Y.js.map → error-KetuWjDJ.js.map} +1 -1
- package/dist/{py-editor-MQAOiLee.js → py-editor-AC_7LQst.js} +2 -2
- package/dist/{py-editor-MQAOiLee.js.map → py-editor-AC_7LQst.js.map} +1 -1
- package/dist/{py-terminal-wDa_Xkqt.js → py-terminal-iIRy_gMx.js} +2 -2
- package/dist/{py-terminal-wDa_Xkqt.js.map → py-terminal-iIRy_gMx.js.map} +1 -1
- package/package.json +2 -2
- package/src/stdlib/pyscript/magic_js.py +15 -1
- package/src/stdlib/pyscript.js +1 -1
- package/dist/core-P7cR7OAZ.js +0 -3
- package/dist/core-P7cR7OAZ.js.map +0 -1
package/dist/core.js
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
export{a as PyWorker,T as TYPES,b as config,e as hooks,c as whenDefined}from"./core-
|
1
|
+
export{a as PyWorker,T as TYPES,b as config,e as hooks,c as whenDefined}from"./core-30w338Df.js";
|
2
2
|
//# sourceMappingURL=core.js.map
|
@@ -1,2 +1,2 @@
|
|
1
|
-
import{e}from"./core-
|
2
|
-
//# sourceMappingURL=deprecations-manager-
|
1
|
+
import{e}from"./core-30w338Df.js";import{notify as o}from"./error-KetuWjDJ.js";function r(){const e=document.querySelectorAll("script");for(const o of e)s(o.src)}function s(e){/\/pyscript\.net\/latest/.test(e)&&o("Loading scripts from latest is deprecated and will be removed soon. Please use a specific version instead.")}e.main.onReady.add(r),e.main.onWorker.add(r);
|
2
|
+
//# sourceMappingURL=deprecations-manager-Jepjjhu6.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"deprecations-manager-
|
1
|
+
{"version":3,"file":"deprecations-manager-Jepjjhu6.js","sources":["../src/plugins/deprecations-manager.js"],"sourcesContent":["// PyScript Derepcations Plugin\nimport { hooks } from \"../core.js\";\nimport { notify } from \"./error.js\";\n\n// react lazily on PyScript bootstrap\nhooks.main.onReady.add(checkDeprecations);\nhooks.main.onWorker.add(checkDeprecations);\n\n/**\n * Check that there are no scripts loading from pyscript.net/latest\n */\nfunction checkDeprecations() {\n const scripts = document.querySelectorAll(\"script\");\n for (const script of scripts) checkLoadingScriptsFromLatest(script.src);\n}\n\n/**\n * Check if src being loaded from pyscript.net/latest and display a notification if true\n * * @param {string} src\n */\nfunction checkLoadingScriptsFromLatest(src) {\n if (/\\/pyscript\\.net\\/latest/.test(src)) {\n notify(\n \"Loading scripts from latest is deprecated and will be removed soon. Please use a specific version instead.\",\n );\n }\n}\n"],"names":["checkDeprecations","scripts","document","querySelectorAll","script","checkLoadingScriptsFromLatest","src","test","notify","hooks","main","onReady","add","onWorker"],"mappings":"+EAWA,SAASA,IACL,MAAMC,EAAUC,SAASC,iBAAiB,UAC1C,IAAK,MAAMC,KAAUH,EAASI,EAA8BD,EAAOE,IACvE,CAMA,SAASD,EAA8BC,GAC/B,0BAA0BC,KAAKD,IAC/BE,EACI,6GAGZ,CArBAC,EAAMC,KAAKC,QAAQC,IAAIZ,GACvBS,EAAMC,KAAKG,SAASD,IAAIZ"}
|
@@ -1,2 +1,2 @@
|
|
1
|
-
import{e}from"./core-
|
2
|
-
//# sourceMappingURL=error-
|
1
|
+
import{e}from"./core-30w338Df.js";function n(e){const n=document.createElement("div");n.className="py-error",n.textContent=e,n.style.cssText="\n border: 1px solid red;\n background: #ffdddd;\n color: black;\n font-family: courier, monospace;\n white-space: pre;\n overflow-x: auto;\n padding: 8px;\n margin-top: 8px;\n ",document.body.append(n)}e.main.onReady.add((function o(r){e.main.onReady.delete(o);const{stderr:t}=r.io;r.io.stderr=(e,...o)=>(n(e.message||e),t(e,...o)),addEventListener("error",(({message:e})=>{e.startsWith("Uncaught PythonError")&&n(e)}))}));export{n as notify};
|
2
|
+
//# sourceMappingURL=error-KetuWjDJ.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"error-
|
1
|
+
{"version":3,"file":"error-KetuWjDJ.js","sources":["../src/plugins/error.js"],"sourcesContent":["// PyScript Error Plugin\nimport { hooks } from \"../core.js\";\n\nhooks.main.onReady.add(function override(pyScript) {\n // be sure this override happens only once\n hooks.main.onReady.delete(override);\n\n // trap generic `stderr` to propagate to it regardless\n const { stderr } = pyScript.io;\n\n // override it with our own logic\n pyScript.io.stderr = (error, ...rest) => {\n notify(error.message || error);\n // let other plugins or stderr hook, if any, do the rest\n return stderr(error, ...rest);\n };\n\n // be sure uncaught Python errors are also visible\n addEventListener(\"error\", ({ message }) => {\n if (message.startsWith(\"Uncaught PythonError\")) notify(message);\n });\n});\n\n// Error hook utilities\n\n// Custom function to show notifications\n\n/**\n * Add a banner to the top of the page, notifying the user of an error\n * @param {string} message\n */\nexport function notify(message) {\n const div = document.createElement(\"div\");\n div.className = \"py-error\";\n div.textContent = message;\n div.style.cssText = `\n border: 1px solid red;\n background: #ffdddd;\n color: black;\n font-family: courier, monospace;\n white-space: pre;\n overflow-x: auto;\n padding: 8px;\n margin-top: 8px;\n `;\n document.body.append(div);\n}\n"],"names":["notify","message","div","document","createElement","className","textContent","style","cssText","body","append","hooks","main","onReady","add","override","pyScript","delete","stderr","io","error","rest","addEventListener","startsWith"],"mappings":"kCA+BO,SAASA,EAAOC,GACnB,MAAMC,EAAMC,SAASC,cAAc,OACnCF,EAAIG,UAAY,WAChBH,EAAII,YAAcL,EAClBC,EAAIK,MAAMC,QAAU,6MAUpBL,SAASM,KAAKC,OAAOR,EACzB,CA3CAS,EAAMC,KAAKC,QAAQC,KAAI,SAASC,EAASC,GAErCL,EAAMC,KAAKC,QAAQI,OAAOF,GAG1B,MAAMG,OAAEA,GAAWF,EAASG,GAG5BH,EAASG,GAAGD,OAAS,CAACE,KAAUC,KAC5BrB,EAAOoB,EAAMnB,SAAWmB,GAEjBF,EAAOE,KAAUC,IAI5BC,iBAAiB,SAAS,EAAGrB,cACrBA,EAAQsB,WAAW,yBAAyBvB,EAAOC,EAAQ,GAEvE"}
|
@@ -1,2 +1,2 @@
|
|
1
|
-
import{T as e,d as t,X as r,H as n}from"./core-
|
2
|
-
//# sourceMappingURL=py-editor-
|
1
|
+
import{T as e,d as t,X as r,H as n}from"./core-30w338Df.js";let o=0;const i=e=>`${e}-editor-${o++}`,s=new Map,a={worker:{onReady:({runAsync:e,io:t},{sync:r})=>{t.stdout=e=>r.write(e),t.stderr=e=>r.writeErr(e),r.revoke(),r.runAsync=e}}};async function c({currentTarget:e}){const{env:t,pySrc:o,outDiv:i}=this;if(e.disabled=!0,i.innerHTML="",!s.has(t)){const e=URL.createObjectURL(new Blob([""])),o=r.call(new n(null,a),e,{type:this.interpreter}),{sync:i}=o,{promise:c,resolve:l}=Promise.withResolvers();s.set(t,c),i.revoke=()=>{URL.revokeObjectURL(e),l(o)}}s.get(t).then((t=>{t.onerror=({error:e})=>{i.innerHTML+=`<span style='color:red'>${e.message||e}</span>\n`,console.error(e)};const r=()=>{e.disabled=!1},{sync:n}=t;n.write=e=>{i.innerText+=`${e}\n`},n.writeErr=e=>{i.innerHTML+=`<span style='color:red'>${e}</span>\n`},n.runAsync(o).then(r,r)}))}const l=(e,t)=>{const r=document.createElement("div");r.className=`${t}-editor-input`,r.setAttribute("aria-label","Python Script Area");const n=((e,t)=>{const r=document.createElement("button");return r.className=`absolute ${t}-editor-run-button`,r.innerHTML='<svg style="height:20px;width:20px;vertical-align:-.125em;transform-origin:center;overflow:visible;color:green" viewBox="0 0 384 512" aria-hidden="true" role="img" xmlns="http://www.w3.org/2000/svg"><g transform="translate(192 256)" transform-origin="96 0"><g transform="translate(0,0) scale(1,1)"><path d="M361 215C375.3 223.8 384 239.3 384 256C384 272.7 375.3 288.2 361 296.1L73.03 472.1C58.21 482 39.66 482.4 24.52 473.9C9.377 465.4 0 449.4 0 432V80C0 62.64 9.377 46.63 24.52 38.13C39.66 29.64 58.21 29.99 73.03 39.04L361 215z" fill="currentColor" transform="translate(-192 -256)"></path></g></g></svg>',r.setAttribute("aria-label","Python Script Run Button"),r.addEventListener("click",e),r})(e,t),o=document.createElement("div");return o.addEventListener("keydown",(e=>{e.stopPropagation()})),r.append(n,o),r},d=(e,t)=>{const r=document.createElement("div");r.className=`${t}-editor-box`;const n=l(e,t),o=(e=>{const t=document.createElement("div");return t.className=`${e}-editor-output`,t.id=`${i(e)}-output`,t})(t);return r.append(n,o),[r,o]},u=async(e,r,n)=>{const[{basicSetup:o,EditorView:s},{Compartment:a},{python:l},{indentUnit:u},{keymap:m},{defaultKeymap:p}]=await Promise.all([import("./codemirror-ka0qQBe9.js"),import("./codemirror_state-zH-H1oSI.js"),import("./codemirror_lang-python-e_MPHeKq.js"),import("./codemirror_language-_x_LhvBl.js").then((function(e){return e.x})),import("./codemirror_view-7AFhckRk.js").then((function(e){return e.q})),import("./codemirror_commands-76da_Zjn.js")]),y=e.getAttribute("target");let v;if(y){if(v=document.getElementById(y)||document.querySelector(y),!v)throw new Error(`Unknown target ${y}`)}else v=document.createElement(`${r}-editor`),v.style.display="block",e.after(v);v.id||(v.id=i(r)),v.hasAttribute("exec-id")||v.setAttribute("exec-id",0),v.hasAttribute("root")||v.setAttribute("root",v.id);const f=`${n}-${e.getAttribute("env")||i(r)}`,g={interpreter:n,env:f,get pySrc(){return L.state.doc.toString()},get outDiv(){return w}},h=c.bind(g),[b,w]=d(h,r);b.dataset.env=e.hasAttribute("env")?f:n;const E=b.querySelector(`.${r}-editor-input > div`).attachShadow({mode:"open"});E.innerHTML="<style> :host { all: initial; }</style>",v.appendChild(b);const $=t(e.textContent).trim(),A=/^(\s+)/m.test($)?RegExp.$1:" ",L=new s({extensions:[u.of(A),(new a).of(l()),m.of([...p,{key:"Ctrl-Enter",run:h,preventDefault:!0},{key:"Cmd-Enter",run:h,preventDefault:!0},{key:"Shift-Enter",run:h,preventDefault:!0}]),o],parent:E,doc:$});L.focus()};let m=0;const p=()=>{m=0,y()},y=async()=>{if(!m){m=setTimeout(p,250);for(const[t,r]of e){const e=`script[type="${t}-editor"]`;for(const n of document.querySelectorAll(e))n.type+="-active",await u(n,t,r)}}};new MutationObserver(y).observe(document,{childList:!0,subtree:!0});var v=y();export{v as default};
|
2
|
+
//# sourceMappingURL=py-editor-AC_7LQst.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"py-editor-MQAOiLee.js","sources":["../src/plugins/py-editor.js"],"sourcesContent":["// PyScript py-editor plugin\nimport { Hook, XWorker, dedent } from \"polyscript/exports\";\nimport { TYPES } from \"../core.js\";\n\nconst RUN_BUTTON = `<svg style=\"height:20px;width:20px;vertical-align:-.125em;transform-origin:center;overflow:visible;color:green\" viewBox=\"0 0 384 512\" aria-hidden=\"true\" role=\"img\" xmlns=\"http://www.w3.org/2000/svg\"><g transform=\"translate(192 256)\" transform-origin=\"96 0\"><g transform=\"translate(0,0) scale(1,1)\"><path d=\"M361 215C375.3 223.8 384 239.3 384 256C384 272.7 375.3 288.2 361 296.1L73.03 472.1C58.21 482 39.66 482.4 24.52 473.9C9.377 465.4 0 449.4 0 432V80C0 62.64 9.377 46.63 24.52 38.13C39.66 29.64 58.21 29.99 73.03 39.04L361 215z\" fill=\"currentColor\" transform=\"translate(-192 -256)\"></path></g></g></svg>`;\n\nlet id = 0;\nconst getID = (type) => `${type}-editor-${id++}`;\n\nconst envs = new Map();\n\nconst hooks = {\n worker: {\n // works on both Pyodide and MicroPython\n onReady: ({ runAsync, io }, { sync }) => {\n io.stdout = (line) => sync.write(line);\n io.stderr = (line) => sync.writeErr(line);\n sync.revoke();\n sync.runAsync = runAsync;\n },\n },\n};\n\nasync function execute({ currentTarget }) {\n const { env, pySrc, outDiv } = this;\n\n currentTarget.disabled = true;\n outDiv.innerHTML = \"\";\n\n if (!envs.has(env)) {\n const srcLink = URL.createObjectURL(new Blob([\"\"]));\n const xworker = XWorker.call(new Hook(null, hooks), srcLink, {\n type: this.interpreter,\n });\n\n const { sync } = xworker;\n const { promise, resolve } = Promise.withResolvers();\n envs.set(env, promise);\n sync.revoke = () => {\n URL.revokeObjectURL(srcLink);\n resolve(xworker);\n };\n }\n\n // wait for the env then set the target div\n // before executing the current code\n envs.get(env).then((xworker) => {\n xworker.onerror = ({ error }) => {\n outDiv.innerHTML += `<span style='color:red'>${\n error.message || error\n }</span>\\n`;\n console.error(error);\n };\n\n const enable = () => {\n currentTarget.disabled = false;\n };\n const { sync } = xworker;\n sync.write = (str) => {\n outDiv.innerText += `${str}\\n`;\n };\n sync.writeErr = (str) => {\n outDiv.innerHTML += `<span style='color:red'>${str}</span>\\n`;\n };\n sync.runAsync(pySrc).then(enable, enable);\n });\n}\n\nconst makeRunButton = (listener, type) => {\n const runButton = document.createElement(\"button\");\n runButton.className = `absolute ${type}-editor-run-button`;\n runButton.innerHTML = RUN_BUTTON;\n runButton.setAttribute(\"aria-label\", \"Python Script Run Button\");\n runButton.addEventListener(\"click\", listener);\n return runButton;\n};\n\nconst makeEditorDiv = (listener, type) => {\n const editorDiv = document.createElement(\"div\");\n editorDiv.className = `${type}-editor-input`;\n editorDiv.setAttribute(\"aria-label\", \"Python Script Area\");\n\n const runButton = makeRunButton(listener, type);\n const editorShadowContainer = document.createElement(\"div\");\n\n // avoid outer elements intercepting key events (reveal as example)\n editorShadowContainer.addEventListener(\"keydown\", (event) => {\n event.stopPropagation();\n });\n\n editorDiv.append(runButton, editorShadowContainer);\n\n return editorDiv;\n};\n\nconst makeOutDiv = (type) => {\n const outDiv = document.createElement(\"div\");\n outDiv.className = `${type}-editor-output`;\n outDiv.id = `${getID(type)}-output`;\n return outDiv;\n};\n\nconst makeBoxDiv = (listener, type) => {\n const boxDiv = document.createElement(\"div\");\n boxDiv.className = `${type}-editor-box`;\n\n const editorDiv = makeEditorDiv(listener, type);\n const outDiv = makeOutDiv(type);\n boxDiv.append(editorDiv, outDiv);\n\n return [boxDiv, outDiv];\n};\n\nconst init = async (script, type, interpreter) => {\n const [\n { basicSetup, EditorView },\n { Compartment },\n { python },\n { indentUnit },\n { keymap },\n { defaultKeymap },\n ] = await Promise.all([\n // TODO: find a way to actually produce these bundles locally\n import(/* webpackIgnore: true */ \"../3rd-party/codemirror.js\"),\n import(/* webpackIgnore: true */ \"../3rd-party/codemirror_state.js\"),\n import(\n /* webpackIgnore: true */ \"../3rd-party/codemirror_lang-python.js\"\n ),\n import(/* webpackIgnore: true */ \"../3rd-party/codemirror_language.js\"),\n import(/* webpackIgnore: true */ \"../3rd-party/codemirror_view.js\"),\n import(/* webpackIgnore: true */ \"../3rd-party/codemirror_commands.js\"),\n ]);\n\n const selector = script.getAttribute(\"target\");\n\n let target;\n if (selector) {\n target =\n document.getElementById(selector) ||\n document.querySelector(selector);\n if (!target) throw new Error(`Unknown target ${selector}`);\n } else {\n target = document.createElement(`${type}-editor`);\n target.style.display = \"block\";\n script.after(target);\n }\n\n if (!target.id) target.id = getID(type);\n if (!target.hasAttribute(\"exec-id\")) target.setAttribute(\"exec-id\", 0);\n if (!target.hasAttribute(\"root\")) target.setAttribute(\"root\", target.id);\n\n const env = `${interpreter}-${script.getAttribute(\"env\") || getID(type)}`;\n const context = {\n interpreter,\n env,\n get pySrc() {\n return editor.state.doc.toString();\n },\n get outDiv() {\n return outDiv;\n },\n };\n\n // @see https://github.com/JeffersGlass/mkdocs-pyscript/blob/main/mkdocs_pyscript/js/makeblocks.js\n const listener = execute.bind(context);\n const [boxDiv, outDiv] = makeBoxDiv(listener, type);\n boxDiv.dataset.env = script.hasAttribute(\"env\") ? env : interpreter;\n\n const inputChild = boxDiv.querySelector(`.${type}-editor-input > div`);\n const parent = inputChild.attachShadow({ mode: \"open\" });\n // avoid inheriting styles from the outer component\n parent.innerHTML = `<style> :host { all: initial; }</style>`;\n\n target.appendChild(boxDiv);\n\n const doc = dedent(script.textContent).trim();\n\n // preserve user indentation, if any\n const indentation = /^(\\s+)/m.test(doc) ? RegExp.$1 : \" \";\n\n const editor = new EditorView({\n extensions: [\n indentUnit.of(indentation),\n new Compartment().of(python()),\n keymap.of([\n ...defaultKeymap,\n { key: \"Ctrl-Enter\", run: listener, preventDefault: true },\n { key: \"Cmd-Enter\", run: listener, preventDefault: true },\n { key: \"Shift-Enter\", run: listener, preventDefault: true },\n ]),\n basicSetup,\n ],\n parent,\n doc,\n });\n\n editor.focus();\n};\n\n// avoid too greedy MutationObserver operations at distance\nlet timeout = 0;\n\n// reset interval value then check for new scripts\nconst resetTimeout = () => {\n timeout = 0;\n pyEditor();\n};\n\n// triggered both ASAP on the living DOM and via MutationObserver later\nconst pyEditor = async () => {\n if (timeout) return;\n timeout = setTimeout(resetTimeout, 250);\n for (const [type, interpreter] of TYPES) {\n const selector = `script[type=\"${type}-editor\"]`;\n for (const script of document.querySelectorAll(selector)) {\n // avoid any further bootstrap\n script.type += \"-active\";\n await init(script, type, interpreter);\n }\n }\n};\n\nnew MutationObserver(pyEditor).observe(document, {\n childList: true,\n subtree: true,\n});\n\n// try to check the current document ASAP\nexport default pyEditor();\n"],"names":["id","getID","type","envs","Map","hooks","worker","onReady","runAsync","io","sync","stdout","line","write","stderr","writeErr","revoke","async","execute","currentTarget","env","pySrc","outDiv","this","disabled","innerHTML","has","srcLink","URL","createObjectURL","Blob","xworker","XWorker","call","Hook","interpreter","promise","resolve","Promise","withResolvers","set","revokeObjectURL","get","then","onerror","error","message","console","enable","str","innerText","makeEditorDiv","listener","editorDiv","document","createElement","className","setAttribute","runButton","addEventListener","makeRunButton","editorShadowContainer","event","stopPropagation","append","makeBoxDiv","boxDiv","makeOutDiv","init","script","basicSetup","EditorView","Compartment","python","indentUnit","keymap","defaultKeymap","all","import","n","x","q","selector","getAttribute","target","getElementById","querySelector","Error","style","display","after","hasAttribute","context","editor","state","doc","toString","bind","dataset","parent","attachShadow","mode","appendChild","dedent","textContent","trim","indentation","test","RegExp","$1","extensions","of","key","run","preventDefault","focus","timeout","resetTimeout","pyEditor","setTimeout","TYPES","querySelectorAll","MutationObserver","observe","childList","subtree","pyEditor$1"],"mappings":"4DAMA,IAAIA,EAAK,EACT,MAAMC,EAASC,GAAS,GAAGA,YAAeF,MAEpCG,EAAO,IAAIC,IAEXC,EAAQ,CACVC,OAAQ,CAEJC,QAAS,EAAGC,WAAUC,OAAQC,WAC1BD,EAAGE,OAAUC,GAASF,EAAKG,MAAMD,GACjCH,EAAGK,OAAUF,GAASF,EAAKK,SAASH,GACpCF,EAAKM,SACLN,EAAKF,SAAWA,CAAQ,IAKpCS,eAAeC,GAAQC,cAAEA,IACrB,MAAMC,IAAEA,EAAGC,MAAEA,EAAKC,OAAEA,GAAWC,KAK/B,GAHAJ,EAAcK,UAAW,EACzBF,EAAOG,UAAY,IAEdtB,EAAKuB,IAAIN,GAAM,CAChB,MAAMO,EAAUC,IAAIC,gBAAgB,IAAIC,KAAK,CAAC,MACxCC,EAAUC,EAAQC,KAAK,IAAIC,EAAK,KAAM7B,GAAQsB,EAAS,CACzDzB,KAAMqB,KAAKY,eAGTzB,KAAEA,GAASqB,GACXK,QAAEA,EAAOC,QAAEA,GAAYC,QAAQC,gBACrCpC,EAAKqC,IAAIpB,EAAKgB,GACd1B,EAAKM,OAAS,KACVY,IAAIa,gBAAgBd,GACpBU,EAAQN,EAAQ,CAEvB,CAID5B,EAAKuC,IAAItB,GAAKuB,MAAMZ,IAChBA,EAAQa,QAAU,EAAGC,YACjBvB,EAAOG,WAAa,2BAChBoB,EAAMC,SAAWD,aAErBE,QAAQF,MAAMA,EAAM,EAGxB,MAAMG,EAAS,KACX7B,EAAcK,UAAW,CAAK,GAE5Bd,KAAEA,GAASqB,EACjBrB,EAAKG,MAASoC,IACV3B,EAAO4B,WAAa,GAAGD,KAAO,EAElCvC,EAAKK,SAAYkC,IACb3B,EAAOG,WAAa,2BAA2BwB,YAAc,EAEjEvC,EAAKF,SAASa,GAAOsB,KAAKK,EAAQA,EAAO,GAEjD,CAEA,MASMG,EAAgB,CAACC,EAAUlD,KAC7B,MAAMmD,EAAYC,SAASC,cAAc,OACzCF,EAAUG,UAAY,GAAGtD,iBACzBmD,EAAUI,aAAa,aAAc,sBAErC,MAAMC,EAdY,EAACN,EAAUlD,KAC7B,MAAMwD,EAAYJ,SAASC,cAAc,UAKzC,OAJAG,EAAUF,UAAY,YAAYtD,sBAClCwD,EAAUjC,UAnEK,gmBAoEfiC,EAAUD,aAAa,aAAc,4BACrCC,EAAUC,iBAAiB,QAASP,GAC7BM,CAAS,EAQEE,CAAcR,EAAUlD,GACpC2D,EAAwBP,SAASC,cAAc,OASrD,OANAM,EAAsBF,iBAAiB,WAAYG,IAC/CA,EAAMC,iBAAiB,IAG3BV,EAAUW,OAAON,EAAWG,GAErBR,CAAS,EAUdY,EAAa,CAACb,EAAUlD,KAC1B,MAAMgE,EAASZ,SAASC,cAAc,OACtCW,EAAOV,UAAY,GAAGtD,eAEtB,MAAMmD,EAAYF,EAAcC,EAAUlD,GACpCoB,EAZS,CAACpB,IAChB,MAAMoB,EAASgC,SAASC,cAAc,OAGtC,OAFAjC,EAAOkC,UAAY,GAAGtD,kBACtBoB,EAAOtB,GAAK,GAAGC,EAAMC,YACdoB,CAAM,EAQE6C,CAAWjE,GAG1B,OAFAgE,EAAOF,OAAOX,EAAW/B,GAElB,CAAC4C,EAAQ5C,EAAO,EAGrB8C,EAAOnD,MAAOoD,EAAQnE,EAAMiC,KAC9B,OACImC,WAAEA,EAAUC,WAAEA,IACdC,YAAEA,IACFC,OAAEA,IACFC,WAAEA,IACFC,OAAEA,IACFC,cAAEA,UACItC,QAAQuC,IAAI,CAElBC,OAAiC,4BACjCA,OAAiC,kCACjCA,OAC8B,wCAE9BA,OAAiC,qCAAsCnC,MAAA,SAAAoC,GAAA,OAAAA,EAAAC,CAAA,IACvEF,OAAiC,iCAAkCnC,MAAA,SAAAoC,GAAA,OAAAA,EAAAE,CAAA,IACnEH,OAAiC,uCAG/BI,EAAWb,EAAOc,aAAa,UAErC,IAAIC,EACJ,GAAIF,GAIA,GAHAE,EACI9B,SAAS+B,eAAeH,IACxB5B,SAASgC,cAAcJ,IACtBE,EAAQ,MAAM,IAAIG,MAAM,kBAAkBL,UAE/CE,EAAS9B,SAASC,cAAc,GAAGrD,YACnCkF,EAAOI,MAAMC,QAAU,QACvBpB,EAAOqB,MAAMN,GAGZA,EAAOpF,KAAIoF,EAAOpF,GAAKC,EAAMC,IAC7BkF,EAAOO,aAAa,YAAYP,EAAO3B,aAAa,UAAW,GAC/D2B,EAAOO,aAAa,SAASP,EAAO3B,aAAa,OAAQ2B,EAAOpF,IAErE,MAAMoB,EAAM,GAAGe,KAAekC,EAAOc,aAAa,QAAUlF,EAAMC,KAC5D0F,EAAU,CACZzD,cACAf,MACA,SAAIC,GACA,OAAOwE,EAAOC,MAAMC,IAAIC,UAC3B,EACD,UAAI1E,GACA,OAAOA,CACV,GAIC8B,EAAWlC,EAAQ+E,KAAKL,IACvB1B,EAAQ5C,GAAU2C,EAAWb,EAAUlD,GAC9CgE,EAAOgC,QAAQ9E,IAAMiD,EAAOsB,aAAa,OAASvE,EAAMe,EAExD,MACMgE,EADajC,EAAOoB,cAAc,IAAIpF,wBAClBkG,aAAa,CAAEC,KAAM,SAE/CF,EAAO1E,UAAY,0CAEnB2D,EAAOkB,YAAYpC,GAEnB,MAAM6B,EAAMQ,EAAOlC,EAAOmC,aAAaC,OAGjCC,EAAc,UAAUC,KAAKZ,GAAOa,OAAOC,GAAK,OAEhDhB,EAAS,IAAItB,EAAW,CAC1BuC,WAAY,CACRpC,EAAWqC,GAAGL,IACd,IAAIlC,GAAcuC,GAAGtC,KACrBE,EAAOoC,GAAG,IACHnC,EACH,CAAEoC,IAAK,aAAcC,IAAK7D,EAAU8D,gBAAgB,GACpD,CAAEF,IAAK,YAAaC,IAAK7D,EAAU8D,gBAAgB,GACnD,CAAEF,IAAK,cAAeC,IAAK7D,EAAU8D,gBAAgB,KAEzD5C,GAEJ6B,SACAJ,QAGJF,EAAOsB,OAAO,EAIlB,IAAIC,EAAU,EAGd,MAAMC,EAAe,KACjBD,EAAU,EACVE,GAAU,EAIRA,EAAWrG,UACb,IAAImG,EAAJ,CACAA,EAAUG,WAAWF,EAAc,KACnC,IAAK,MAAOnH,EAAMiC,KAAgBqF,EAAO,CACrC,MAAMtC,EAAW,gBAAgBhF,aACjC,IAAK,MAAMmE,KAAUf,SAASmE,iBAAiBvC,GAE3Cb,EAAOnE,MAAQ,gBACTkE,EAAKC,EAAQnE,EAAMiC,EAEhC,CATmB,CASnB,EAGL,IAAIuF,iBAAiBJ,GAAUK,QAAQrE,SAAU,CAC7CsE,WAAW,EACXC,SAAS,IAIb,IAAAC,EAAeR"}
|
1
|
+
{"version":3,"file":"py-editor-AC_7LQst.js","sources":["../src/plugins/py-editor.js"],"sourcesContent":["// PyScript py-editor plugin\nimport { Hook, XWorker, dedent } from \"polyscript/exports\";\nimport { TYPES } from \"../core.js\";\n\nconst RUN_BUTTON = `<svg style=\"height:20px;width:20px;vertical-align:-.125em;transform-origin:center;overflow:visible;color:green\" viewBox=\"0 0 384 512\" aria-hidden=\"true\" role=\"img\" xmlns=\"http://www.w3.org/2000/svg\"><g transform=\"translate(192 256)\" transform-origin=\"96 0\"><g transform=\"translate(0,0) scale(1,1)\"><path d=\"M361 215C375.3 223.8 384 239.3 384 256C384 272.7 375.3 288.2 361 296.1L73.03 472.1C58.21 482 39.66 482.4 24.52 473.9C9.377 465.4 0 449.4 0 432V80C0 62.64 9.377 46.63 24.52 38.13C39.66 29.64 58.21 29.99 73.03 39.04L361 215z\" fill=\"currentColor\" transform=\"translate(-192 -256)\"></path></g></g></svg>`;\n\nlet id = 0;\nconst getID = (type) => `${type}-editor-${id++}`;\n\nconst envs = new Map();\n\nconst hooks = {\n worker: {\n // works on both Pyodide and MicroPython\n onReady: ({ runAsync, io }, { sync }) => {\n io.stdout = (line) => sync.write(line);\n io.stderr = (line) => sync.writeErr(line);\n sync.revoke();\n sync.runAsync = runAsync;\n },\n },\n};\n\nasync function execute({ currentTarget }) {\n const { env, pySrc, outDiv } = this;\n\n currentTarget.disabled = true;\n outDiv.innerHTML = \"\";\n\n if (!envs.has(env)) {\n const srcLink = URL.createObjectURL(new Blob([\"\"]));\n const xworker = XWorker.call(new Hook(null, hooks), srcLink, {\n type: this.interpreter,\n });\n\n const { sync } = xworker;\n const { promise, resolve } = Promise.withResolvers();\n envs.set(env, promise);\n sync.revoke = () => {\n URL.revokeObjectURL(srcLink);\n resolve(xworker);\n };\n }\n\n // wait for the env then set the target div\n // before executing the current code\n envs.get(env).then((xworker) => {\n xworker.onerror = ({ error }) => {\n outDiv.innerHTML += `<span style='color:red'>${\n error.message || error\n }</span>\\n`;\n console.error(error);\n };\n\n const enable = () => {\n currentTarget.disabled = false;\n };\n const { sync } = xworker;\n sync.write = (str) => {\n outDiv.innerText += `${str}\\n`;\n };\n sync.writeErr = (str) => {\n outDiv.innerHTML += `<span style='color:red'>${str}</span>\\n`;\n };\n sync.runAsync(pySrc).then(enable, enable);\n });\n}\n\nconst makeRunButton = (listener, type) => {\n const runButton = document.createElement(\"button\");\n runButton.className = `absolute ${type}-editor-run-button`;\n runButton.innerHTML = RUN_BUTTON;\n runButton.setAttribute(\"aria-label\", \"Python Script Run Button\");\n runButton.addEventListener(\"click\", listener);\n return runButton;\n};\n\nconst makeEditorDiv = (listener, type) => {\n const editorDiv = document.createElement(\"div\");\n editorDiv.className = `${type}-editor-input`;\n editorDiv.setAttribute(\"aria-label\", \"Python Script Area\");\n\n const runButton = makeRunButton(listener, type);\n const editorShadowContainer = document.createElement(\"div\");\n\n // avoid outer elements intercepting key events (reveal as example)\n editorShadowContainer.addEventListener(\"keydown\", (event) => {\n event.stopPropagation();\n });\n\n editorDiv.append(runButton, editorShadowContainer);\n\n return editorDiv;\n};\n\nconst makeOutDiv = (type) => {\n const outDiv = document.createElement(\"div\");\n outDiv.className = `${type}-editor-output`;\n outDiv.id = `${getID(type)}-output`;\n return outDiv;\n};\n\nconst makeBoxDiv = (listener, type) => {\n const boxDiv = document.createElement(\"div\");\n boxDiv.className = `${type}-editor-box`;\n\n const editorDiv = makeEditorDiv(listener, type);\n const outDiv = makeOutDiv(type);\n boxDiv.append(editorDiv, outDiv);\n\n return [boxDiv, outDiv];\n};\n\nconst init = async (script, type, interpreter) => {\n const [\n { basicSetup, EditorView },\n { Compartment },\n { python },\n { indentUnit },\n { keymap },\n { defaultKeymap },\n ] = await Promise.all([\n // TODO: find a way to actually produce these bundles locally\n import(/* webpackIgnore: true */ \"../3rd-party/codemirror.js\"),\n import(/* webpackIgnore: true */ \"../3rd-party/codemirror_state.js\"),\n import(\n /* webpackIgnore: true */ \"../3rd-party/codemirror_lang-python.js\"\n ),\n import(/* webpackIgnore: true */ \"../3rd-party/codemirror_language.js\"),\n import(/* webpackIgnore: true */ \"../3rd-party/codemirror_view.js\"),\n import(/* webpackIgnore: true */ \"../3rd-party/codemirror_commands.js\"),\n ]);\n\n const selector = script.getAttribute(\"target\");\n\n let target;\n if (selector) {\n target =\n document.getElementById(selector) ||\n document.querySelector(selector);\n if (!target) throw new Error(`Unknown target ${selector}`);\n } else {\n target = document.createElement(`${type}-editor`);\n target.style.display = \"block\";\n script.after(target);\n }\n\n if (!target.id) target.id = getID(type);\n if (!target.hasAttribute(\"exec-id\")) target.setAttribute(\"exec-id\", 0);\n if (!target.hasAttribute(\"root\")) target.setAttribute(\"root\", target.id);\n\n const env = `${interpreter}-${script.getAttribute(\"env\") || getID(type)}`;\n const context = {\n interpreter,\n env,\n get pySrc() {\n return editor.state.doc.toString();\n },\n get outDiv() {\n return outDiv;\n },\n };\n\n // @see https://github.com/JeffersGlass/mkdocs-pyscript/blob/main/mkdocs_pyscript/js/makeblocks.js\n const listener = execute.bind(context);\n const [boxDiv, outDiv] = makeBoxDiv(listener, type);\n boxDiv.dataset.env = script.hasAttribute(\"env\") ? env : interpreter;\n\n const inputChild = boxDiv.querySelector(`.${type}-editor-input > div`);\n const parent = inputChild.attachShadow({ mode: \"open\" });\n // avoid inheriting styles from the outer component\n parent.innerHTML = `<style> :host { all: initial; }</style>`;\n\n target.appendChild(boxDiv);\n\n const doc = dedent(script.textContent).trim();\n\n // preserve user indentation, if any\n const indentation = /^(\\s+)/m.test(doc) ? RegExp.$1 : \" \";\n\n const editor = new EditorView({\n extensions: [\n indentUnit.of(indentation),\n new Compartment().of(python()),\n keymap.of([\n ...defaultKeymap,\n { key: \"Ctrl-Enter\", run: listener, preventDefault: true },\n { key: \"Cmd-Enter\", run: listener, preventDefault: true },\n { key: \"Shift-Enter\", run: listener, preventDefault: true },\n ]),\n basicSetup,\n ],\n parent,\n doc,\n });\n\n editor.focus();\n};\n\n// avoid too greedy MutationObserver operations at distance\nlet timeout = 0;\n\n// reset interval value then check for new scripts\nconst resetTimeout = () => {\n timeout = 0;\n pyEditor();\n};\n\n// triggered both ASAP on the living DOM and via MutationObserver later\nconst pyEditor = async () => {\n if (timeout) return;\n timeout = setTimeout(resetTimeout, 250);\n for (const [type, interpreter] of TYPES) {\n const selector = `script[type=\"${type}-editor\"]`;\n for (const script of document.querySelectorAll(selector)) {\n // avoid any further bootstrap\n script.type += \"-active\";\n await init(script, type, interpreter);\n }\n }\n};\n\nnew MutationObserver(pyEditor).observe(document, {\n childList: true,\n subtree: true,\n});\n\n// try to check the current document ASAP\nexport default pyEditor();\n"],"names":["id","getID","type","envs","Map","hooks","worker","onReady","runAsync","io","sync","stdout","line","write","stderr","writeErr","revoke","async","execute","currentTarget","env","pySrc","outDiv","this","disabled","innerHTML","has","srcLink","URL","createObjectURL","Blob","xworker","XWorker","call","Hook","interpreter","promise","resolve","Promise","withResolvers","set","revokeObjectURL","get","then","onerror","error","message","console","enable","str","innerText","makeEditorDiv","listener","editorDiv","document","createElement","className","setAttribute","runButton","addEventListener","makeRunButton","editorShadowContainer","event","stopPropagation","append","makeBoxDiv","boxDiv","makeOutDiv","init","script","basicSetup","EditorView","Compartment","python","indentUnit","keymap","defaultKeymap","all","import","n","x","q","selector","getAttribute","target","getElementById","querySelector","Error","style","display","after","hasAttribute","context","editor","state","doc","toString","bind","dataset","parent","attachShadow","mode","appendChild","dedent","textContent","trim","indentation","test","RegExp","$1","extensions","of","key","run","preventDefault","focus","timeout","resetTimeout","pyEditor","setTimeout","TYPES","querySelectorAll","MutationObserver","observe","childList","subtree","pyEditor$1"],"mappings":"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"}
|
@@ -1,2 +1,2 @@
|
|
1
|
-
import{T as e,e as t}from"./core-
|
2
|
-
//# sourceMappingURL=py-terminal-
|
1
|
+
import{T as e,e as t}from"./core-30w338Df.js";import{notify as r}from"./error-KetuWjDJ.js";const n=[...e.keys()].map((e=>`script[type="${e}"][terminal],${e}-script[terminal]`)).join(","),o=e=>{throw r(e),new Error(e)},i=async()=>{const e=document.querySelectorAll(n);if(!e.length)return;s.disconnect(),e.length>1&&o("You can use at most 1 terminal.");const[r]=e;r.matches('script[type="mpy"],mpy-script')&&o("Unsupported terminal."),document.head.append(Object.assign(document.createElement("link"),{rel:"stylesheet",href:new URL("./xterm.css",import.meta.url)}));const[{Terminal:i},{Readline:a},{FitAddon:d}]=await Promise.all([import("./xterm-f2QfYNGL.js"),import("./xterm-readline-ONk85xtH.js"),import("./xterm_addon-fit-E4yMPZTX.js")]),l=new a,c=e=>{let t=r;const n=r.getAttribute("target");if(n){if(t=document.getElementById(n)||document.querySelector(n),!t)throw new Error(`Unknown target ${n}`)}else t=document.createElement("py-terminal"),t.style.display="block",r.after(t);const o=new i({theme:{background:"#191A19",foreground:"#F5F2E7"},...e}),s=new d;o.loadAddon(s),o.loadAddon(l),o.open(t),s.fit(),o.focus()};if(r.hasAttribute("worker")){const e=({interpreter:e},{sync:t})=>{t.pyterminal_drop_hooks();const r=new TextDecoder;let n="";const o={isatty:!0,write:e=>(n=r.decode(e),t.pyterminal_write(n),e.length)};e.setStdout(o),e.setStderr(o),e.setStdin({isatty:!0,stdin:()=>t.pyterminal_read(n)})};t.main.onWorker.add((function r(n,o){t.main.onWorker.delete(r),c({disableStdin:!1,cursorBlink:!0,cursorStyle:"block"}),o.sync.pyterminal_read=l.read.bind(l),o.sync.pyterminal_write=l.write.bind(l),o.sync.pyterminal_drop_hooks=()=>{t.worker.onReady.delete(e)}})),t.worker.onReady.add(e)}else t.main.onReady.add((function e({io:r}){console.warn("py-terminal is read only on main thread"),t.main.onReady.delete(e),c({disableStdin:!0,cursorBlink:!1,cursorStyle:"underline"}),r.stdout=e=>{l.write(`${e}\n`)},r.stderr=e=>{l.write(`${e.message||e}\n`)}}))},s=new MutationObserver(i);s.observe(document,{childList:!0,subtree:!0});var a=i();export{a as default};
|
2
|
+
//# sourceMappingURL=py-terminal-iIRy_gMx.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"py-terminal-
|
1
|
+
{"version":3,"file":"py-terminal-iIRy_gMx.js","sources":["../src/plugins/py-terminal.js"],"sourcesContent":["// PyScript py-terminal plugin\nimport { TYPES, hooks } from \"../core.js\";\nimport { notify } from \"./error.js\";\n\nconst SELECTOR = [...TYPES.keys()]\n .map((type) => `script[type=\"${type}\"][terminal],${type}-script[terminal]`)\n .join(\",\");\n\n// show the error on main and\n// stops the module from keep executing\nconst notifyAndThrow = (message) => {\n notify(message);\n throw new Error(message);\n};\n\nconst pyTerminal = async () => {\n const terminals = document.querySelectorAll(SELECTOR);\n\n // no results will look further for runtime nodes\n if (!terminals.length) return;\n\n // if we arrived this far, let's drop the MutationObserver\n // as we only support one terminal per page (right now).\n mo.disconnect();\n\n // we currently support only one terminal as in \"classic\"\n if (terminals.length > 1) notifyAndThrow(\"You can use at most 1 terminal.\");\n\n const [element] = terminals;\n // hopefully to be removed in the near future!\n if (element.matches('script[type=\"mpy\"],mpy-script'))\n notifyAndThrow(\"Unsupported terminal.\");\n\n // import styles lazily\n document.head.append(\n Object.assign(document.createElement(\"link\"), {\n rel: \"stylesheet\",\n href: new URL(\"./xterm.css\", import.meta.url),\n }),\n );\n\n // lazy load these only when a valid terminal is found\n const [{ Terminal }, { Readline }, { FitAddon }] = await Promise.all([\n import(/* webpackIgnore: true */ \"../3rd-party/xterm.js\"),\n import(/* webpackIgnore: true */ \"../3rd-party/xterm-readline.js\"),\n import(/* webpackIgnore: true */ \"../3rd-party/xterm_addon-fit.js\"),\n ]);\n\n const readline = new Readline();\n\n // common main thread initialization for both worker\n // or main case, bootstrapping the terminal on its target\n const init = (options) => {\n let target = element;\n const selector = element.getAttribute(\"target\");\n if (selector) {\n target =\n document.getElementById(selector) ||\n document.querySelector(selector);\n if (!target) throw new Error(`Unknown target ${selector}`);\n } else {\n target = document.createElement(\"py-terminal\");\n target.style.display = \"block\";\n element.after(target);\n }\n const terminal = new Terminal({\n theme: {\n background: \"#191A19\",\n foreground: \"#F5F2E7\",\n },\n ...options,\n });\n const fitAddon = new FitAddon();\n terminal.loadAddon(fitAddon);\n terminal.loadAddon(readline);\n terminal.open(target);\n fitAddon.fit();\n terminal.focus();\n };\n\n // branch logic for the worker\n if (element.hasAttribute(\"worker\")) {\n // when the remote thread onReady triggers:\n // setup the interpreter stdout and stderr\n const workerReady = ({ interpreter }, { sync }) => {\n sync.pyterminal_drop_hooks();\n const decoder = new TextDecoder();\n let data = \"\";\n const generic = {\n isatty: true,\n write(buffer) {\n data = decoder.decode(buffer);\n sync.pyterminal_write(data);\n return buffer.length;\n },\n };\n interpreter.setStdout(generic);\n interpreter.setStderr(generic);\n interpreter.setStdin({\n isatty: true,\n stdin: () => sync.pyterminal_read(data),\n });\n };\n\n // add a hook on the main thread to setup all sync helpers\n // also bootstrapping the XTerm target on main\n hooks.main.onWorker.add(function worker(_, xworker) {\n hooks.main.onWorker.delete(worker);\n init({\n disableStdin: false,\n cursorBlink: true,\n cursorStyle: \"block\",\n });\n xworker.sync.pyterminal_read = readline.read.bind(readline);\n xworker.sync.pyterminal_write = readline.write.bind(readline);\n // allow a worker to drop main thread hooks ASAP\n xworker.sync.pyterminal_drop_hooks = () => {\n hooks.worker.onReady.delete(workerReady);\n };\n });\n\n // setup remote thread JS/Python code for whenever the\n // worker is ready to become a terminal\n hooks.worker.onReady.add(workerReady);\n } else {\n // in the main case, just bootstrap XTerm without\n // allowing any input as that's not possible / awkward\n hooks.main.onReady.add(function main({ io }) {\n console.warn(\"py-terminal is read only on main thread\");\n hooks.main.onReady.delete(main);\n init({\n disableStdin: true,\n cursorBlink: false,\n cursorStyle: \"underline\",\n });\n io.stdout = (value) => {\n readline.write(`${value}\\n`);\n };\n io.stderr = (error) => {\n readline.write(`${error.message || error}\\n`);\n };\n });\n }\n};\n\nconst mo = new MutationObserver(pyTerminal);\nmo.observe(document, { childList: true, subtree: true });\n\n// try to check the current document ASAP\nexport default pyTerminal();\n"],"names":["SELECTOR","TYPES","keys","map","type","join","notifyAndThrow","message","notify","Error","pyTerminal","async","terminals","document","querySelectorAll","length","mo","disconnect","element","matches","head","append","Object","assign","createElement","rel","href","URL","url","Terminal","Readline","FitAddon","Promise","all","import","readline","init","options","target","selector","getAttribute","getElementById","querySelector","style","display","after","terminal","theme","background","foreground","fitAddon","loadAddon","open","fit","focus","hasAttribute","workerReady","interpreter","sync","pyterminal_drop_hooks","decoder","TextDecoder","data","generic","isatty","write","buffer","decode","pyterminal_write","setStdout","setStderr","setStdin","stdin","pyterminal_read","hooks","main","onWorker","add","worker","_","xworker","delete","disableStdin","cursorBlink","cursorStyle","read","bind","onReady","io","console","warn","stdout","value","stderr","error","MutationObserver","observe","childList","subtree","pyTerminal$1"],"mappings":"2FAIA,MAAMA,EAAW,IAAIC,EAAMC,QACtBC,KAAKC,GAAS,gBAAgBA,iBAAoBA,uBAClDC,KAAK,KAIJC,EAAkBC,IAEpB,MADAC,EAAOD,GACD,IAAIE,MAAMF,EAAQ,EAGtBG,EAAaC,UACf,MAAMC,EAAYC,SAASC,iBAAiBd,GAG5C,IAAKY,EAAUG,OAAQ,OAIvBC,EAAGC,aAGCL,EAAUG,OAAS,GAAGT,EAAe,mCAEzC,MAAOY,GAAWN,EAEdM,EAAQC,QAAQ,kCAChBb,EAAe,yBAGnBO,SAASO,KAAKC,OACVC,OAAOC,OAAOV,SAASW,cAAc,QAAS,CAC1CC,IAAK,aACLC,KAAM,IAAIC,IAAI,0BAA2BC,QAKjD,OAAOC,SAAEA,IAAYC,SAAEA,IAAYC,SAAEA,UAAoBC,QAAQC,IAAI,CACjEC,OAAiC,uBACjCA,OAAiC,gCACjCA,OAAiC,mCAG/BC,EAAW,IAAIL,EAIfM,EAAQC,IACV,IAAIC,EAASpB,EACb,MAAMqB,EAAWrB,EAAQsB,aAAa,UACtC,GAAID,GAIA,GAHAD,EACIzB,SAAS4B,eAAeF,IACxB1B,SAAS6B,cAAcH,IACtBD,EAAQ,MAAM,IAAI7B,MAAM,kBAAkB8B,UAE/CD,EAASzB,SAASW,cAAc,eAChCc,EAAOK,MAAMC,QAAU,QACvB1B,EAAQ2B,MAAMP,GAElB,MAAMQ,EAAW,IAAIjB,EAAS,CAC1BkB,MAAO,CACHC,WAAY,UACZC,WAAY,cAEbZ,IAEDa,EAAW,IAAInB,EACrBe,EAASK,UAAUD,GACnBJ,EAASK,UAAUhB,GACnBW,EAASM,KAAKd,GACdY,EAASG,MACTP,EAASQ,OAAO,EAIpB,GAAIpC,EAAQqC,aAAa,UAAW,CAGhC,MAAMC,EAAc,EAAGC,gBAAiBC,WACpCA,EAAKC,wBACL,MAAMC,EAAU,IAAIC,YACpB,IAAIC,EAAO,GACX,MAAMC,EAAU,CACZC,QAAQ,EACRC,MAAMC,IACFJ,EAAOF,EAAQO,OAAOD,GACtBR,EAAKU,iBAAiBN,GACfI,EAAOnD,SAGtB0C,EAAYY,UAAUN,GACtBN,EAAYa,UAAUP,GACtBN,EAAYc,SAAS,CACjBP,QAAQ,EACRQ,MAAO,IAAMd,EAAKe,gBAAgBX,IACpC,EAKNY,EAAMC,KAAKC,SAASC,KAAI,SAASC,EAAOC,EAAGC,GACvCN,EAAMC,KAAKC,SAASK,OAAOH,GAC3B1C,EAAK,CACD8C,cAAc,EACdC,aAAa,EACbC,YAAa,UAEjBJ,EAAQtB,KAAKe,gBAAkBtC,EAASkD,KAAKC,KAAKnD,GAClD6C,EAAQtB,KAAKU,iBAAmBjC,EAAS8B,MAAMqB,KAAKnD,GAEpD6C,EAAQtB,KAAKC,sBAAwB,KACjCe,EAAMI,OAAOS,QAAQN,OAAOzB,EAAY,CAExD,IAIQkB,EAAMI,OAAOS,QAAQV,IAAIrB,EACjC,MAGQkB,EAAMC,KAAKY,QAAQV,KAAI,SAASF,GAAKa,GAAEA,IACnCC,QAAQC,KAAK,2CACbhB,EAAMC,KAAKY,QAAQN,OAAON,GAC1BvC,EAAK,CACD8C,cAAc,EACdC,aAAa,EACbC,YAAa,cAEjBI,EAAGG,OAAUC,IACTzD,EAAS8B,MAAM,GAAG2B,MAAU,EAEhCJ,EAAGK,OAAUC,IACT3D,EAAS8B,MAAM,GAAG6B,EAAMvF,SAAWuF,MAAU,CAE7D,GACK,EAGC9E,EAAK,IAAI+E,iBAAiBrF,GAChCM,EAAGgF,QAAQnF,SAAU,CAAEoF,WAAW,EAAMC,SAAS,IAGjD,IAAAC,EAAezF"}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@pyscript/core",
|
3
|
-
"version": "0.3.
|
3
|
+
"version": "0.3.13",
|
4
4
|
"type": "module",
|
5
5
|
"description": "PyScript",
|
6
6
|
"module": "./index.js",
|
@@ -42,7 +42,7 @@
|
|
42
42
|
"dependencies": {
|
43
43
|
"@ungap/with-resolvers": "^0.1.0",
|
44
44
|
"basic-devtools": "^0.1.6",
|
45
|
-
"polyscript": "^0.6.
|
45
|
+
"polyscript": "^0.6.8",
|
46
46
|
"sticky-module": "^0.1.1",
|
47
47
|
"to-json-callback": "^0.1.1",
|
48
48
|
"type-checked-collections": "^0.1.7"
|
@@ -5,7 +5,21 @@ from pyscript.util import NotSupported
|
|
5
5
|
|
6
6
|
RUNNING_IN_WORKER = not hasattr(globalThis, "document")
|
7
7
|
|
8
|
-
|
8
|
+
|
9
|
+
# allow `from pyscript.js_modules.xxx import yyy`
|
10
|
+
class JSModule(object):
|
11
|
+
def __init__(self, name):
|
12
|
+
self.name = name
|
13
|
+
|
14
|
+
def __getattr__(self, field):
|
15
|
+
# avoid pyodide looking for non existent fields
|
16
|
+
if not field.startswith("_"):
|
17
|
+
return getattr(getattr(js_modules, self.name), field)
|
18
|
+
|
19
|
+
|
20
|
+
# generate N modules in the system that will proxy the real value
|
21
|
+
for name in globalThis.Reflect.ownKeys(js_modules):
|
22
|
+
sys.modules[f"pyscript.js_modules.{name}"] = JSModule(name)
|
9
23
|
sys.modules["pyscript.js_modules"] = js_modules
|
10
24
|
|
11
25
|
if RUNNING_IN_WORKER:
|
package/src/stdlib/pyscript.js
CHANGED
@@ -4,7 +4,7 @@ export default {
|
|
4
4
|
"__init__.py": "# Some notes about the naming conventions and the relationship between various\n# similar-but-different names.\n#\n# import pyscript\n# this package contains the main user-facing API offered by pyscript. All\n# the names which are supposed be used by end users should be made\n# available in pyscript/__init__.py (i.e., this file)\n#\n# import _pyscript\n# this is an internal module implemented in JS. It is used internally by\n# the pyscript package, end users should not use it directly. For its\n# implementation, grep for `interpreter.registerJsModule(\"_pyscript\",\n# ...)` in core.js\n#\n# import js\n# this is the JS globalThis, as exported by pyodide and/or micropython's\n# FFIs. As such, it contains different things in the main thread or in a\n# worker.\n#\n# import pyscript.magic_js\n# this submodule abstracts away some of the differences between the main\n# thread and the worker. In particular, it defines `window` and `document`\n# in such a way that these names work in both cases: in the main thread,\n# they are the \"real\" objects, in the worker they are proxies which work\n# thanks to coincident.\n#\n# from pyscript import window, document\n# these are just the window and document objects as defined by\n# pyscript.magic_js. This is the blessed way to access them from pyscript,\n# as it works transparently in both the main thread and worker cases.\n\nfrom pyscript.display import HTML, display\nfrom pyscript.magic_js import (\n RUNNING_IN_WORKER,\n PyWorker,\n current_target,\n document,\n js_modules,\n sync,\n window,\n)\n\ntry:\n from pyscript.event_handling import when\nexcept:\n from pyscript.util import NotSupported\n\n when = NotSupported(\n \"pyscript.when\", \"pyscript.when currently not available with this interpreter\"\n )\n",
|
5
5
|
"display.py": "import base64\nimport html\nimport io\nimport re\n\nfrom pyscript.magic_js import current_target, document, window\n\n_MIME_METHODS = {\n \"__repr__\": \"text/plain\",\n \"_repr_html_\": \"text/html\",\n \"_repr_markdown_\": \"text/markdown\",\n \"_repr_svg_\": \"image/svg+xml\",\n \"_repr_pdf_\": \"application/pdf\",\n \"_repr_jpeg_\": \"image/jpeg\",\n \"_repr_png_\": \"image/png\",\n \"_repr_latex\": \"text/latex\",\n \"_repr_json_\": \"application/json\",\n \"_repr_javascript_\": \"application/javascript\",\n \"savefig\": \"image/png\",\n}\n\n\ndef _render_image(mime, value, meta):\n # If the image value is using bytes we should convert it to base64\n # otherwise it will return raw bytes and the browser will not be able to\n # render it.\n if isinstance(value, bytes):\n value = base64.b64encode(value).decode(\"utf-8\")\n\n # This is the pattern of base64 strings\n base64_pattern = re.compile(\n r\"^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$\"\n )\n # If value doesn't match the base64 pattern we should encode it to base64\n if len(value) > 0 and not base64_pattern.match(value):\n value = base64.b64encode(value.encode(\"utf-8\")).decode(\"utf-8\")\n\n data = f\"data:{mime};charset=utf-8;base64,{value}\"\n attrs = \" \".join(['{k}=\"{v}\"' for k, v in meta.items()])\n return f'<img src=\"{data}\" {attrs}></img>'\n\n\ndef _identity(value, meta):\n return value\n\n\n_MIME_RENDERERS = {\n \"text/plain\": html.escape,\n \"text/html\": _identity,\n \"image/png\": lambda value, meta: _render_image(\"image/png\", value, meta),\n \"image/jpeg\": lambda value, meta: _render_image(\"image/jpeg\", value, meta),\n \"image/svg+xml\": _identity,\n \"application/json\": _identity,\n \"application/javascript\": lambda value, meta: f\"<script>{value}<\\\\/script>\",\n}\n\n\nclass HTML:\n \"\"\"\n Wrap a string so that display() can render it as plain HTML\n \"\"\"\n\n def __init__(self, html):\n self._html = html\n\n def _repr_html_(self):\n return self._html\n\n\ndef _eval_formatter(obj, print_method):\n \"\"\"\n Evaluates a formatter method.\n \"\"\"\n if print_method == \"__repr__\":\n return repr(obj)\n elif hasattr(obj, print_method):\n if print_method == \"savefig\":\n buf = io.BytesIO()\n obj.savefig(buf, format=\"png\")\n buf.seek(0)\n return base64.b64encode(buf.read()).decode(\"utf-8\")\n return getattr(obj, print_method)()\n elif print_method == \"_repr_mimebundle_\":\n return {}, {}\n return None\n\n\ndef _format_mime(obj):\n \"\"\"\n Formats object using _repr_x_ methods.\n \"\"\"\n if isinstance(obj, str):\n return html.escape(obj), \"text/plain\"\n\n mimebundle = _eval_formatter(obj, \"_repr_mimebundle_\")\n if isinstance(mimebundle, tuple):\n format_dict, _ = mimebundle\n else:\n format_dict = mimebundle\n\n output, not_available = None, []\n for method, mime_type in reversed(_MIME_METHODS.items()):\n if mime_type in format_dict:\n output = format_dict[mime_type]\n else:\n output = _eval_formatter(obj, method)\n\n if output is None:\n continue\n elif mime_type not in _MIME_RENDERERS:\n not_available.append(mime_type)\n continue\n break\n if output is None:\n if not_available:\n window.console.warn(\n f\"Rendered object requested unavailable MIME renderers: {not_available}\"\n )\n output = repr(output)\n mime_type = \"text/plain\"\n elif isinstance(output, tuple):\n output, meta = output\n else:\n meta = {}\n return _MIME_RENDERERS[mime_type](output, meta), mime_type\n\n\ndef _write(element, value, append=False):\n html, mime_type = _format_mime(value)\n if html == \"\\\\n\":\n return\n\n if append:\n out_element = document.createElement(\"div\")\n element.append(out_element)\n else:\n out_element = element.lastElementChild\n if out_element is None:\n out_element = element\n\n if mime_type in (\"application/javascript\", \"text/html\"):\n script_element = document.createRange().createContextualFragment(html)\n out_element.append(script_element)\n else:\n out_element.innerHTML = html\n\n\ndef display(*values, target=None, append=True):\n if target is None:\n target = current_target()\n elif not isinstance(target, str):\n raise TypeError(f\"target must be str or None, not {target.__class__.__name__}\")\n elif target == \"\":\n raise ValueError(\"Cannot have an empty target\")\n elif target.startswith(\"#\"):\n # note: here target is str and not None!\n # align with @when behavior\n target = target[1:]\n\n element = document.getElementById(target)\n\n # If target cannot be found on the page, a ValueError is raised\n if element is None:\n raise ValueError(\n f\"Invalid selector with id={target}. Cannot be found in the page.\"\n )\n\n # if element is a <script type=\"py\">, it has a 'target' attribute which\n # points to the visual element holding the displayed values. In that case,\n # use that.\n if element.tagName == \"SCRIPT\" and hasattr(element, \"target\"):\n element = element.target\n\n for v in values:\n if not append:\n element.replaceChildren()\n _write(element, v, append=append)\n",
|
6
6
|
"event_handling.py": "import inspect\n\nfrom pyodide.ffi.wrappers import add_event_listener\nfrom pyscript.magic_js import document\n\n\ndef when(event_type=None, selector=None):\n \"\"\"\n Decorates a function and passes py-* events to the decorated function\n The events might or not be an argument of the decorated function\n \"\"\"\n\n def decorator(func):\n if isinstance(selector, str):\n elements = document.querySelectorAll(selector)\n else:\n # TODO: This is a hack that will be removed when pyscript becomes a package\n # and we can better manage the imports without circular dependencies\n from pyweb import pydom\n\n if isinstance(selector, pydom.Element):\n elements = [selector._js]\n elif isinstance(selector, pydom.ElementCollection):\n elements = [el._js for el in selector]\n else:\n raise ValueError(\n f\"Invalid selector: {selector}. Selector must\"\n \" be a string, a pydom.Element or a pydom.ElementCollection.\"\n )\n\n sig = inspect.signature(func)\n # Function doesn't receive events\n if not sig.parameters:\n\n def wrapper(*args, **kwargs):\n func()\n\n for el in elements:\n add_event_listener(el, event_type, wrapper)\n else:\n for el in elements:\n add_event_listener(el, event_type, func)\n return func\n\n return decorator\n",
|
7
|
-
"magic_js.py": "import sys\nimport js as globalThis\nfrom polyscript import js_modules\nfrom pyscript.util import NotSupported\n\nRUNNING_IN_WORKER = not hasattr(globalThis, \"document\")\n\n# allow `from pyscript.js_modules import
|
7
|
+
"magic_js.py": "import sys\nimport js as globalThis\nfrom polyscript import js_modules\nfrom pyscript.util import NotSupported\n\nRUNNING_IN_WORKER = not hasattr(globalThis, \"document\")\n\n\n# allow `from pyscript.js_modules.xxx import yyy`\nclass JSModule(object):\n def __init__(self, name):\n self.name = name\n\n def __getattr__(self, field):\n # avoid pyodide looking for non existent fields\n if not field.startswith(\"_\"):\n return getattr(getattr(js_modules, self.name), field)\n\n\n# generate N modules in the system that will proxy the real value\nfor name in globalThis.Reflect.ownKeys(js_modules):\n sys.modules[f\"pyscript.js_modules.{name}\"] = JSModule(name)\nsys.modules[\"pyscript.js_modules\"] = js_modules\n\nif RUNNING_IN_WORKER:\n import js\n import polyscript\n\n PyWorker = NotSupported(\n \"pyscript.PyWorker\",\n \"pyscript.PyWorker works only when running in the main thread\",\n )\n window = polyscript.xworker.window\n document = window.document\n js.document = document\n sync = polyscript.xworker.sync\n\n # in workers the display does not have a default ID\n # but there is a sync utility from xworker\n def current_target():\n return polyscript.target\n\nelse:\n import _pyscript\n from _pyscript import PyWorker\n\n window = globalThis\n document = globalThis.document\n sync = NotSupported(\n \"pyscript.sync\", \"pyscript.sync works only when running in a worker\"\n )\n\n # in MAIN the current element target exist, just use it\n def current_target():\n return _pyscript.target\n",
|
8
8
|
"util.py": "class NotSupported:\n \"\"\"\n Small helper that raises exceptions if you try to get/set any attribute on\n it.\n \"\"\"\n\n def __init__(self, name, error):\n object.__setattr__(self, \"name\", name)\n object.__setattr__(self, \"error\", error)\n\n def __repr__(self):\n return f\"<NotSupported {self.name} [{self.error}]>\"\n\n def __getattr__(self, attr):\n raise AttributeError(self.error)\n\n def __setattr__(self, attr, value):\n raise AttributeError(self.error)\n\n def __call__(self, *args):\n raise TypeError(self.error)\n"
|
9
9
|
},
|
10
10
|
"pyweb": {
|
package/dist/core-P7cR7OAZ.js
DELETED
@@ -1,3 +0,0 @@
|
|
1
|
-
const e=(e,t,n=globalThis)=>{const r=Symbol.for(e),s=r in n;return[s?n[r]:Object.defineProperty(n,r,{value:t})[r],s]};Promise.withResolvers||(Promise.withResolvers=function(){var e,t,n=new this((function(n,r){e=n,t=r}));return{resolve:e,reject:t,promise:n}});const t=e=>e.arrayBuffer(),n=e=>e.json(),r=e=>e.text(),s=(e,t=document)=>[...t.querySelectorAll(e)],o=(e,t=document)=>{const n=(new XPathEvaluator).createExpression(e).evaluate(t,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE),r=[];for(let e=0,{snapshotLength:t}=n;e<t;e++)r.push(n.snapshotItem(e));return r},a="object"==typeof self?self:globalThis,i=e=>((e,t)=>{const n=(t,n)=>(e.set(n,t),t),r=s=>{if(e.has(s))return e.get(s);const[o,i]=t[s];switch(o){case 0:case-1:return n(i,s);case 1:{const e=n([],s);for(const t of i)e.push(r(t));return e}case 2:{const e=n({},s);for(const[t,n]of i)e[r(t)]=r(n);return e}case 3:return n(new Date(i),s);case 4:{const{source:e,flags:t}=i;return n(new RegExp(e,t),s)}case 5:{const e=n(new Map,s);for(const[t,n]of i)e.set(r(t),r(n));return e}case 6:{const e=n(new Set,s);for(const t of i)e.add(r(t));return e}case 7:{const{name:e,message:t}=i;return n(new a[e](t),s)}case 8:return n(BigInt(i),s);case"BigInt":return n(Object(BigInt(i)),s)}return n(new a[o](i),s)};return r})(new Map,e)(0),l="",{toString:c}={},{keys:p}=Object,u=e=>{const t=typeof e;if("object"!==t||!e)return[0,t];const n=c.call(e).slice(8,-1);switch(n){case"Array":return[1,l];case"Object":return[2,l];case"Date":return[3,l];case"RegExp":return[4,l];case"Map":return[5,l];case"Set":return[6,l]}return n.includes("Array")?[1,n]:n.includes("Error")?[7,n]:[2,n]},f=([e,t])=>0===e&&("function"===t||"symbol"===t),d=(e,{json:t,lossy:n}={})=>{const r=[];return((e,t,n,r)=>{const s=(e,t)=>{const s=r.push(e)-1;return n.set(t,s),s},o=r=>{if(n.has(r))return n.get(r);let[a,i]=u(r);switch(a){case 0:{let t=r;switch(i){case"bigint":a=8,t=r.toString();break;case"function":case"symbol":if(e)throw new TypeError("unable to serialize "+i);t=null;break;case"undefined":return s([-1],r)}return s([a,t],r)}case 1:{if(i)return s([i,[...r]],r);const e=[],t=s([a,e],r);for(const t of r)e.push(o(t));return t}case 2:{if(i)switch(i){case"BigInt":return s([i,r.toString()],r);case"Boolean":case"Number":case"String":return s([i,r.valueOf()],r)}if(t&&"toJSON"in r)return o(r.toJSON());const n=[],l=s([a,n],r);for(const t of p(r))!e&&f(u(r[t]))||n.push([o(t),o(r[t])]);return l}case 3:return s([a,r.toISOString()],r);case 4:{const{source:e,flags:t}=r;return s([a,{source:e,flags:t}],r)}case 5:{const t=[],n=s([a,t],r);for(const[n,s]of r)(e||!f(u(n))&&!f(u(s)))&&t.push([o(n),o(s)]);return n}case 6:{const t=[],n=s([a,t],r);for(const n of r)!e&&f(u(n))||t.push(o(n));return n}}const{message:l}=r;return s([a,{name:i,message:l}],r)};return o})(!(t||n),!!t,new Map,r)(e),r},{parse:h,stringify:m}=JSON,y={json:!0,lossy:!0};var _=Object.freeze({__proto__:null,parse:e=>i(h(e)),stringify:e=>m(d(e,y))});const g="ac6c1995-0675-46af-a99f-7fa298fa4e6d",w="M"+g,b="T"+g,v="array",E="function",k="null",j="number",R="object",A="string",T="symbol",x="undefined",S="apply",N="construct",O="defineProperty",P="deleteProperty",I="get",$="getOwnPropertyDescriptor",M="getPrototypeOf",F="has",C="isExtensible",H="ownKeys",L="preventExtensions",W="set",D="setPrototypeOf",B="delete";var U=e=>({value:new Promise((t=>{let n=new Worker("data:application/javascript,onmessage%3D(%7Bdata%3Ab%7D)%3D%3E(Atomics.wait(b%2C0)%2CpostMessage(0))");n.onmessage=t,n.postMessage(e)}))})
|
2
|
-
/*! (c) Andrea Giammarchi - ISC */;const{Int32Array:J,Map:q,SharedArrayBuffer:G,Uint16Array:Y}=globalThis,{BYTES_PER_ELEMENT:K}=J,{BYTES_PER_ELEMENT:X}=Y,{isArray:V}=Array,{notify:z,wait:Z,waitAsync:Q}=Atomics,ee=new WeakSet,te=new WeakMap,ne={value:{then:e=>e()}};let re=0;const se=(e,{parse:t=JSON.parse,stringify:n=JSON.stringify,transform:r,interrupt:s}=JSON)=>{if(!te.has(e)){const o=(t,...n)=>e.postMessage({[g]:n},{transfer:t}),a=typeof s===E?s:s?.handler,i=s?.delay||42,l=new TextDecoder("utf-16"),c=(e,t)=>e?(Q||U)(t,0):(a?((e,t,n)=>{for(;"timed-out"===Z(e,0,0,t);)n()})(t,i,a):Z(t,0),ne);let p=!1;te.set(e,new Proxy(new q,{[F]:(e,t)=>"string"==typeof t&&!t.startsWith("_"),[I]:(n,s)=>"then"===s?null:(...n)=>{const a=re++;let i=new J(new G(2*K)),u=[];ee.has(n.at(-1)||u)&&ee.delete(u=n.pop()),o(u,a,i,s,r?n.map(r):n);const f=e!==globalThis;let d=0;return p&&f&&(d=setTimeout(console.warn,1e3,`💀🔒 - Possible deadlock if proxy.${s}(...args) is awaited`)),c(f,i).value.then((()=>{clearTimeout(d);const e=i[1];if(!e)return;const n=X*e;return i=new J(new G(n+n%K)),o([],a,i),c(f,i).value.then((()=>t(l.decode(new Y(i.buffer).slice(0,e)))))}))},[W](t,s,o){const a=typeof o;if(a!==E)throw new Error(`Unable to assign ${s} as ${a}`);if(!t.size){const s=new q;e.addEventListener("message",(async e=>{const o=e.data?.[g];if(V(o)){e.stopImmediatePropagation();const[a,i,...l]=o;let c;if(l.length){const[e,o]=l;if(t.has(e)){p=!0;try{const l=await t.get(e)(...o);if(void 0!==l){const e=n(r?r(l):l);s.set(a,e),i[1]=e.length}}catch(e){c=e}finally{p=!1}}else c=new Error(`Unsupported action: ${e}`);i[0]=1}else{const e=s.get(a);s.delete(a);for(let t=new Y(i.buffer),n=0;n<e.length;n++)t[n]=e.charCodeAt(n)}if(z(i,0),c)throw c}}))}return!!t.set(s,o)}}))}return te.get(e)};se.transfer=(...e)=>(ee.add(e),e);const{isArray:oe}=Array,ae=(e,t)=>t,ie=e=>typeof e===E?(e=>e())(e):e;function le(){return this}const ce=(e,t)=>e===v?[t]:{t:e,v:t},pe=(e,t=ae)=>{let n=typeof e,r=e;return n===R&&(oe(e)?(n=v,r=e.at(0)):({t:n,v:r}=e)),t(n,r)},ue=(e,t)=>e===E?t:ce(e,t),fe=(e,t=ue)=>{const n=null===e?k:typeof e;return t(n===R&&oe(e)?v:n,e)},de=new FinalizationRegistry((([e,t,n])=>{n&&console.debug(`Held value ${String(t)} not relevant anymore`),e(t)})),he=Object.create(null),me=(e,t,{debug:n,return:r,token:s=e}=he)=>{const o=r||new Proxy(e,he),a=[o,[t,e,!!n]];return!1!==s&&a.push(s),de.register(...a),o},{defineProperty:ye,deleteProperty:_e,getOwnPropertyDescriptor:ge,getPrototypeOf:we,isExtensible:be,ownKeys:ve,preventExtensions:Ee,set:ke,setPrototypeOf:je}=Reflect,{assign:Re,create:Ae}=Object,Te=we(Int8Array),xe=(e,t)=>{const{get:n,set:r,value:s}=e;return n&&(e.get=t(n)),r&&(e.set=t(r)),s&&(e.value=t(s)),e},Se=e=>t=>fe(t,((t,n)=>{switch(t){case k:return ce(k,n);case R:if(n===globalThis)return ce(t,null);case v:case E:return e(t,n);case"boolean":case j:case A:case x:case"bigint":return ce(t,n);case T:{if(Ne.has(n))return ce(t,Ne.get(n));let e=Symbol.keyFor(n);if(e)return ce(t,`.${e}`)}}throw new TypeError(`Unable to handle this ${t}: ${String(n)}`)})),Ne=new Map(ve(Symbol).filter((e=>typeof Symbol[e]===T)).map((e=>[Symbol[e],e]))),Oe=e=>{if(e.startsWith("."))return Symbol.for(e.slice(1));for(const[t,n]of Ne)if(n===e)return t},Pe=e=>e;var Ie=((e,t)=>{const n=t&&new WeakMap;if(t){const{addEventListener:e}=EventTarget.prototype;ye(EventTarget.prototype,"addEventListener",{value(t,r,...s){return s.at(0)?.invoke&&(n.has(this)||n.set(this,new Map),n.get(this).set(t,[].concat(s[0].invoke)),delete s[0].invoke),e.call(this,t,r,...s)}})}const r=t&&(e=>{const{currentTarget:t,target:r,type:s}=e;for(const o of n.get(t||r)?.get(s)||[])e[o]()});return function(n,s,o,...a){let i=0,l=this?.transform||Pe;const c=new Map,p=new Map,{[o]:u}=n,f=a.length?Re(Ae(globalThis),...a):globalThis,d=Se(((e,t)=>{if(!c.has(t)){let n;for(;p.has(n=i++););c.set(t,n),p.set(n,e===E?t:l(t))}return ce(e,c.get(t))})),h=e=>{u(B,ce(A,e))},m=(e,n)=>{switch(e){case R:if(null==n)return f;case v:if(typeof n===j)return p.get(n);if(!(n instanceof Te))for(const e in n)n[e]=y(n[e]);return n;case E:if(typeof n===A){if(!p.has(n)){const e=function(...e){return t&&e.at(0)instanceof Event&&r(...e),u(S,ce(E,n),d(this),e.map(d))};return p.set(n,new WeakRef(e)),me(n,h,{return:e,token:!1})}return p.get(n).deref()}return p.get(n);case T:return Oe(n)}return n},y=e=>pe(e,m),_={[S]:(e,t,n)=>d(e.apply(t,n)),[N]:(e,t)=>d(new e(...t)),[O]:(e,t,n)=>d(ye(e,t,n)),[P]:(e,t)=>d(_e(e,t)),[M]:e=>d(we(e)),[I]:(e,t)=>d(e[t]),[$]:(e,t)=>{const n=ge(e,t);return n?ce(R,xe(n,d)):ce(x,n)},[F]:(e,t)=>d(t in e),[C]:e=>d(be(e)),[H]:e=>ce(v,ve(e).map(d)),[L]:e=>d(Ee(e)),[W]:(e,t,n)=>d(ke(e,t,n)),[D]:(e,t)=>d(je(e,t)),[B](e){c.delete(p.get(e)),p.delete(e)}};return n[s]=(e,t,...n)=>{switch(e){case S:n[0]=y(n[0]),n[1]=n[1].map(y);break;case N:n[0]=n[0].map(y);break;case O:{const[e,t]=n;n[0]=y(e);const{get:r,set:s,value:o}=t;r&&(t.get=y(r)),s&&(t.set=y(s)),o&&(t.value=y(o));break}default:n=n.map(y)}return _[e](y(t),...n)},{proxy:n,[e.toLowerCase()]:f,[`is${e}Proxy`]:()=>!1}}})("Window",!0),$e=(e=>{let t=0;const n=new Map,r=new Map,s=Symbol();return function(o,a,i){const l=this?.transform||Pe,{[a]:c}=o,p=new Map,u=e=>{p.delete(e),c(B,f(e))},f=Se(((e,o)=>{if(s in o)return ie(o[s]);if(e===E){if(o=l(o),!r.has(o)){let e;for(;r.has(e=String(t++)););n.set(o,e),r.set(e,o)}return ce(e,n.get(o))}if(!(o instanceof Te)){o=l(o);for(const e in o)o[e]=f(o[e])}return ce(e,o)})),d=(e,t,n)=>{if(!p.has(n)){const r=t===E?(e=>le.bind(e))(e):e,s=new Proxy(r,y);return p.set(n,new WeakRef(s)),me(n,u,{return:s,token:!1})}return p.get(n).deref()},h=e=>pe(e,((t,n)=>{switch(t){case R:if(null===n)return globalThis;case v:return typeof n===j?d(e,t,n):n;case E:return typeof n===A?r.get(n):d(e,t,n);case T:return Oe(n)}return n})),m=(e,t,...n)=>h(c(e,ie(t),...n)),y={[S]:(e,t,n)=>m(S,e,f(t),n.map(f)),[N]:(e,t)=>m(N,e,t.map(f)),[O]:(e,t,n)=>{const{get:r,set:s,value:o}=n;return typeof r===E&&(n.get=f(r)),typeof s===E&&(n.set=f(s)),typeof o===E&&(n.value=f(o)),m(O,e,f(t),n)},[P]:(e,t)=>m(P,e,f(t)),[M]:e=>m(M,e),[I]:(e,t)=>t===s?e:m(I,e,f(t)),[$]:(e,t)=>{const n=m($,e,f(t));return n&&xe(n,h)},[F]:(e,t)=>t===s||m(F,e,f(t)),[C]:e=>m(C,e),[H]:e=>m(H,e).map(h),[L]:e=>m(L,e),[W]:(e,t,n)=>m(W,e,f(t),f(n)),[D]:(e,t)=>m(D,e,f(t))};o[i]=(e,t,s,o)=>{switch(e){case S:return h(t).apply(h(s),o.map(h));case B:{const e=h(t);n.delete(r.get(e)),r.delete(e)}}};const _=new Proxy(ce(R,null),y);return{[e.toLowerCase()]:_,[`is${e}Proxy`]:e=>typeof e===R&&!!e&&s in e,proxy:o}}})("Window"),Me=typeof Worker===E?Worker:class{};const Fe=new WeakMap,Ce=(e,...t)=>{const n=se(e,...t);if(!Fe.has(n)){const r=e instanceof Me?Ie:$e;Fe.set(n,r.call(t.at(0),n,w,b))}return Fe.get(n)};Ce.transfer=se.transfer;const He={object(...e){return this.string(function(e){for(var t=e[0],n=1,r=arguments.length;n<r;n++)t+=arguments[n]+e[n];return t}(...e))},string(e){for(const t of e.split(/[\r\n]+/))if(t.trim().length){/^(\s+)/.test(t)&&(e=e.replace(new RegExp("^"+RegExp.$1,"gm"),""));break}return e}},{replace:Le}="",We=/&(?:amp|#38|lt|#60|gt|#62|apos|#39|quot|#34);/g,De={"&":"&","&":"&","<":"<","<":"<",">":">",">":">","'":"'","'":"'",""":'"',""":'"'},Be=e=>De[e],Ue=(e,...t)=>He[typeof e](e,...t),Je=e=>Le.call(e,We,Be),{isArray:qe}=Array,{assign:Ge,create:Ye,defineProperties:Ke,defineProperty:Xe,entries:Ve}=Object,{all:ze,resolve:Ze}=new Proxy(Promise,{get:(e,t)=>e[t].bind(e)}),Qe=(e,t=location.href)=>new URL(e,t.replace(/^blob:/,"")).href;let et=0;const tt=(e,t)=>({id:e.id||(e.id=`${t}-w${et++}`),tag:e.tagName}),nt=(e,t,n,r=!1,s=CustomEvent)=>{e.dispatchEvent(new s(`${t}:${n}`,{bubbles:!0,detail:{worker:r}}))},rt=(e,t,n,r)=>({type:t,config:n,interpreter:r,io:ut.get(r),run:(t,...n)=>e.run(r,t,...n),runAsync:(t,...n)=>e.runAsync(r,t,...n),runEvent:(...t)=>e.runEvent(r,...t)}),st=(e,t)=>(n,r)=>{const s=e[t].bind(e);e[t]=(e,t,...o)=>s(e,`${r?n:t}\n${r?t:n}`,...o)},ot=Symbol.for("polyscript.js_modules"),at=new Map;Xe(globalThis,ot,{value:at});const it=new Proxy(at,{get:(e,t)=>e.get(t),has:(e,t)=>e.has(t),ownKeys:e=>[...e.keys()]}),lt=(e,t)=>import(e).then((e=>{at.set(t,{...e})})),ct=e=>new Promise(((t,n)=>{document.querySelector(`link[href="${e}"]`)&&t(),document.head.append(Ge(document.createElement("link"),{rel:"stylesheet",href:e,onload:t,onerror:n}))})),pt=e=>/\.css/i.test(new URL(e).pathname),ut=new WeakMap,ft=e=>{const t=e||console,n={stderr:(t.stderr||console.error).bind(t),stdout:(t.stdout||console.log).bind(t)};return{stderr:(...e)=>n.stderr(...e),stdout:(...e)=>n.stdout(...e),async get(e){const t=await e;return ut.set(t,n),t}}},dt=({FS:e,PATH:t,PATH_FS:n},r,s)=>{const o=n.resolve(r);return e.mkdirTree(t.dirname(o)),e.writeFile(o,new Uint8Array(s),{canOwn:!0})},ht=e=>{const t=e.split("/");return t.pop(),t.join("/")},mt=(e,t)=>{const n=[];for(const r of t.split("/"))"."!==r&&(n.push(r),r&&e.mkdir(n.join("/")))},yt=(e,t)=>{const n=[];for(const e of t.split("/"))switch(e){case"":case".":break;case"..":n.pop();break;default:n.push(e)}return[e.cwd()].concat(n).join("/").replace(/^\/+/,"/")},_t=e=>{const t=e.map((e=>e.trim().replace(/(^[/]*|[/]*$)/g,""))).filter((e=>""!==e&&"."!==e)).join("/");return e[0].startsWith("/")?`/${t}`:t},gt=(e,t)=>fetch(Qe(t,wt.get(e))),wt=new WeakMap,bt=(e,n,r)=>ze((e=>{for(const{files:t,to_file:n,from:r=""}of e){if(void 0!==t&&void 0!==n)throw new Error("Cannot use 'to_file' and 'files' parameters together!");if(void 0===t&&void 0===n&&r.endsWith("/"))throw new Error(`Couldn't determine the filename from the path ${r}, please supply 'to_file' parameter.`)}return e.flatMap((({from:e="",to_folder:t=".",to_file:n,files:r})=>{if(qe(r))return r.map((n=>({url:_t([e,n]),path:_t([t,n])})));const s=n||e.slice(1+e.lastIndexOf("/"));return[{url:e,path:_t([t,s])}]}))})(r).map((({url:s,path:o})=>gt(r,s).then(t).then((t=>e.writeFile(n,o,t)))))),vt=(e,t)=>t.endsWith("/")?`${t}${e.split("/").pop()}`:t,Et=(e,t)=>e.replace(/\{.+?\}/g,(e=>{if(!t.has(e))throw new SyntaxError(`Invalid template: ${e}`);return t.get(e)})),kt=(e,n,r)=>ze((e=>{const t=new Map,n=new Set,r=[];for(const[s,o]of Ve(e))if(/^\{.+\}$/.test(s)){if(t.has(s))throw new SyntaxError(`Duplicated template: ${s}`);t.set(s,Et(o,t))}else{const e=Et(s,t),a=vt(e,Et(o||"./",t));if(n.has(a))throw new SyntaxError(`Duplicated destination: ${a}`);n.add(a),r.push({url:e,path:a})}return r})(r).map((({url:s,path:o})=>gt(r,s).then(t).then((t=>e.writeFile(n,o,t)))))),jt=typeof document===x,Rt=({main:e,worker:t})=>{const n=[];if(t&&jt)for(let[e,r]of Ve(t))e=Qe(e,wt.get(t)),n.push(lt(e,r));if(e&&!jt)for(let[t,r]of Ve(e))t=Qe(t,wt.get(e)),pt(t)?ct(t):n.push(lt(t,r));return ze(n)},At=(e,t,n)=>{e.registerJsModule(t,n)},Tt=(e,t,...n)=>{try{return e.runPython(Ue(t),...n)}catch(t){ut.get(e).stderr(t)}},xt=async(e,t,...n)=>{try{return await e.runPythonAsync(Ue(t),...n)}catch(t){ut.get(e).stderr(t)}},St=async(e,t,n)=>{const[r,...s]=t.split(".");let o,a=e.globals.get(r);for(const e of s)[o,a]=[a,a[e]];try{await a.call(o,n)}catch(t){ut.get(e).stderr(t)}};var Nt=(new TextEncoder).encode('from uio import StringIO\nimport sys\n\nclass Response:\n def __init__(self, f):\n self.raw = f\n self.encoding = "utf-8"\n self._cached = None\n\n def close(self):\n if self.raw:\n self.raw.close()\n self.raw = None\n self._cached = None\n\n @property\n def content(self):\n if self._cached is None:\n try:\n self._cached = self.raw.read()\n finally:\n self.raw.close()\n self.raw = None\n return self._cached\n\n @property\n def text(self):\n return str(self.content, self.encoding)\n\n def json(self):\n import ujson\n\n return ujson.loads(self.content)\n\n\n# TODO try to support streaming xhr requests, a-la pyodide-http\nHEADERS_TO_IGNORE = ("user-agent",)\n\n\ntry:\n import js\nexcept Exception as err:\n raise OSError("This version of urequests can only be used in the browser")\n\n# TODO try to support streaming xhr requests, a-la pyodide-http\n\nHEADERS_TO_IGNORE = ("user-agent",)\n\n\ndef request(\n method,\n url,\n data=None,\n json=None,\n headers={},\n stream=None,\n auth=None,\n timeout=None,\n parse_headers=True,\n):\n from js import XMLHttpRequest\n\n xhr = XMLHttpRequest.new()\n xhr.withCredentials = False\n\n if auth is not None:\n import ubinascii\n\n username, password = auth\n xhr.open(method, url, False, username, password)\n else:\n xhr.open(method, url, False)\n\n for name, value in headers.items():\n if name.lower() not in HEADERS_TO_IGNORE:\n xhr.setRequestHeader(name, value)\n\n if timeout:\n xhr.timeout = int(timeout * 1000)\n\n if json is not None:\n assert data is None\n import ujson\n\n data = ujson.dumps(json)\n # s.write(b"Content-Type: application/json\\r\\n")\n xhr.setRequestHeader("Content-Type", "application/json")\n\n xhr.send(data)\n\n # Emulates the construction process in the original urequests\n resp = Response(StringIO(xhr.responseText))\n resp.status_code = xhr.status\n resp.reason = xhr.statusText\n resp.headers = xhr.getAllResponseHeaders()\n\n return resp\n\n\n# Other methods - head, post, put, patch, delete - are not used by\n# mip and therefore not included\n\n\ndef get(url, **kw):\n return request("GET", url, **kw)\n\n\n# Content below this line is from the Micropython MIP package and is covered\n# by the applicable MIT license:\n# \n# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, \n# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER \n# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING \n# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER \n# DEALINGS IN THE SOFTWARE.\n\n# MicroPython package installer\n# MIT license; Copyright (c) 2022 Jim Mussared\n\n\n_PACKAGE_INDEX = const("https://micropython.org/pi/v2")\n_CHUNK_SIZE = 128\n\n\n# This implements os.makedirs(os.dirname(path))\ndef _ensure_path_exists(path):\n import os\n\n split = path.split("/")\n\n # Handle paths starting with "/".\n if not split[0]:\n split.pop(0)\n split[0] = "/" + split[0]\n\n prefix = ""\n for i in range(len(split) - 1):\n prefix += split[i]\n try:\n os.stat(prefix)\n except:\n os.mkdir(prefix)\n prefix += "/"\n\n\n# Copy from src (stream) to dest (function-taking-bytes)\ndef _chunk(src, dest):\n buf = memoryview(bytearray(_CHUNK_SIZE))\n while True:\n n = src.readinto(buf)\n if n == 0:\n break\n dest(buf if n == _CHUNK_SIZE else buf[:n])\n\n\n# Check if the specified path exists and matches the hash.\ndef _check_exists(path, short_hash):\n import os\n\n try:\n import binascii\n import hashlib\n\n with open(path, "rb") as f:\n hs256 = hashlib.sha256()\n _chunk(f, hs256.update)\n existing_hash = str(binascii.hexlify(hs256.digest())[: len(short_hash)], "utf-8")\n return existing_hash == short_hash\n except:\n return False\n\n\ndef _rewrite_url(url, branch=None):\n if not branch:\n branch = "HEAD"\n if url.startswith("github:"):\n url = url[7:].split("/")\n url = (\n "https://raw.githubusercontent.com/"\n + url[0]\n + "/"\n + url[1]\n + "/"\n + branch\n + "/"\n + "/".join(url[2:])\n )\n return url\n\n\ndef _download_file(url, dest):\n response = get(url)\n try:\n if response.status_code != 200:\n print("Error", response.status_code, "requesting", url)\n return False\n\n print("Copying:", dest)\n _ensure_path_exists(dest)\n with open(dest, "wb") as f:\n _chunk(response.raw, f.write)\n\n return True\n finally:\n response.close()\n\n\ndef _install_json(package_json_url, index, target, version, mpy):\n response = get(_rewrite_url(package_json_url, version))\n try:\n if response.status_code != 200:\n print("Package not found:", package_json_url)\n return False\n\n package_json = response.json()\n finally:\n response.close()\n for target_path, short_hash in package_json.get("hashes", ()):\n fs_target_path = target + "/" + target_path\n if _check_exists(fs_target_path, short_hash):\n print("Exists:", fs_target_path)\n else:\n file_url = "{}/file/{}/{}".format(index, short_hash[:2], short_hash)\n if not _download_file(file_url, fs_target_path):\n print("File not found: {} {}".format(target_path, short_hash))\n return False\n for target_path, url in package_json.get("urls", ()):\n fs_target_path = target + "/" + target_path\n if not _download_file(_rewrite_url(url, version), fs_target_path):\n print("File not found: {} {}".format(target_path, url))\n return False\n for dep, dep_version in package_json.get("deps", ()):\n if not _install_package(dep, index, target, dep_version, mpy):\n return False\n return True\n\n\ndef _install_package(package, index, target, version, mpy):\n if (\n package.startswith("http://")\n or package.startswith("https://")\n or package.startswith("github:")\n ):\n if package.endswith(".py") or package.endswith(".mpy"):\n print("Downloading {} to {}".format(package, target))\n return _download_file(\n _rewrite_url(package, version), target + "/" + package.rsplit("/")[-1]\n )\n else:\n if not package.endswith(".json"):\n if not package.endswith("/"):\n package += "/"\n package += "package.json"\n print("Installing {} to {}".format(package, target))\n else:\n if not version:\n version = "latest"\n print("Installing {} ({}) from {} to {}".format(package, version, index, target))\n\n mpy_version = (\n sys.implementation._mpy & 0xFF if mpy and hasattr(sys.implementation, "_mpy") else "py"\n )\n\n package = "{}/package/{}/{}/{}.json".format(index, mpy_version, package, version)\n\n return _install_json(package, index, target, version, mpy)\n\n\ndef install(package, index=None, target=None, version=None, mpy=True):\n if not target:\n for p in sys.path:\n if p.endswith("/lib"):\n target = p\n break\n else:\n print("Unable to find lib dir in sys.path")\n return\n\n if not index:\n index = _PACKAGE_INDEX\n\n if _install_package(package, index.rstrip("/"), target, version, mpy):\n print("Done")\n else:\n print("Package may be partially installed")\n');var Ot={type:"micropython",module:(e="1.21.0-278")=>`https://cdn.jsdelivr.net/npm/@micropython/micropython-webassembly-pyscript@${e}/micropython.mjs`,async engine({loadMicroPython:e},t,n){const{stderr:r,stdout:s,get:o}=ft();n=n.replace(/\.m?js$/,".wasm");const a=await o(e({stderr:r,stdout:s,url:n}));if(t.files&&await kt(this,a,t.files),t.fetch&&await bt(this,a,t.fetch),t.js_modules&&await Rt(t.js_modules),this.writeFile(a,"./mip.py",Nt),t.packages){const e=a.pyimport("mip");for(const n of t.packages)e.install(n)}return a},registerJSModule:At,run:Tt,runAsync:xt,runEvent:St,transform:(e,t)=>t instanceof e.PyProxy?e.PyProxy.toJs(t):t,writeFile:({FS:e,_module:{PATH:t,PATH_FS:n}},r,s)=>dt({FS:e,PATH:t,PATH_FS:n},r,s)};const Pt={dict_converter:Object.fromEntries};var It={type:"pyodide",module:(e="0.24.1")=>`https://cdn.jsdelivr.net/pyodide/v${e}/full/pyodide.mjs`,async engine({loadPyodide:e},t,n){const{stderr:r,stdout:s,get:o}=ft(),a=n.slice(0,n.lastIndexOf("/")),i=await o(e({stderr:r,stdout:s,indexURL:a}));if(t.files&&await kt(this,i,t.files),t.fetch&&await bt(this,i,t.fetch),t.js_modules&&await Rt(t.js_modules),t.packages){await i.loadPackage("micropip");const e=await i.pyimport("micropip");await e.install(t.packages,{keep_going:!0}),e.destroy()}return i},registerJSModule:At,run:Tt,runAsync:xt,runEvent:St,transform:(e,t)=>t instanceof e.ffi.PyProxy?t.toJs(Pt):t,writeFile:({FS:e,PATH:t,_module:{PATH_FS:n}},r,s)=>dt({FS:e,PATH:t,PATH_FS:n},r,s)};const $t="ruby-wasm-wasi",Mt=$t.replace(/\W+/g,"_");var Ft={type:$t,experimental:!0,module:(e="2.2.0")=>`https://cdn.jsdelivr.net/npm/ruby-3_2-wasm-wasi@${e}/dist/browser.esm.js`,async engine({DefaultRubyVM:e},t,n){const r=await fetch(`${n.slice(0,n.lastIndexOf("/"))}/ruby.wasm`),s=await WebAssembly.compile(await r.arrayBuffer()),{vm:o}=await e(s);return t.files&&await kt(this,o,t.files),t.fetch&&await bt(this,o,t.fetch),t.js_modules&&await Rt(t.js_modules),o},registerJSModule(e,t,n){t=t.replace(/\W+/g,"__");const r=`__module_${Mt}_${t}`;globalThis[r]=n,this.run(e,`require "js";$${t}=JS.global[:${r}]`),delete globalThis[r]},run:(e,t,...n)=>e.eval(Ue(t),...n),runAsync:(e,t,...n)=>e.evalAsync(Ue(t),...n),async runEvent(e,t,n){if(/^xworker\.(on\w+)$/.test(t)){const{$1:t}=RegExp,r=`__module_${Mt}_event`;globalThis[r]=n,this.run(e,`require "js";$xworker.call("${t}",JS.global[:${r}])`),delete globalThis[r]}else{const r=this.run(e,`method(:${t})`);await r.call(t,e.wrap(n))}},transform:(e,t)=>t,writeFile:()=>{throw new Error(`writeFile is not supported in ${$t}`)}};var Ct={type:"wasmoon",module:(e="1.16.0")=>`https://cdn.jsdelivr.net/npm/wasmoon@${e}/+esm`,async engine({LuaFactory:e,LuaLibraries:t},n){const{stderr:r,stdout:s,get:o}=ft(),a=await o((new e).createEngine());return a.global.getTable(t.Base,(e=>{a.global.setField(e,"print",s),a.global.setField(e,"printErr",r)})),n.files&&await kt(this,a,n.files),n.fetch&&await bt(this,a,n.fetch),n.js_modules&&await Rt(n.js_modules),a},registerJSModule:(e,t,n)=>{e.global.set(t,n)},run:(e,t,...n)=>{try{return e.doStringSync(Ue(t),...n)}catch(t){ut.get(e).stderr(t)}},runAsync:async(e,t,...n)=>{try{return await e.doString(Ue(t),...n)}catch(t){ut.get(e).stderr(t)}},runEvent:async(e,t,n)=>{const[r,...s]=t.split(".");let o,a=e.global.get(r);for(const e of s)[o,a]=[a,a[e]];try{await a.call(o,n)}catch(t){ut.get(e).stderr(t)}},transform:(e,t)=>t,writeFile:({cmodule:{module:{FS:e}}},t,n)=>((e,t,n)=>(mt(e,ht(t)),t=yt(e,t),e.writeFile(t,new Uint8Array(n),{canOwn:!0})))(e,t,n)};const Ht=new Map,Lt=new Map,Wt=[],Dt=[],Bt=new Proxy(new Map,{get(e,t){if(!e.has(t)){const[n,...r]=t.split("@"),s=Ht.get(n),o=/^(?:\.?\.?\/|https?:\/\/)/i.test(r)?r.join("@"):s.module(...r);e.set(t,{url:o,module:import(o),engine:s.engine.bind(s)})}const{url:n,module:r,engine:s}=e.get(t);return(e,o)=>r.then((r=>{Lt.set(t,e);for(const t of["files","fetch"]){const n=e?.[t];n&&wt.set(n,o)}for(const t of["main","worker"]){const n=e?.js_modules?.[t];n&&wt.set(n,o)}return s(r,e,n)}))}}),Ut=e=>{for(const t of[].concat(e.type))Ht.set(t,e),Wt.push(`script[type="${t}"]`),Dt.push(`${t}-`)};for(const e of[Ot,It,Ft,Ct])Ut(e);const Jt=async e=>(await import("https://cdn.jsdelivr.net/npm/basic-toml@0.3.1/es.js")).parse(e),qt=e=>{let t=typeof e;return"string"===t&&/\.(json|toml|txt)$/.test(e)?t=RegExp.$1:e="./config.txt",[Qe(e),t]},Gt=(e,t,s={})=>{if(t){const[e,o]=qt(t);if("json"===o)s=fetch(e).then(n);else if("toml"===o)s=fetch(e).then(r).then(Jt);else if("string"===o)try{s=JSON.parse(t)}catch(e){s=Jt(t)}else"object"===o&&t&&(s=t);t=e}return Ze(s).then((n=>Bt[e](n,t)))},Yt=(e,t="")=>`${e}@${t}`.replace(/@$/,""),Kt="BeforeRun",Xt="AfterRun",Vt=[`code${Kt}`,`code${Kt}Async`,`code${Xt}`,`code${Xt}Async`],zt=["onWorker","onReady",`on${Kt}`,`on${Kt}Async`,`on${Xt}`,`on${Xt}Async`];function Zt(e,t){const{run:n,runAsync:r}=Ht.get(this.type);return{...e,run:n.bind(this,t),runAsync:r.bind(this,t)}}const Qt=(e,t,n,r,s,o)=>{if(s||o){const a=Zt.bind(e,t),i=r?"runAsync":"run",l=e[i];e[i]=r?async function(e,t,...r){s&&await s.call(this,a(e),n);const i=await l.call(this,e,t,...r);return o&&await o.call(this,a(e),n),i}:function(e,t,...r){s&&s.call(this,a(e),n);const i=l.call(this,e,t,...r);return o&&o.call(this,a(e),n),i}}};let en=class{constructor(e,t={}){const{main:n,worker:r}=t;this.interpreter=e,this.onWorker=n?.onWorker;for(const e of zt.slice(1))this[e]=r?.[e];for(const e of Vt)this[e]=r?.[e]}toJSON(){const e={};for(const t of zt.slice(1))this[t]&&(e[t]=String(this[t]));for(const t of Vt)this[t]&&(e[t]=Ue(this[t]()));return e}};var tn=(...e)=>function(t,n){const s=new Worker(URL.createObjectURL(new Blob(['const e="object"==typeof self?self:globalThis,t=t=>((t,n)=>{const r=(e,n)=>(t.set(n,e),e),s=o=>{if(t.has(o))return t.get(o);const[a,i]=n[o];switch(a){case 0:case-1:return r(i,o);case 1:{const e=r([],o);for(const t of i)e.push(s(t));return e}case 2:{const e=r({},o);for(const[t,n]of i)e[s(t)]=s(n);return e}case 3:return r(new Date(i),o);case 4:{const{source:e,flags:t}=i;return r(new RegExp(e,t),o)}case 5:{const e=r(new Map,o);for(const[t,n]of i)e.set(s(t),s(n));return e}case 6:{const e=r(new Set,o);for(const t of i)e.add(s(t));return e}case 7:{const{name:t,message:n}=i;return r(new e[t](n),o)}case 8:return r(BigInt(i),o);case"BigInt":return r(Object(BigInt(i)),o)}return r(new e[a](i),o)};return s})(new Map,t)(0),n="",{toString:r}={},{keys:s}=Object,o=e=>{const t=typeof e;if("object"!==t||!e)return[0,t];const s=r.call(e).slice(8,-1);switch(s){case"Array":return[1,n];case"Object":return[2,n];case"Date":return[3,n];case"RegExp":return[4,n];case"Map":return[5,n];case"Set":return[6,n]}return s.includes("Array")?[1,s]:s.includes("Error")?[7,s]:[2,s]},a=([e,t])=>0===e&&("function"===t||"symbol"===t),i=(e,{json:t,lossy:n}={})=>{const r=[];return((e,t,n,r)=>{const i=(e,t)=>{const s=r.push(e)-1;return n.set(t,s),s},c=r=>{if(n.has(r))return n.get(r);let[l,u]=o(r);switch(l){case 0:{let t=r;switch(u){case"bigint":l=8,t=r.toString();break;case"function":case"symbol":if(e)throw new TypeError("unable to serialize "+u);t=null;break;case"undefined":return i([-1],r)}return i([l,t],r)}case 1:{if(u)return i([u,[...r]],r);const e=[],t=i([l,e],r);for(const t of r)e.push(c(t));return t}case 2:{if(u)switch(u){case"BigInt":return i([u,r.toString()],r);case"Boolean":case"Number":case"String":return i([u,r.valueOf()],r)}if(t&&"toJSON"in r)return c(r.toJSON());const n=[],p=i([l,n],r);for(const t of s(r))!e&&a(o(r[t]))||n.push([c(t),c(r[t])]);return p}case 3:return i([l,r.toISOString()],r);case 4:{const{source:e,flags:t}=r;return i([l,{source:e,flags:t}],r)}case 5:{const t=[],n=i([l,t],r);for(const[n,s]of r)(e||!a(o(n))&&!a(o(s)))&&t.push([c(n),c(s)]);return n}case 6:{const t=[],n=i([l,t],r);for(const n of r)!e&&a(o(n))||t.push(c(n));return n}}const{message:p}=r;return i([l,{name:u,message:p}],r)};return c})(!(t||n),!!t,new Map,r)(e),r},{parse:c,stringify:l}=JSON,u={json:!0,lossy:!0};var p=Object.freeze({__proto__:null,parse:e=>t(c(e)),stringify:e=>l(i(e,u))});const f="ac6c1995-0675-46af-a99f-7fa298fa4e6d",d="M"+f,h="T"+f,g="array",y="function",m="null",w="number",_="object",b="string",E="symbol",v="undefined",k="apply",T="construct",S="defineProperty",x="deleteProperty",A="get",O="getOwnPropertyDescriptor",j="getPrototypeOf",R="has",P="isExtensible",N="ownKeys",I="preventExtensions",$="set",M="setPrototypeOf",F="delete";var H=e=>({value:new Promise((t=>{let n=new Worker("data:application/javascript,onmessage%3D(%7Bdata%3Ab%7D)%3D%3E(Atomics.wait(b%2C0)%2CpostMessage(0))");n.onmessage=t,n.postMessage(e)}))})\n/*! (c) Andrea Giammarchi - ISC */;const{Int32Array:W,Map:C,SharedArrayBuffer:D,Uint16Array:L}=globalThis,{BYTES_PER_ELEMENT:B}=W,{BYTES_PER_ELEMENT:U}=L,{isArray:J}=Array,{notify:q,wait:G,waitAsync:K}=Atomics,Y=new WeakSet,z=new WeakMap,X={value:{then:e=>e()}};let V=0;const Z=(e,{parse:t=JSON.parse,stringify:n=JSON.stringify,transform:r,interrupt:s}=JSON)=>{if(!z.has(e)){const o=(t,...n)=>e.postMessage({[f]:n},{transfer:t}),a=typeof s===y?s:s?.handler,i=s?.delay||42,c=new TextDecoder("utf-16"),l=(e,t)=>e?(K||H)(t,0):(a?((e,t,n)=>{for(;"timed-out"===G(e,0,0,t);)n()})(t,i,a):G(t,0),X);let u=!1;z.set(e,new Proxy(new C,{[R]:(e,t)=>"string"==typeof t&&!t.startsWith("_"),[A]:(n,s)=>"then"===s?null:(...n)=>{const a=V++;let i=new W(new D(2*B)),p=[];Y.has(n.at(-1)||p)&&Y.delete(p=n.pop()),o(p,a,i,s,r?n.map(r):n);const f=e!==globalThis;let d=0;return u&&f&&(d=setTimeout(console.warn,1e3,`💀🔒 - Possible deadlock if proxy.${s}(...args) is awaited`)),l(f,i).value.then((()=>{clearTimeout(d);const e=i[1];if(!e)return;const n=U*e;return i=new W(new D(n+n%B)),o([],a,i),l(f,i).value.then((()=>t(c.decode(new L(i.buffer).slice(0,e)))))}))},[$](t,s,o){const a=typeof o;if(a!==y)throw new Error(`Unable to assign ${s} as ${a}`);if(!t.size){const s=new C;e.addEventListener("message",(async e=>{const o=e.data?.[f];if(J(o)){e.stopImmediatePropagation();const[a,i,...c]=o;let l;if(c.length){const[e,o]=c;if(t.has(e)){u=!0;try{const c=await t.get(e)(...o);if(void 0!==c){const e=n(r?r(c):c);s.set(a,e),i[1]=e.length}}catch(e){l=e}finally{u=!1}}else l=new Error(`Unsupported action: ${e}`);i[0]=1}else{const e=s.get(a);s.delete(a);for(let t=new L(i.buffer),n=0;n<e.length;n++)t[n]=e.charCodeAt(n)}if(q(i,0),l)throw l}}))}return!!t.set(s,o)}}))}return z.get(e)};Z.transfer=(...e)=>(Y.add(e),e);const{isArray:Q}=Array,ee=(e,t)=>t,te=e=>typeof e===y?(e=>e())(e):e;function ne(){return this}const re=(e,t)=>e===g?[t]:{t:e,v:t},se=(e,t=ee)=>{let n=typeof e,r=e;return n===_&&(Q(e)?(n=g,r=e.at(0)):({t:n,v:r}=e)),t(n,r)},oe=(e,t)=>e===y?t:re(e,t),ae=(e,t=oe)=>{const n=null===e?m:typeof e;return t(n===_&&Q(e)?g:n,e)},ie=new FinalizationRegistry((([e,t,n])=>{n&&console.debug(`Held value ${String(t)} not relevant anymore`),e(t)})),ce=Object.create(null),le=(e,t,{debug:n,return:r,token:s=e}=ce)=>{const o=r||new Proxy(e,ce),a=[o,[t,e,!!n]];return!1!==s&&a.push(s),ie.register(...a),o},{defineProperty:ue,deleteProperty:pe,getOwnPropertyDescriptor:fe,getPrototypeOf:de,isExtensible:he,ownKeys:ge,preventExtensions:ye,set:me,setPrototypeOf:we}=Reflect,{assign:_e,create:be}=Object,Ee=de(Int8Array),ve=(e,t)=>{const{get:n,set:r,value:s}=e;return n&&(e.get=t(n)),r&&(e.set=t(r)),s&&(e.value=t(s)),e},ke=e=>t=>ae(t,((t,n)=>{switch(t){case m:return re(m,n);case _:if(n===globalThis)return re(t,null);case g:case y:return e(t,n);case"boolean":case w:case b:case v:case"bigint":return re(t,n);case E:{if(Te.has(n))return re(t,Te.get(n));let e=Symbol.keyFor(n);if(e)return re(t,`.${e}`)}}throw new TypeError(`Unable to handle this ${t}: ${String(n)}`)})),Te=new Map(ge(Symbol).filter((e=>typeof Symbol[e]===E)).map((e=>[Symbol[e],e]))),Se=e=>{if(e.startsWith("."))return Symbol.for(e.slice(1));for(const[t,n]of Te)if(n===e)return t},xe=e=>e;var Ae=((e,t)=>{const n=t&&new WeakMap;if(t){const{addEventListener:e}=EventTarget.prototype;ue(EventTarget.prototype,"addEventListener",{value(t,r,...s){return s.at(0)?.invoke&&(n.has(this)||n.set(this,new Map),n.get(this).set(t,[].concat(s[0].invoke)),delete s[0].invoke),e.call(this,t,r,...s)}})}const r=t&&(e=>{const{currentTarget:t,target:r,type:s}=e;for(const o of n.get(t||r)?.get(s)||[])e[o]()});return function(n,s,o,...a){let i=0,c=this?.transform||xe;const l=new Map,u=new Map,{[o]:p}=n,f=a.length?_e(be(globalThis),...a):globalThis,d=ke(((e,t)=>{if(!l.has(t)){let n;for(;u.has(n=i++););l.set(t,n),u.set(n,e===y?t:c(t))}return re(e,l.get(t))})),h=e=>{p(F,re(b,e))},m=(e,n)=>{switch(e){case _:if(null==n)return f;case g:if(typeof n===w)return u.get(n);if(!(n instanceof Ee))for(const e in n)n[e]=H(n[e]);return n;case y:if(typeof n===b){if(!u.has(n)){const e=function(...e){return t&&e.at(0)instanceof Event&&r(...e),p(k,re(y,n),d(this),e.map(d))};return u.set(n,new WeakRef(e)),le(n,h,{return:e,token:!1})}return u.get(n).deref()}return u.get(n);case E:return Se(n)}return n},H=e=>se(e,m),W={[k]:(e,t,n)=>d(e.apply(t,n)),[T]:(e,t)=>d(new e(...t)),[S]:(e,t,n)=>d(ue(e,t,n)),[x]:(e,t)=>d(pe(e,t)),[j]:e=>d(de(e)),[A]:(e,t)=>d(e[t]),[O]:(e,t)=>{const n=fe(e,t);return n?re(_,ve(n,d)):re(v,n)},[R]:(e,t)=>d(t in e),[P]:e=>d(he(e)),[N]:e=>re(g,ge(e).map(d)),[I]:e=>d(ye(e)),[$]:(e,t,n)=>d(me(e,t,n)),[M]:(e,t)=>d(we(e,t)),[F](e){l.delete(u.get(e)),u.delete(e)}};return n[s]=(e,t,...n)=>{switch(e){case k:n[0]=H(n[0]),n[1]=n[1].map(H);break;case T:n[0]=n[0].map(H);break;case S:{const[e,t]=n;n[0]=H(e);const{get:r,set:s,value:o}=t;r&&(t.get=H(r)),s&&(t.set=H(s)),o&&(t.value=H(o));break}default:n=n.map(H)}return W[e](H(t),...n)},{proxy:n,[e.toLowerCase()]:f,[`is${e}Proxy`]:()=>!1}}})("Window",!0),Oe=(e=>{let t=0;const n=new Map,r=new Map,s=Symbol();return function(o,a,i){const c=this?.transform||xe,{[a]:l}=o,u=new Map,p=e=>{u.delete(e),l(F,f(e))},f=ke(((e,o)=>{if(s in o)return te(o[s]);if(e===y){if(o=c(o),!r.has(o)){let e;for(;r.has(e=String(t++)););n.set(o,e),r.set(e,o)}return re(e,n.get(o))}if(!(o instanceof Ee)){o=c(o);for(const e in o)o[e]=f(o[e])}return re(e,o)})),d=(e,t,n)=>{if(!u.has(n)){const r=t===y?(e=>ne.bind(e))(e):e,s=new Proxy(r,v);return u.set(n,new WeakRef(s)),le(n,p,{return:s,token:!1})}return u.get(n).deref()},h=e=>se(e,((t,n)=>{switch(t){case _:if(null===n)return globalThis;case g:return typeof n===w?d(e,t,n):n;case y:return typeof n===b?r.get(n):d(e,t,n);case E:return Se(n)}return n})),m=(e,t,...n)=>h(l(e,te(t),...n)),v={[k]:(e,t,n)=>m(k,e,f(t),n.map(f)),[T]:(e,t)=>m(T,e,t.map(f)),[S]:(e,t,n)=>{const{get:r,set:s,value:o}=n;return typeof r===y&&(n.get=f(r)),typeof s===y&&(n.set=f(s)),typeof o===y&&(n.value=f(o)),m(S,e,f(t),n)},[x]:(e,t)=>m(x,e,f(t)),[j]:e=>m(j,e),[A]:(e,t)=>t===s?e:m(A,e,f(t)),[O]:(e,t)=>{const n=m(O,e,f(t));return n&&ve(n,h)},[R]:(e,t)=>t===s||m(R,e,f(t)),[P]:e=>m(P,e),[N]:e=>m(N,e).map(h),[I]:e=>m(I,e),[$]:(e,t,n)=>m($,e,f(t),f(n)),[M]:(e,t)=>m(M,e,f(t))};o[i]=(e,t,s,o)=>{switch(e){case k:return h(t).apply(h(s),o.map(h));case F:{const e=h(t);n.delete(r.get(e)),r.delete(e)}}};const H=new Proxy(re(_,null),v);return{[e.toLowerCase()]:H,[`is${e}Proxy`]:e=>typeof e===_&&!!e&&s in e,proxy:o}}})("Window"),je=typeof Worker===y?Worker:class{};const Re=new WeakMap,Pe=(e,...t)=>{const n=Z(e,...t);if(!Re.has(n)){const r=e instanceof je?Ae:Oe;Re.set(n,r.call(t.at(0),n,d,h))}return Re.get(n)};Pe.transfer=Z.transfer;const Ne={object(...e){return this.string(function(e){for(var t=e[0],n=1,r=arguments.length;n<r;n++)t+=arguments[n]+e[n];return t}(...e))},string(e){for(const t of e.split(/[\\r\\n]+/))if(t.trim().length){/^(\\s+)/.test(t)&&(e=e.replace(new RegExp("^"+RegExp.$1,"gm"),""));break}return e}};Promise.withResolvers||(Promise.withResolvers=function(){var e,t,n=new this((function(n,r){e=n,t=r}));return{resolve:e,reject:t,promise:n}});const Ie=e=>e.arrayBuffer(),$e=e=>e.json(),Me=e=>e.text(),Fe=new WeakMap,He=e=>{const t=e||console,n={stderr:(t.stderr||console.error).bind(t),stdout:(t.stdout||console.log).bind(t)};return{stderr:(...e)=>n.stderr(...e),stdout:(...e)=>n.stdout(...e),async get(e){const t=await e;return Fe.set(t,n),t}}},We=({FS:e,PATH:t,PATH_FS:n},r,s)=>{const o=n.resolve(r);return e.mkdirTree(t.dirname(o)),e.writeFile(o,new Uint8Array(s),{canOwn:!0})},Ce=e=>{const t=e.split("/");return t.pop(),t.join("/")},De=(e,t)=>{const n=[];for(const r of t.split("/"))"."!==r&&(n.push(r),r&&e.mkdir(n.join("/")))},Le=(e,t)=>{const n=[];for(const e of t.split("/"))switch(e){case"":case".":break;case"..":n.pop();break;default:n.push(e)}return[e.cwd()].concat(n).join("/").replace(/^\\/+/,"/")},Be=e=>{const t=e.map((e=>e.trim().replace(/(^[/]*|[/]*$)/g,""))).filter((e=>""!==e&&"."!==e)).join("/");return e[0].startsWith("/")?`/${t}`:t},Ue=(e,t)=>fetch(at(t,Je.get(e))),Je=new WeakMap,qe=(e,t,n)=>st((e=>{for(const{files:t,to_file:n,from:r=""}of e){if(void 0!==t&&void 0!==n)throw new Error("Cannot use \'to_file\' and \'files\' parameters together!");if(void 0===t&&void 0===n&&r.endsWith("/"))throw new Error(`Couldn\'t determine the filename from the path ${r}, please supply \'to_file\' parameter.`)}return e.flatMap((({from:e="",to_folder:t=".",to_file:n,files:r})=>{if(Ze(r))return r.map((n=>({url:Be([e,n]),path:Be([t,n])})));const s=n||e.slice(1+e.lastIndexOf("/"));return[{url:e,path:Be([t,s])}]}))})(n).map((({url:r,path:s})=>Ue(n,r).then(Ie).then((n=>e.writeFile(t,s,n)))))),Ge=(e,t)=>t.endsWith("/")?`${t}${e.split("/").pop()}`:t,Ke=(e,t)=>e.replace(/\\{.+?\\}/g,(e=>{if(!t.has(e))throw new SyntaxError(`Invalid template: ${e}`);return t.get(e)})),Ye=(e,t,n)=>st((e=>{const t=new Map,n=new Set,r=[];for(const[s,o]of rt(e))if(/^\\{.+\\}$/.test(s)){if(t.has(s))throw new SyntaxError(`Duplicated template: ${s}`);t.set(s,Ke(o,t))}else{const e=Ke(s,t),a=Ge(e,Ke(o||"./",t));if(n.has(a))throw new SyntaxError(`Duplicated destination: ${a}`);n.add(a),r.push({url:e,path:a})}return r})(n).map((({url:r,path:s})=>Ue(n,r).then(Ie).then((n=>e.writeFile(t,s,n)))))),ze=typeof document===v,Xe=({main:e,worker:t})=>{const n=[];if(t&&ze)for(let[e,r]of rt(t))e=at(e,Je.get(t)),n.push(pt(e,r));if(e&&!ze)for(let[t,r]of rt(e))t=at(t,Je.get(e)),dt(t)?ft(t):n.push(pt(t,r));return st(n)},Ve=(e,...t)=>Ne[typeof e](e,...t),{isArray:Ze}=Array,{assign:Qe,create:et,defineProperties:tt,defineProperty:nt,entries:rt}=Object,{all:st,resolve:ot}=new Proxy(Promise,{get:(e,t)=>e[t].bind(e)}),at=(e,t=location.href)=>new URL(e,t.replace(/^blob:/,"")).href,it=(e,t,n,r=!1,s=CustomEvent)=>{e.dispatchEvent(new s(`${t}:${n}`,{bubbles:!0,detail:{worker:r}}))},ct=e=>Function(`\'use strict\';return (${e})`)(),lt=Symbol.for("polyscript.js_modules"),ut=new Map;nt(globalThis,lt,{value:ut}),new Proxy(ut,{get:(e,t)=>e.get(t),has:(e,t)=>e.has(t),ownKeys:e=>[...e.keys()]});const pt=(e,t)=>import(e).then((e=>{ut.set(t,{...e})})),ft=e=>new Promise(((t,n)=>{document.querySelector(`link[href="${e}"]`)&&t(),document.head.append(Qe(document.createElement("link"),{rel:"stylesheet",href:e,onload:t,onerror:n}))})),dt=e=>/\\.css/i.test(new URL(e).pathname);const ht=(e,t,n)=>{e.registerJsModule(t,n)},gt=(e,t,...n)=>{try{return e.runPython(Ve(t),...n)}catch(t){Fe.get(e).stderr(t)}},yt=async(e,t,...n)=>{try{return await e.runPythonAsync(Ve(t),...n)}catch(t){Fe.get(e).stderr(t)}},mt=async(e,t,n)=>{const[r,...s]=t.split(".");let o,a=e.globals.get(r);for(const e of s)[o,a]=[a,a[e]];try{await a.call(o,n)}catch(t){Fe.get(e).stderr(t)}};var wt=(new TextEncoder).encode(\'from uio import StringIO\\nimport sys\\n\\nclass Response:\\n def __init__(self, f):\\n self.raw = f\\n self.encoding = "utf-8"\\n self._cached = None\\n\\n def close(self):\\n if self.raw:\\n self.raw.close()\\n self.raw = None\\n self._cached = None\\n\\n @property\\n def content(self):\\n if self._cached is None:\\n try:\\n self._cached = self.raw.read()\\n finally:\\n self.raw.close()\\n self.raw = None\\n return self._cached\\n\\n @property\\n def text(self):\\n return str(self.content, self.encoding)\\n\\n def json(self):\\n import ujson\\n\\n return ujson.loads(self.content)\\n\\n\\n# TODO try to support streaming xhr requests, a-la pyodide-http\\nHEADERS_TO_IGNORE = ("user-agent",)\\n\\n\\ntry:\\n import js\\nexcept Exception as err:\\n raise OSError("This version of urequests can only be used in the browser")\\n\\n# TODO try to support streaming xhr requests, a-la pyodide-http\\n\\nHEADERS_TO_IGNORE = ("user-agent",)\\n\\n\\ndef request(\\n method,\\n url,\\n data=None,\\n json=None,\\n headers={},\\n stream=None,\\n auth=None,\\n timeout=None,\\n parse_headers=True,\\n):\\n from js import XMLHttpRequest\\n\\n xhr = XMLHttpRequest.new()\\n xhr.withCredentials = False\\n\\n if auth is not None:\\n import ubinascii\\n\\n username, password = auth\\n xhr.open(method, url, False, username, password)\\n else:\\n xhr.open(method, url, False)\\n\\n for name, value in headers.items():\\n if name.lower() not in HEADERS_TO_IGNORE:\\n xhr.setRequestHeader(name, value)\\n\\n if timeout:\\n xhr.timeout = int(timeout * 1000)\\n\\n if json is not None:\\n assert data is None\\n import ujson\\n\\n data = ujson.dumps(json)\\n # s.write(b"Content-Type: application/json\\\\r\\\\n")\\n xhr.setRequestHeader("Content-Type", "application/json")\\n\\n xhr.send(data)\\n\\n # Emulates the construction process in the original urequests\\n resp = Response(StringIO(xhr.responseText))\\n resp.status_code = xhr.status\\n resp.reason = xhr.statusText\\n resp.headers = xhr.getAllResponseHeaders()\\n\\n return resp\\n\\n\\n# Other methods - head, post, put, patch, delete - are not used by\\n# mip and therefore not included\\n\\n\\ndef get(url, **kw):\\n return request("GET", url, **kw)\\n\\n\\n# Content below this line is from the Micropython MIP package and is covered\\n# by the applicable MIT license:\\n# \\n# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\\n# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, \\n# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\\n# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER \\n# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING \\n# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER \\n# DEALINGS IN THE SOFTWARE.\\n\\n# MicroPython package installer\\n# MIT license; Copyright (c) 2022 Jim Mussared\\n\\n\\n_PACKAGE_INDEX = const("https://micropython.org/pi/v2")\\n_CHUNK_SIZE = 128\\n\\n\\n# This implements os.makedirs(os.dirname(path))\\ndef _ensure_path_exists(path):\\n import os\\n\\n split = path.split("/")\\n\\n # Handle paths starting with "/".\\n if not split[0]:\\n split.pop(0)\\n split[0] = "/" + split[0]\\n\\n prefix = ""\\n for i in range(len(split) - 1):\\n prefix += split[i]\\n try:\\n os.stat(prefix)\\n except:\\n os.mkdir(prefix)\\n prefix += "/"\\n\\n\\n# Copy from src (stream) to dest (function-taking-bytes)\\ndef _chunk(src, dest):\\n buf = memoryview(bytearray(_CHUNK_SIZE))\\n while True:\\n n = src.readinto(buf)\\n if n == 0:\\n break\\n dest(buf if n == _CHUNK_SIZE else buf[:n])\\n\\n\\n# Check if the specified path exists and matches the hash.\\ndef _check_exists(path, short_hash):\\n import os\\n\\n try:\\n import binascii\\n import hashlib\\n\\n with open(path, "rb") as f:\\n hs256 = hashlib.sha256()\\n _chunk(f, hs256.update)\\n existing_hash = str(binascii.hexlify(hs256.digest())[: len(short_hash)], "utf-8")\\n return existing_hash == short_hash\\n except:\\n return False\\n\\n\\ndef _rewrite_url(url, branch=None):\\n if not branch:\\n branch = "HEAD"\\n if url.startswith("github:"):\\n url = url[7:].split("/")\\n url = (\\n "https://raw.githubusercontent.com/"\\n + url[0]\\n + "/"\\n + url[1]\\n + "/"\\n + branch\\n + "/"\\n + "/".join(url[2:])\\n )\\n return url\\n\\n\\ndef _download_file(url, dest):\\n response = get(url)\\n try:\\n if response.status_code != 200:\\n print("Error", response.status_code, "requesting", url)\\n return False\\n\\n print("Copying:", dest)\\n _ensure_path_exists(dest)\\n with open(dest, "wb") as f:\\n _chunk(response.raw, f.write)\\n\\n return True\\n finally:\\n response.close()\\n\\n\\ndef _install_json(package_json_url, index, target, version, mpy):\\n response = get(_rewrite_url(package_json_url, version))\\n try:\\n if response.status_code != 200:\\n print("Package not found:", package_json_url)\\n return False\\n\\n package_json = response.json()\\n finally:\\n response.close()\\n for target_path, short_hash in package_json.get("hashes", ()):\\n fs_target_path = target + "/" + target_path\\n if _check_exists(fs_target_path, short_hash):\\n print("Exists:", fs_target_path)\\n else:\\n file_url = "{}/file/{}/{}".format(index, short_hash[:2], short_hash)\\n if not _download_file(file_url, fs_target_path):\\n print("File not found: {} {}".format(target_path, short_hash))\\n return False\\n for target_path, url in package_json.get("urls", ()):\\n fs_target_path = target + "/" + target_path\\n if not _download_file(_rewrite_url(url, version), fs_target_path):\\n print("File not found: {} {}".format(target_path, url))\\n return False\\n for dep, dep_version in package_json.get("deps", ()):\\n if not _install_package(dep, index, target, dep_version, mpy):\\n return False\\n return True\\n\\n\\ndef _install_package(package, index, target, version, mpy):\\n if (\\n package.startswith("http://")\\n or package.startswith("https://")\\n or package.startswith("github:")\\n ):\\n if package.endswith(".py") or package.endswith(".mpy"):\\n print("Downloading {} to {}".format(package, target))\\n return _download_file(\\n _rewrite_url(package, version), target + "/" + package.rsplit("/")[-1]\\n )\\n else:\\n if not package.endswith(".json"):\\n if not package.endswith("/"):\\n package += "/"\\n package += "package.json"\\n print("Installing {} to {}".format(package, target))\\n else:\\n if not version:\\n version = "latest"\\n print("Installing {} ({}) from {} to {}".format(package, version, index, target))\\n\\n mpy_version = (\\n sys.implementation._mpy & 0xFF if mpy and hasattr(sys.implementation, "_mpy") else "py"\\n )\\n\\n package = "{}/package/{}/{}/{}.json".format(index, mpy_version, package, version)\\n\\n return _install_json(package, index, target, version, mpy)\\n\\n\\ndef install(package, index=None, target=None, version=None, mpy=True):\\n if not target:\\n for p in sys.path:\\n if p.endswith("/lib"):\\n target = p\\n break\\n else:\\n print("Unable to find lib dir in sys.path")\\n return\\n\\n if not index:\\n index = _PACKAGE_INDEX\\n\\n if _install_package(package, index.rstrip("/"), target, version, mpy):\\n print("Done")\\n else:\\n print("Package may be partially installed")\\n\');var _t={type:"micropython",module:(e="1.21.0-278")=>`https://cdn.jsdelivr.net/npm/@micropython/micropython-webassembly-pyscript@${e}/micropython.mjs`,async engine({loadMicroPython:e},t,n){const{stderr:r,stdout:s,get:o}=He();n=n.replace(/\\.m?js$/,".wasm");const a=await o(e({stderr:r,stdout:s,url:n}));if(t.files&&await Ye(this,a,t.files),t.fetch&&await qe(this,a,t.fetch),t.js_modules&&await Xe(t.js_modules),this.writeFile(a,"./mip.py",wt),t.packages){const e=a.pyimport("mip");for(const n of t.packages)e.install(n)}return a},registerJSModule:ht,run:gt,runAsync:yt,runEvent:mt,transform:(e,t)=>t instanceof e.PyProxy?e.PyProxy.toJs(t):t,writeFile:({FS:e,_module:{PATH:t,PATH_FS:n}},r,s)=>We({FS:e,PATH:t,PATH_FS:n},r,s)};const bt={dict_converter:Object.fromEntries};var Et={type:"pyodide",module:(e="0.24.1")=>`https://cdn.jsdelivr.net/pyodide/v${e}/full/pyodide.mjs`,async engine({loadPyodide:e},t,n){const{stderr:r,stdout:s,get:o}=He(),a=n.slice(0,n.lastIndexOf("/")),i=await o(e({stderr:r,stdout:s,indexURL:a}));if(t.files&&await Ye(this,i,t.files),t.fetch&&await qe(this,i,t.fetch),t.js_modules&&await Xe(t.js_modules),t.packages){await i.loadPackage("micropip");const e=await i.pyimport("micropip");await e.install(t.packages,{keep_going:!0}),e.destroy()}return i},registerJSModule:ht,run:gt,runAsync:yt,runEvent:mt,transform:(e,t)=>t instanceof e.ffi.PyProxy?t.toJs(bt):t,writeFile:({FS:e,PATH:t,_module:{PATH_FS:n}},r,s)=>We({FS:e,PATH:t,PATH_FS:n},r,s)};const vt="ruby-wasm-wasi",kt=vt.replace(/\\W+/g,"_");var Tt={type:vt,experimental:!0,module:(e="2.2.0")=>`https://cdn.jsdelivr.net/npm/ruby-3_2-wasm-wasi@${e}/dist/browser.esm.js`,async engine({DefaultRubyVM:e},t,n){const r=await fetch(`${n.slice(0,n.lastIndexOf("/"))}/ruby.wasm`),s=await WebAssembly.compile(await r.arrayBuffer()),{vm:o}=await e(s);return t.files&&await Ye(this,o,t.files),t.fetch&&await qe(this,o,t.fetch),t.js_modules&&await Xe(t.js_modules),o},registerJSModule(e,t,n){t=t.replace(/\\W+/g,"__");const r=`__module_${kt}_${t}`;globalThis[r]=n,this.run(e,`require "js";$${t}=JS.global[:${r}]`),delete globalThis[r]},run:(e,t,...n)=>e.eval(Ve(t),...n),runAsync:(e,t,...n)=>e.evalAsync(Ve(t),...n),async runEvent(e,t,n){if(/^xworker\\.(on\\w+)$/.test(t)){const{$1:t}=RegExp,r=`__module_${kt}_event`;globalThis[r]=n,this.run(e,`require "js";$xworker.call("${t}",JS.global[:${r}])`),delete globalThis[r]}else{const r=this.run(e,`method(:${t})`);await r.call(t,e.wrap(n))}},transform:(e,t)=>t,writeFile:()=>{throw new Error(`writeFile is not supported in ${vt}`)}};var St={type:"wasmoon",module:(e="1.16.0")=>`https://cdn.jsdelivr.net/npm/wasmoon@${e}/+esm`,async engine({LuaFactory:e,LuaLibraries:t},n){const{stderr:r,stdout:s,get:o}=He(),a=await o((new e).createEngine());return a.global.getTable(t.Base,(e=>{a.global.setField(e,"print",s),a.global.setField(e,"printErr",r)})),n.files&&await Ye(this,a,n.files),n.fetch&&await qe(this,a,n.fetch),n.js_modules&&await Xe(n.js_modules),a},registerJSModule:(e,t,n)=>{e.global.set(t,n)},run:(e,t,...n)=>{try{return e.doStringSync(Ve(t),...n)}catch(t){Fe.get(e).stderr(t)}},runAsync:async(e,t,...n)=>{try{return await e.doString(Ve(t),...n)}catch(t){Fe.get(e).stderr(t)}},runEvent:async(e,t,n)=>{const[r,...s]=t.split(".");let o,a=e.global.get(r);for(const e of s)[o,a]=[a,a[e]];try{await a.call(o,n)}catch(t){Fe.get(e).stderr(t)}},transform:(e,t)=>t,writeFile:({cmodule:{module:{FS:e}}},t,n)=>((e,t,n)=>(De(e,Ce(t)),t=Le(e,t),e.writeFile(t,new Uint8Array(n),{canOwn:!0})))(e,t,n)};const xt=new Map,At=new Map,Ot=new Proxy(new Map,{get(e,t){if(!e.has(t)){const[n,...r]=t.split("@"),s=xt.get(n),o=/^(?:\\.?\\.?\\/|https?:\\/\\/)/i.test(r)?r.join("@"):s.module(...r);e.set(t,{url:o,module:import(o),engine:s.engine.bind(s)})}const{url:n,module:r,engine:s}=e.get(t);return(e,o)=>r.then((r=>{At.set(t,e);for(const t of["files","fetch"]){const n=e?.[t];n&&Je.set(n,o)}for(const t of["main","worker"]){const n=e?.js_modules?.[t];n&&Je.set(n,o)}return s(r,e,n)}))}}),jt=e=>{for(const t of[].concat(e.type))xt.set(t,e)};for(const e of[_t,Et,Tt,St])jt(e);const Rt=async e=>(await import("https://cdn.jsdelivr.net/npm/basic-toml@0.3.1/es.js")).parse(e),Pt=(e,t,n={})=>{if(t){const[e,r]=(e=>{let t=typeof e;return"string"===t&&/\\.(json|toml|txt)$/.test(e)?t=RegExp.$1:e="./config.txt",[at(e),t]})(t);if("json"===r)n=fetch(e).then($e);else if("toml"===r)n=fetch(e).then(Me).then(Rt);else if("string"===r)try{n=JSON.parse(t)}catch(e){n=Rt(t)}else"object"===r&&t&&(n=t);t=e}return ot(n).then((n=>Ot[e](n,t)))},Nt="BeforeRun",It="AfterRun",$t=[`code${Nt}`,`code${Nt}Async`,`code${It}`,`code${It}Async`],Mt=["onWorker","onReady",`on${Nt}`,`on${Nt}Async`,`on${It}`,`on${It}Async`];function Ft(e,t){const{run:n,runAsync:r}=xt.get(this.type);return{...e,run:n.bind(this,t),runAsync:r.bind(this,t)}}const Ht=(e,t,n,r,s,o)=>{if(s||o){const a=Ft.bind(e,t),i=r?"runAsync":"run",c=e[i];e[i]=r?async function(e,t,...r){s&&await s.call(this,a(e),n);const i=await c.call(this,e,t,...r);return o&&await o.call(this,a(e),n),i}:function(e,t,...r){s&&s.call(this,a(e),n);const i=c.call(this,e,t,...r);return o&&o.call(this,a(e),n),i}}};try{new SharedArrayBuffer(4)}catch(e){throw new Error(["Unable to use SharedArrayBuffer due insecure environment.","Please read requirements in MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements"].join("\\n"))}let Wt,Ct,Dt;const Lt=(e,t)=>{addEventListener(e,t||(async t=>{try{await Wt,Ct(`xworker.on${e}`,t)}catch(e){postMessage(e)}}),!!t&&{once:!0})},{parse:Bt,stringify:Ut}=p,{proxy:Jt,window:qt,isWindowProxy:Gt}=Pe(self,{parse:Bt,stringify:Ut,transform:e=>Dt?Dt(e):e}),Kt={sync:Jt,window:qt,isWindowProxy:Gt,onmessage:console.info,onerror:console.error,onmessageerror:console.warn,postMessage:postMessage.bind(self)};Lt("message",(({data:{options:e,config:t,code:n,hooks:r}})=>{Wt=(async()=>{try{const{id:s,tag:o,type:a,custom:i,version:c,config:l,async:u}=e,p=((e,t="")=>`${e}@${t}`.replace(/@$/,""))(a,c),f=await Pt(p,t,l),d=At.get(p).js_modules?.main,h=et(xt.get(a)),g=((e,t,n,r)=>({type:t,config:n,interpreter:r,io:Fe.get(r),run:(t,...n)=>e.run(r,t,...n),runAsync:(t,...n)=>e.runAsync(r,t,...n),runEvent:(...t)=>e.runEvent(r,...t)}))(h,i||a,l,f);let y="run";if(u&&(y+="Async"),r){const e=((e,t)=>(n,r)=>{const s=e[t].bind(e);e[t]=(e,t,...o)=>s(e,`${r?n:t}\\n${r?t:n}`,...o)})(h,y);let t,n,s="",o="";for(const e of $t){const t=r[e];if(t){const n=e.endsWith("Async");(n&&u||!n&&!u)&&(e.startsWith("codeBefore")?s=t:o=t)}}o&&e(o,!1),s&&e(s,!0);for(const e of Mt.slice(2)){const s=r[e];if(s){const r=e.endsWith("Async");if(r&&u||!r&&!u){const r=ct(s);e.startsWith("onBefore")?t=r:n=r}}}Ht(h,g,Kt,u,t,n)}const{CustomEvent:m,document:w}=qt,_=s&&w.getElementById(s)||null,b=e=>it(_,i||a,e,!0,m),E=((e,t,n)=>{const r={},s={},o=new Set,a=new Map;for(const[e,t]of globalThis[lt])o.add(e),s[e]={value:t};if(n){for(let[e,t]of rt(n)){if(o.has(t))continue;let n=a.get(t);n||a.set(t,n=[]),n.push(e)}for(const[o,i]of a)s[o]={configurable:!0,get(){let s;for(let r of i)r=at(r,Je.get(n)),dt(r)?t.importCSS(r):(t.importJS(r,o),s=e[lt].get(o));return nt(r,o,{configurable:!1,get:()=>s}),s}}}return tt(r,s)})(qt,Jt,d);let v="";return h.registerJSModule(f,"polyscript.js_modules",E),h.registerJSModule(f,"polyscript",{xworker:Kt,js_modules:E,get target(){return!v&&_&&("SCRIPT"===o?_.after(Qe(w.createElement(`script-${i||a}`),{id:v=`${s}-target`})):(v=s,_.replaceChildren(),_.style.display="block")),v}}),Ct=h.runEvent.bind(h,f),Dt=h.transform.bind(h,f),_&&b("ready"),r?.onReady&&ct(r?.onReady).call(h,Ft.call(h,g,f),Kt),await h[y](f,n),_&&b("done"),f}catch(e){postMessage(e)}})(),Lt("error"),Lt("message"),Lt("messageerror")}));\n'],{type:"application/javascript"})),{type:"module"}),{postMessage:o}=s,a=this instanceof en;if(e.length){const[t,r]=e;(n=Ge({},n||{type:t,version:r})).type||(n.type=t)}const[i]=qt(n.config),l=fetch(t).then(r).then((e=>{const t=a?this.toJSON():void 0;o.call(s,{options:n,config:i,code:e,hooks:t})})),c=Ge(Ce(s,_).proxy,{importJS:lt,importCSS:ct});return Ke(s,{sync:{value:c},postMessage:{value:(e,...t)=>l.then((()=>o.call(s,e,...t)))},onerror:{writable:!0,configurable:!0,value:console.error}}),s.addEventListener("message",(e=>{const{data:t}=e;t instanceof Error&&(e.stopImmediatePropagation(),s.onerror(Ye(e,{type:{value:"error"},error:{value:t}})))})),a&&this.onWorker?.(this.interpreter,s),s};const nn="Invalid content";var rn=e=>{const{src:t,worker:n}=e.attributes;if(n){let{value:r}=n;if(r)throw new SyntaxError("Invalid worker attribute");if(r=t?.value,!r){if(t)throw new SyntaxError("Invalid worker attribute");if(e.childElementCount){const{innerHTML:t,localName:n,type:s}=e,o=s||n.replace(/-script$/,"");r=Je(t),console.warn(`Deprecated: use <script type="${o}"> for an always safe content parsing:\n`,r)}else r=e.textContent;return URL.createObjectURL(new Blob([Ue(r)],{type:"text/plain"}))}return r}if(t&&e.textContent.replace(/\/\*[\s\S]*?\*\//g,"").replace(/^\s*(?:\/\/|#).*/gm,"").trim())throw new SyntaxError(nn)};const sn=(e,t)=>{const n=(e=>{let t=e;for(;t.parentNode;)t=t.parentNode;return t})(e);return n.getElementById(t)||((e,t=document)=>t.querySelector(e))(t,n)},on=new WeakMap,an={get(){let e=on.get(this);return e||(e=document.createElement(`${this.type}-script`),on.set(this,e),fn(this)),e},set(e){"string"==typeof e?on.set(this,sn(this,e)):(on.set(this,e),fn(this))}},ln=new WeakMap,cn=new Map,pn=(e,t)=>{const n=e?.value;return n?t+n:""},un=(e,t,n,r,s,o=e)=>{if(!cn.has(t)){const a={interpreter:Gt(n,s),queue:Ze(),XWorker:tn(e,r)};cn.set(t,a),cn.has(e)||cn.set(e,a),cn.has(o)||cn.set(o,a)}return cn.get(t)},fn=async e=>{if(ln.has(e)){const{target:t}=e;t&&(e.closest("head")?document.body.append(t):e.after(t))}else{const{attributes:{async:t,config:n,env:s,target:o,version:a},src:i,type:l}=e,c=a?.value,p=Yt(l,c);let u=pn(n,"|");const f=pn(s,"")||`${p}${u}`;u=u.slice(1);const d=rn(e);if(d){const n=new(tn(l,c))(d,{...tt(e,l),async:!!t,config:u});return void ln.set(Xe(e,"xworker",{value:n}),{xworker:n})}const h=pn(o,""),m=un(l,f,p,c,u);ln.set(Xe(e,"target",an),m),h&&on.set(e,sn(e,h));const y=i?fetch(i).then(r):e.textContent;m.queue=m.queue.then((()=>(async(e,t,n,r)=>{const{type:s}=e,o=Ht.get(s);o.experimental&&console.warn(`The ${s} interpreter is experimental`);const[a,i]=await ze([ln.get(e).interpreter,t]);try{Xe(document,"currentScript",{configurable:!0,get:()=>e}),o.registerJSModule(a,"polyscript.js_modules",it),o.registerJSModule(a,"polyscript",{js_modules:it,XWorker:n}),nt(e,s,"ready");const t=o[r?"runAsync":"run"](a,i),l=nt.bind(null,e,s,"done");return r?t.then(l):l(),t}finally{delete document.currentScript}})(e,y,m.XWorker,!!t)))}},dn=new Proxy(Ye(null),{get:(e,t)=>new Promise(queueMicrotask).then((()=>hn(t)))}),hn=async e=>{if(cn.has(e)){const{interpreter:t,queue:n}=cn.get(e);return(await ze([t,n]))[0]}const t=cn.size?`Available interpreters are: ${[...cn.keys()].map((e=>`"${e}"`)).join(", ")}.`:"There are no interpreters in this page.";throw new Error(`The interpreter "${e}" was not found. ${t}`)},mn=async e=>{const{type:t,currentTarget:n}=e;if(Dt.length)for(let{name:r,value:s,ownerElement:a}of o(`./@*[${Dt.map((e=>`name()="${e}${t}"`)).join(" or ")}]`,n)){r=r.slice(0,-(t.length+1));const n=await hn(a.getAttribute(`${r}-env`)||r);Ht.get(r).runEvent(n,s,e)}},yn=e=>{if(Dt.length)for(let{name:t,ownerElement:n}of o(`.//@*[${Dt.map((e=>`starts-with(name(),"${e}")`)).join(" or ")}]`,e)){const e=t.lastIndexOf("-"),r=t.slice(e+1);"env"!==r&&(n.addEventListener(r,mn),"disabled"in n&&!n.disabled&&(n.disabled=!0,dn[t.slice(0,e)].then((()=>{n.disabled=!1}))))}},_n=tn(),gn=[],wn=new Map,bn=new Map,vn=e=>{for(const t of gn)if(e.matches(t)){const n=wn.get(t),r=En.get(n),{resolve:s}=bn.get(n),{options:o,known:a}=r;if(!a.has(e)){a.add(e);const{interpreter:t,config:i,version:l,env:c,onerror:p,hooks:u}=o;let f;try{const r=rn(e);if(r){const o=_n.call(new en(null,u),r,{...tt(e,n),version:l,type:t,custom:n,config:e.getAttribute("config")||i||{},async:e.hasAttribute("async")});return Xe(e,"xworker",{value:o}),void s({type:n,xworker:o})}}catch(e){f=e}const d=Yt(t,l),h=c||`${d}${i?`|${i}`:""}`,{interpreter:m,XWorker:y}=un(n,h,d,l,i,t);m.then((o=>{const a=Ye(Ht.get(t)),i=new en(o,u),l=function(...e){return y.apply(i,e)},c={...rt(a,n,structuredClone(Lt.get(d)),o),XWorker:l};a.registerJSModule(o,"polyscript.js_modules",it),a.registerJSModule(o,"polyscript",{js_modules:it,XWorker:l});for(const t of["Run","RunAsync"]){const n=st(a,`r${t.slice(1)}`);let r,s,o="",i="";for(const e of Vt){const n=u?.main?.[e];n&&e.endsWith(t)&&(e.startsWith("codeBefore")?o=Ue(n()):i=Ue(n()))}i&&n(i,!1),o&&n(o,!0);for(let e=2;e<zt.length;e++){const n=zt[e],o=u?.main?.[n];o&&n.endsWith(t)&&(n.startsWith("onBefore")?r=o:s=o)}Qt(a,c,e,t.endsWith("Async"),r,s)}r.queue=r.queue.then((()=>(s(c),f&&p?.(f,e),u?.main?.onReady?.(c,e))))}))}}},En=new Map;let kn=0;const jn=e=>(bn.has(e)||bn.set(e,Promise.withResolvers()),bn.get(e).promise),[{define:Rn,whenDefined:An,env:Tn,Hook:xn,XWorker:Sn},Nn]=e("polyscript",{define:(e,t)=>{let n=null==e;if(n)e="_ps"+kn++;else if(Ht.has(e)||En.has(e))throw new Error(`<script type="${e}"> already registered`);if(!Ht.has(t?.interpreter))throw new Error("Unspecified interpreter");Ht.set(e,Ht.get(t.interpreter));const r=[`script[type="${e}"]`];if(jn(e),n){const{hooks:n}=t,r=n?.main?.onReady;t={...t,hooks:{...n,main:{...n?.main,onReady(t,n){gn.splice(gn.indexOf(e),1),Ht.delete(e),En.delete(e),bn.delete(e),n.remove(),r?.(t)}}}},document.head.append(Ge(document.createElement("script"),{type:e}))}else r.push(`${e}-script`),Dt.push(`${e}-`);for(const t of r)wn.set(t,e);gn.push(...r),En.set(e,{options:Ge({env:e},t),known:new WeakSet,queue:Promise.resolve()}),n||yn(document),s(r.join(",")).forEach(vn)},whenDefined:jn,env:dn,Hook:en,XWorker:_n});if(!Nn){const e=new MutationObserver((e=>{const n=Wt.join(",");for(const{type:r,target:s,attributeName:o,addedNodes:a}of e)if("attributes"!==r)for(const e of a)1===e.nodeType&&(yn(e),n&&e.matches(n)?fn(e):t(n,e,!0));else{const e=o.lastIndexOf("-")+1;if(e){const t=o.slice(0,e);for(const n of Dt)if(t===n){const t=o.slice(e);if("env"!==t){const e=s.hasAttribute(o)?"add":"remove";s[`${e}EventListener`](t,mn)}break}}}})),t=(e,t,n)=>{e&&s(e,t).forEach(fn),(e=gn.join(","))&&(n&&vn(t),s(e,t).forEach(vn))},n=t=>(e.observe(t,{childList:!0,subtree:!0,attributes:!0}),t),{attachShadow:r}=Element.prototype;Ge(Element.prototype,{attachShadow(e){return n(r.call(this,e))}}),queueMicrotask((()=>{yn(n(document)),t(Wt.join(","),document,!1)}))}var On=new Map([["py","pyodide"],["mpy","micropython"]]);const Pn=[];for(const[e]of On){const t=[`script[type="${e}"]`,`${e}-script`];for(const n of document.querySelectorAll(t.join(","))){const{promise:t,resolve:r}=Promise.withResolvers();Pn.push(t),n.addEventListener(`${e}:done`,r,{once:!0})}}Promise.all(Pn).then((()=>{dispatchEvent(new Event("py:all-done"))}));var In={"deprecations-manager":()=>import("./deprecations-manager-WuZTBZG0.js"),error:()=>import("./error-6r_xlA-Y.js"),"py-editor":()=>import("./py-editor-MQAOiLee.js"),"py-terminal":()=>import("./py-terminal-wDa_Xkqt.js")};const $n={GENERIC:"PY0000",CONFLICTING_CODE:"PY0409",BAD_CONFIG:"PY1000",MICROPIP_INSTALL_ERROR:"PY1001",BAD_PLUGIN_FILE_EXTENSION:"PY2000",NO_DEFAULT_EXPORT:"PY2001",TOP_LEVEL_AWAIT:"PY9000",FETCH_ERROR:"PY0001",FETCH_NAME_ERROR:"PY0002",FETCH_UNAUTHORIZED_ERROR:"PY0401",FETCH_FORBIDDEN_ERROR:"PY0403",FETCH_NOT_FOUND_ERROR:"PY0404",FETCH_SERVER_ERROR:"PY0500",FETCH_UNAVAILABLE_ERROR:"PY0503"};class Mn extends Error{constructor(e,t="",n="text"){super(`(${e}): ${t}`),this.errorCode=e,this.messageType=n,this.name="UserError"}}class Fn extends Mn{constructor(e,t){super(e,t),this.name="FetchError"}}async function Cn(e,t){let n;try{n=await fetch(e,t)}catch(t){const n=t;let r;throw r=e.startsWith("http")?`Fetching from URL ${e} failed with error '${n.message}'. Are your filename and path correct?`:'Polyscript: Access to local files\n (using [[fetch]] configurations in <py-config>)\n is not available when directly opening a HTML file;\n you must use a webserver to serve the additional files.\n See <a style="text-decoration: underline;" href="https://github.com/pyscript/pyscript/issues/257#issuecomment-1119595062">this reference</a>\n on starting a simple webserver with Python.\n ',new Fn($n.FETCH_ERROR,r)}if(!n.ok){const t=`Fetching from URL ${e} failed with error ${n.status} (${n.statusText}). Are your filename and path correct?`;switch(n.status){case 404:throw new Fn($n.FETCH_NOT_FOUND_ERROR,t);case 401:throw new Fn($n.FETCH_UNAUTHORIZED_ERROR,t);case 403:throw new Fn($n.FETCH_FORBIDDEN_ERROR,t);case 500:throw new Fn($n.FETCH_SERVER_ERROR,t);case 503:throw new Fn($n.FETCH_UNAVAILABLE_ERROR,t);default:throw new Fn($n.FETCH_ERROR,t)}}return n}const{BAD_CONFIG:Hn,CONFLICTING_CODE:Ln}=$n,Wn=async(e,t)=>{let n=e?.trim(),s="",o=!1,a=/^{/.test(n)&&/}$/.test(n);if(!a&&/\.(\w+)(?:\?\S*)?$/.test(n)){const e=RegExp.$1;"json"===e&&"toml"!==t?a=!0:"toml"===e&&"json"!==t?o=!0:((e,t="")=>{let n=`(${Hn}): Invalid URL: ${e}`;throw t&&(n+=`\nexpected ${t} content`),new Error(n)})(n,t),s=n,n=(await Cn(s).then(r)).trim()}return{json:a,toml:o||!a&&!!n,text:n,url:s}},Dn=e=>new Error(`(${Ln}): ${e}`),Bn=(e,t,{message:n})=>{let r=`(${Hn}): Invalid ${e}`;return t&&(r+=` @ ${t}`),new SyntaxError(`${r}\n${n}`)},Un=new Map;for(const[e]of On){let t,n,r,o,a,i,l=s(`${e}-config`),c=s([`script[type="${e}"][config]:not([worker])`,`${e}-script[config]:not([worker])`].join(","));if(l.length>1?r=Dn(`Too many ${e}-config`):l.length&&c.length?r=Dn(`Ambiguous ${e}-config VS config attribute`):l.length?([i]=l,o=i.getAttribute("src")||i.textContent,a=i.getAttribute("type")):c.length&&([i,...c]=c,o=i.getAttribute("config"),c.some((e=>e.getAttribute("config")!==o))&&(r=Dn("Unable to use different configs on main"))),!r&&o)try{const{json:e,toml:t,text:s,url:i}=await Wn(o,a);if(o=s,e||"json"===a)try{n=JSON.parse(s)}catch(e){r=Bn("JSON",i,e)}else if(t||"toml"===a)try{const{parse:e}=await import("./toml--Dzglv4T.js");n=e(s)}catch(e){r=Bn("TOML",i,e)}}catch(e){r=e}const p=[];for(const[e,t]of Object.entries(In))r?"error"===e&&t().then((({notify:e})=>e(r.message))):n?.plugins?.includes(`!${e}`)||p.push(t().then((({default:e})=>e)));t=Promise.all(p),Un.set(e,{config:n,plugins:t,error:r})}var Jn={sleep:e=>new Promise((t=>setTimeout(t,1e3*e)))};const qn=e=>{Xe(document,"currentScript",{configurable:!0,get:()=>e})},Gn=()=>{delete document.currentScript};var Yn=async(e,t,n,r)=>{const s=r.endsWith("Async");(r.startsWith("onBefore")?qn:Gn)(n);for(const o of e(r))s?await o(t,n):o(t,n)};const Kn=()=>!0,Xn=e=>{throw new TypeError(e)},Vn=(e,t)=>{const n=[];if(e)for(const t of e.split(/\s*\|\s*/))"object"===t?n.push((e=>null!==e&&typeof e===t)):"null"===t?n.push((e=>null===e)):n.push((e=>typeof e===t));if(t)for(const e of[].concat(t))n.push((t=>t instanceof e));switch(n.length){case 0:return Kn;case 1:return n[0];default:return e=>n.some((t=>t(e)))}},zn=(e,t,n,r=Xn)=>s=>{const o=[`Invalid ${typeof s} ${n}: expected `];e&&(o.push(e),t&&o.push(" or ")),t&&(o.push("an instanceof "),o.push([].concat(t).map((({name:e})=>e)).join(" | "))),r(o.join(""),s)},Zn=(e=>t=>{const[n,r]=((e,t="value")=>{const n=e?.typeof,r=e?.instanceof;return[Vn(n,r),zn(n,r,t,e?.onerror)]})(t);return class extends e{add(e){return n(e)?super.add(e):r(e)}}})(Set);function Qn(e=this){return String(e).replace(/^(async\s*)?(\bfunction\b)?(.*?)\(/,((e,t,n,r)=>r&&!n?`${t||""}function ${r}(`:e))}const{entries:er}=Object,tr=["import os as _os","from pathlib import Path as _Path","_path = None"],nr=(e,t)=>{for(const[n,r]of er(t))if(tr.push(`_path = _Path("${e}/${n}")`),"string"==typeof r){const e=JSON.stringify(r);tr.push(`_path.write_text(${e})`)}else tr.push(`if not _os.path.exists("${e}/${n}"):`),tr.push(" _path.mkdir(parents=True, exist_ok=True)"),nr(`${e}/${n}`,r)};nr(".",{pyscript:{"__init__.py":'# Some notes about the naming conventions and the relationship between various\n# similar-but-different names.\n#\n# import pyscript\n# this package contains the main user-facing API offered by pyscript. All\n# the names which are supposed be used by end users should be made\n# available in pyscript/__init__.py (i.e., this file)\n#\n# import _pyscript\n# this is an internal module implemented in JS. It is used internally by\n# the pyscript package, end users should not use it directly. For its\n# implementation, grep for `interpreter.registerJsModule("_pyscript",\n# ...)` in core.js\n#\n# import js\n# this is the JS globalThis, as exported by pyodide and/or micropython\'s\n# FFIs. As such, it contains different things in the main thread or in a\n# worker.\n#\n# import pyscript.magic_js\n# this submodule abstracts away some of the differences between the main\n# thread and the worker. In particular, it defines `window` and `document`\n# in such a way that these names work in both cases: in the main thread,\n# they are the "real" objects, in the worker they are proxies which work\n# thanks to coincident.\n#\n# from pyscript import window, document\n# these are just the window and document objects as defined by\n# pyscript.magic_js. This is the blessed way to access them from pyscript,\n# as it works transparently in both the main thread and worker cases.\n\nfrom pyscript.display import HTML, display\nfrom pyscript.magic_js import (\n RUNNING_IN_WORKER,\n PyWorker,\n current_target,\n document,\n js_modules,\n sync,\n window,\n)\n\ntry:\n from pyscript.event_handling import when\nexcept:\n from pyscript.util import NotSupported\n\n when = NotSupported(\n "pyscript.when", "pyscript.when currently not available with this interpreter"\n )\n',"display.py":'import base64\nimport html\nimport io\nimport re\n\nfrom pyscript.magic_js import current_target, document, window\n\n_MIME_METHODS = {\n "__repr__": "text/plain",\n "_repr_html_": "text/html",\n "_repr_markdown_": "text/markdown",\n "_repr_svg_": "image/svg+xml",\n "_repr_pdf_": "application/pdf",\n "_repr_jpeg_": "image/jpeg",\n "_repr_png_": "image/png",\n "_repr_latex": "text/latex",\n "_repr_json_": "application/json",\n "_repr_javascript_": "application/javascript",\n "savefig": "image/png",\n}\n\n\ndef _render_image(mime, value, meta):\n # If the image value is using bytes we should convert it to base64\n # otherwise it will return raw bytes and the browser will not be able to\n # render it.\n if isinstance(value, bytes):\n value = base64.b64encode(value).decode("utf-8")\n\n # This is the pattern of base64 strings\n base64_pattern = re.compile(\n r"^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$"\n )\n # If value doesn\'t match the base64 pattern we should encode it to base64\n if len(value) > 0 and not base64_pattern.match(value):\n value = base64.b64encode(value.encode("utf-8")).decode("utf-8")\n\n data = f"data:{mime};charset=utf-8;base64,{value}"\n attrs = " ".join([\'{k}="{v}"\' for k, v in meta.items()])\n return f\'<img src="{data}" {attrs}></img>\'\n\n\ndef _identity(value, meta):\n return value\n\n\n_MIME_RENDERERS = {\n "text/plain": html.escape,\n "text/html": _identity,\n "image/png": lambda value, meta: _render_image("image/png", value, meta),\n "image/jpeg": lambda value, meta: _render_image("image/jpeg", value, meta),\n "image/svg+xml": _identity,\n "application/json": _identity,\n "application/javascript": lambda value, meta: f"<script>{value}<\\\\/script>",\n}\n\n\nclass HTML:\n """\n Wrap a string so that display() can render it as plain HTML\n """\n\n def __init__(self, html):\n self._html = html\n\n def _repr_html_(self):\n return self._html\n\n\ndef _eval_formatter(obj, print_method):\n """\n Evaluates a formatter method.\n """\n if print_method == "__repr__":\n return repr(obj)\n elif hasattr(obj, print_method):\n if print_method == "savefig":\n buf = io.BytesIO()\n obj.savefig(buf, format="png")\n buf.seek(0)\n return base64.b64encode(buf.read()).decode("utf-8")\n return getattr(obj, print_method)()\n elif print_method == "_repr_mimebundle_":\n return {}, {}\n return None\n\n\ndef _format_mime(obj):\n """\n Formats object using _repr_x_ methods.\n """\n if isinstance(obj, str):\n return html.escape(obj), "text/plain"\n\n mimebundle = _eval_formatter(obj, "_repr_mimebundle_")\n if isinstance(mimebundle, tuple):\n format_dict, _ = mimebundle\n else:\n format_dict = mimebundle\n\n output, not_available = None, []\n for method, mime_type in reversed(_MIME_METHODS.items()):\n if mime_type in format_dict:\n output = format_dict[mime_type]\n else:\n output = _eval_formatter(obj, method)\n\n if output is None:\n continue\n elif mime_type not in _MIME_RENDERERS:\n not_available.append(mime_type)\n continue\n break\n if output is None:\n if not_available:\n window.console.warn(\n f"Rendered object requested unavailable MIME renderers: {not_available}"\n )\n output = repr(output)\n mime_type = "text/plain"\n elif isinstance(output, tuple):\n output, meta = output\n else:\n meta = {}\n return _MIME_RENDERERS[mime_type](output, meta), mime_type\n\n\ndef _write(element, value, append=False):\n html, mime_type = _format_mime(value)\n if html == "\\\\n":\n return\n\n if append:\n out_element = document.createElement("div")\n element.append(out_element)\n else:\n out_element = element.lastElementChild\n if out_element is None:\n out_element = element\n\n if mime_type in ("application/javascript", "text/html"):\n script_element = document.createRange().createContextualFragment(html)\n out_element.append(script_element)\n else:\n out_element.innerHTML = html\n\n\ndef display(*values, target=None, append=True):\n if target is None:\n target = current_target()\n elif not isinstance(target, str):\n raise TypeError(f"target must be str or None, not {target.__class__.__name__}")\n elif target == "":\n raise ValueError("Cannot have an empty target")\n elif target.startswith("#"):\n # note: here target is str and not None!\n # align with @when behavior\n target = target[1:]\n\n element = document.getElementById(target)\n\n # If target cannot be found on the page, a ValueError is raised\n if element is None:\n raise ValueError(\n f"Invalid selector with id={target}. Cannot be found in the page."\n )\n\n # if element is a <script type="py">, it has a \'target\' attribute which\n # points to the visual element holding the displayed values. In that case,\n # use that.\n if element.tagName == "SCRIPT" and hasattr(element, "target"):\n element = element.target\n\n for v in values:\n if not append:\n element.replaceChildren()\n _write(element, v, append=append)\n',"event_handling.py":'import inspect\n\nfrom pyodide.ffi.wrappers import add_event_listener\nfrom pyscript.magic_js import document\n\n\ndef when(event_type=None, selector=None):\n """\n Decorates a function and passes py-* events to the decorated function\n The events might or not be an argument of the decorated function\n """\n\n def decorator(func):\n if isinstance(selector, str):\n elements = document.querySelectorAll(selector)\n else:\n # TODO: This is a hack that will be removed when pyscript becomes a package\n # and we can better manage the imports without circular dependencies\n from pyweb import pydom\n\n if isinstance(selector, pydom.Element):\n elements = [selector._js]\n elif isinstance(selector, pydom.ElementCollection):\n elements = [el._js for el in selector]\n else:\n raise ValueError(\n f"Invalid selector: {selector}. Selector must"\n " be a string, a pydom.Element or a pydom.ElementCollection."\n )\n\n sig = inspect.signature(func)\n # Function doesn\'t receive events\n if not sig.parameters:\n\n def wrapper(*args, **kwargs):\n func()\n\n for el in elements:\n add_event_listener(el, event_type, wrapper)\n else:\n for el in elements:\n add_event_listener(el, event_type, func)\n return func\n\n return decorator\n',"magic_js.py":'import sys\nimport js as globalThis\nfrom polyscript import js_modules\nfrom pyscript.util import NotSupported\n\nRUNNING_IN_WORKER = not hasattr(globalThis, "document")\n\n# allow `from pyscript.js_modules import xxx`\nsys.modules["pyscript.js_modules"] = js_modules\n\nif RUNNING_IN_WORKER:\n import js\n import polyscript\n\n PyWorker = NotSupported(\n "pyscript.PyWorker",\n "pyscript.PyWorker works only when running in the main thread",\n )\n window = polyscript.xworker.window\n document = window.document\n js.document = document\n sync = polyscript.xworker.sync\n\n # in workers the display does not have a default ID\n # but there is a sync utility from xworker\n def current_target():\n return polyscript.target\n\nelse:\n import _pyscript\n from _pyscript import PyWorker\n\n window = globalThis\n document = globalThis.document\n sync = NotSupported(\n "pyscript.sync", "pyscript.sync works only when running in a worker"\n )\n\n # in MAIN the current element target exist, just use it\n def current_target():\n return _pyscript.target\n',"util.py":'class NotSupported:\n """\n Small helper that raises exceptions if you try to get/set any attribute on\n it.\n """\n\n def __init__(self, name, error):\n object.__setattr__(self, "name", name)\n object.__setattr__(self, "error", error)\n\n def __repr__(self):\n return f"<NotSupported {self.name} [{self.error}]>"\n\n def __getattr__(self, attr):\n raise AttributeError(self.error)\n\n def __setattr__(self, attr, value):\n raise AttributeError(self.error)\n\n def __call__(self, *args):\n raise TypeError(self.error)\n'},pyweb:{"pydom.py":'import sys\nimport warnings\nfrom functools import cached_property\nfrom typing import Any\n\nfrom pyodide.ffi import JsProxy\nfrom pyscript import display, document, window\n\nalert = window.alert\n\n\nclass BaseElement:\n def __init__(self, js_element):\n self._js = js_element\n self._parent = None\n self.style = StyleProxy(self)\n self._proxies = {}\n\n def __eq__(self, obj):\n """Check if the element is the same as the other element by comparing\n the underlying JS element"""\n return isinstance(obj, BaseElement) and obj._js == self._js\n\n @property\n def parent(self):\n if self._parent:\n return self._parent\n\n if self._js.parentElement:\n self._parent = self.__class__(self._js.parentElement)\n\n return self._parent\n\n @property\n def __class(self):\n return self.__class__ if self.__class__ != PyDom else Element\n\n def create(self, type_, is_child=True, classes=None, html=None, label=None):\n js_el = document.createElement(type_)\n element = self.__class(js_el)\n\n if classes:\n for class_ in classes:\n element.add_class(class_)\n\n if html is not None:\n element.html = html\n\n if label is not None:\n element.label = label\n\n if is_child:\n self.append(element)\n\n return element\n\n def find(self, selector):\n """Return an ElementCollection representing all the child elements that\n match the specified selector.\n\n Args:\n selector (str): A string containing a selector expression\n\n Returns:\n ElementCollection: A collection of elements matching the selector\n """\n elements = self._js.querySelectorAll(selector)\n if not elements:\n return None\n return ElementCollection([Element(el) for el in elements])\n\n\nclass Element(BaseElement):\n @property\n def children(self):\n return [self.__class__(el) for el in self._js.children]\n\n def append(self, child):\n # TODO: this is Pyodide specific for now!!!!!!\n # if we get passed a JSProxy Element directly we just map it to the\n # higher level Python element\n if isinstance(child, JsProxy):\n return self.append(Element(child))\n\n elif isinstance(child, Element):\n self._js.appendChild(child._js)\n\n return child\n\n elif isinstance(child, ElementCollection):\n for el in child:\n self.append(el)\n\n # -------- Pythonic Interface to Element -------- #\n @property\n def html(self):\n return self._js.innerHTML\n\n @html.setter\n def html(self, value):\n self._js.innerHTML = value\n\n @property\n def content(self):\n # TODO: This breaks with with standard template elements. Define how to best\n # handle this specifica use case. Just not support for now?\n if self._js.tagName == "TEMPLATE":\n warnings.warn(\n "Content attribute not supported for template elements.", stacklevel=2\n )\n return None\n return self._js.innerHTML\n\n @content.setter\n def content(self, value):\n # TODO: (same comment as above)\n if self._js.tagName == "TEMPLATE":\n warnings.warn(\n "Content attribute not supported for template elements.", stacklevel=2\n )\n return\n\n display(value, target=self.id)\n\n @property\n def id(self):\n return self._js.id\n\n @id.setter\n def id(self, value):\n self._js.id = value\n\n @property\n def options(self):\n if "options" in self._proxies:\n return self._proxies["options"]\n\n if not self._js.tagName.lower() in {"select", "datalist", "optgroup"}:\n raise AttributeError(\n f"Element {self._js.tagName} has no options attribute."\n )\n self._proxies["options"] = OptionsProxy(self)\n return self._proxies["options"]\n\n @property\n def value(self):\n return self._js.value\n\n @value.setter\n def value(self, value):\n # in order to avoid confusion to the user, we don\'t allow setting the\n # value of elements that don\'t have a value attribute\n if not hasattr(self._js, "value"):\n raise AttributeError(\n f"Element {self._js.tagName} has no value attribute. If you want to "\n "force a value attribute, set it directly using the `_js.value = <value>` "\n "javascript API attribute instead."\n )\n self._js.value = value\n\n @property\n def selected(self):\n return self._js.selected\n\n @selected.setter\n def selected(self, value):\n # in order to avoid confusion to the user, we don\'t allow setting the\n # value of elements that don\'t have a value attribute\n if not hasattr(self._js, "selected"):\n raise AttributeError(\n f"Element {self._js.tagName} has no value attribute. If you want to "\n "force a value attribute, set it directly using the `_js.value = <value>` "\n "javascript API attribute instead."\n )\n self._js.selected = value\n\n def clone(self, new_id=None):\n clone = Element(self._js.cloneNode(True))\n clone.id = new_id\n\n return clone\n\n def remove_class(self, classname):\n classList = self._js.classList\n if isinstance(classname, list):\n classList.remove(*classname)\n else:\n classList.remove(classname)\n return self\n\n def add_class(self, classname):\n classList = self._js.classList\n if isinstance(classname, list):\n classList.add(*classname)\n else:\n self._js.classList.add(classname)\n return self\n\n @property\n def classes(self):\n classes = self._js.classList.values()\n return [x for x in classes]\n\n def show_me(self):\n self._js.scrollIntoView()\n\n\nclass OptionsProxy:\n """This class represents the options of a select element. It\n allows to access to add and remove options by using the `add` and `remove` methods.\n """\n\n def __init__(self, element: Element) -> None:\n self._element = element\n if self._element._js.tagName.lower() != "select":\n raise AttributeError(\n f"Element {self._element._js.tagName} has no options attribute."\n )\n\n def add(\n self,\n value: Any = None,\n html: str = None,\n text: str = None,\n before: Element | int = None,\n **kws,\n ) -> None:\n """Add a new option to the select element"""\n # create the option element and set the attributes\n option = document.createElement("option")\n if value is not None:\n kws["value"] = value\n if html is not None:\n option.innerHTML = html\n if text is not None:\n kws["text"] = text\n\n for key, value in kws.items():\n option.setAttribute(key, value)\n\n if before:\n if isinstance(before, Element):\n before = before._js\n\n self._element._js.add(option, before)\n\n def remove(self, item: int) -> None:\n """Remove the option at the specified index"""\n self._element._js.remove(item)\n\n def clear(self) -> None:\n """Remove all the options"""\n for i in range(len(self)):\n self.remove(0)\n\n @property\n def options(self):\n """Return the list of options"""\n return [Element(opt) for opt in self._element._js.options]\n\n @property\n def selected(self):\n """Return the selected option"""\n return self.options[self._element._js.selectedIndex]\n\n def __iter__(self):\n yield from self.options\n\n def __len__(self):\n return len(self.options)\n\n def __repr__(self):\n return f"{self.__class__.__name__} (length: {len(self)}) {self.options}"\n\n def __getitem__(self, key):\n return self.options[key]\n\n\nclass StyleProxy(dict):\n def __init__(self, element: Element) -> None:\n self._element = element\n\n @cached_property\n def _style(self):\n return self._element._js.style\n\n def __getitem__(self, key):\n return self._style.getPropertyValue(key)\n\n def __setitem__(self, key, value):\n self._style.setProperty(key, value)\n\n def remove(self, key):\n self._style.removeProperty(key)\n\n def set(self, **kws):\n for k, v in kws.items():\n self._element._js.style.setProperty(k, v)\n\n # CSS Properties\n # Reference: https://github.com/microsoft/TypeScript/blob/main/src/lib/dom.generated.d.ts#L3799C1-L5005C2\n # Following prperties automatically generated from the above reference using\n # tools/codegen_css_proxy.py\n @property\n def visible(self):\n return self._element._js.style.visibility\n\n @visible.setter\n def visible(self, value):\n self._element._js.style.visibility = value\n\n\nclass StyleCollection:\n def __init__(self, collection: "ElementCollection") -> None:\n self._collection = collection\n\n def __get__(self, obj, objtype=None):\n return obj._get_attribute("style")\n\n def __getitem__(self, key):\n return self._collection._get_attribute("style")[key]\n\n def __setitem__(self, key, value):\n for element in self._collection._elements:\n element.style[key] = value\n\n def remove(self, key):\n for element in self._collection._elements:\n element.style.remove(key)\n\n\nclass ElementCollection:\n def __init__(self, elements: [Element]) -> None:\n self._elements = elements\n self.style = StyleCollection(self)\n\n def __getitem__(self, key):\n # If it\'s an integer we use it to access the elements in the collection\n if isinstance(key, int):\n return self._elements[key]\n # If it\'s a slice we use it to support slice operations over the elements\n # in the collection\n elif isinstance(key, slice):\n return ElementCollection(self._elements[key])\n\n # If it\'s anything else (basically a string) we use it as a selector\n # TODO: Write tests!\n elements = self._element.querySelectorAll(key)\n return ElementCollection([Element(el) for el in elements])\n\n def __len__(self):\n return len(self._elements)\n\n def __eq__(self, obj):\n """Check if the element is the same as the other element by comparing\n the underlying JS element"""\n return isinstance(obj, ElementCollection) and obj._elements == self._elements\n\n def _get_attribute(self, attr, index=None):\n if index is None:\n return [getattr(el, attr) for el in self._elements]\n\n # As JQuery, when getting an attr, only return it for the first element\n return getattr(self._elements[index], attr)\n\n def _set_attribute(self, attr, value):\n for el in self._elements:\n setattr(el, attr, value)\n\n @property\n def html(self):\n return self._get_attribute("html")\n\n @html.setter\n def html(self, value):\n self._set_attribute("html", value)\n\n @property\n def value(self):\n return self._get_attribute("value")\n\n @value.setter\n def value(self, value):\n self._set_attribute("value", value)\n\n @property\n def children(self):\n return self._elements\n\n def __iter__(self):\n yield from self._elements\n\n def __repr__(self):\n return f"{self.__class__.__name__} (length: {len(self._elements)}) {self._elements}"\n\n\nclass DomScope:\n def __getattr__(self, __name: str) -> Any:\n element = document[f"#{__name}"]\n if element:\n return element[0]\n\n\nclass PyDom(BaseElement):\n # Add objects we want to expose to the DOM namespace since this class instance is being\n # remapped as "the module" itself\n BaseElement = BaseElement\n Element = Element\n ElementCollection = ElementCollection\n\n def __init__(self):\n super().__init__(document)\n self.ids = DomScope()\n self.body = Element(document.body)\n self.head = Element(document.head)\n\n def create(self, type_, classes=None, html=None):\n return super().create(type_, is_child=False, classes=classes, html=html)\n\n def __getitem__(self, key):\n if isinstance(key, int):\n indices = range(*key.indices(len(self.list)))\n return [self.list[i] for i in indices]\n\n elements = self._js.querySelectorAll(key)\n if not elements:\n return None\n return ElementCollection([Element(el) for el in elements])\n\n\ndom = PyDom()\n\nsys.modules[__name__] = dom\n'}}),tr.push("import pyscript as _pyscript"),tr.push(...["_Path","_path","_os","_pyscript"].map((e=>`del ${e}`))),tr.push("\n");var rr=tr.join("\n");const sr=e=>ur.main[e],or=e=>ur.worker[e],ar=(e,t,n,r)=>{e[n]=()=>{const e=r?[r]:[];return e.push(...t(n)),e.map(Ue).join("\n")}},ir=e=>{const t={};return ar(t,e,"codeBeforeRun",rr),ar(t,e,"codeBeforeRunAsync",rr),ar(t,e,"codeAfterRun"),ar(t,e,"codeAfterRunAsync"),t},lr=(e,t)=>{const n=[...or(t)];if(n.length){const r=Qn(e[`_${t}`]||(t.endsWith("Async")?async(e,t,...n)=>{for(const r of n)await r(e,t)}:(e,t,...n)=>{for(const r of n)r(e,t)})),s=n.map(Qn).join(", ");return Function(`return(w,x)=>(${r})(w,x,...[${s}])`)()}},cr=Zn({typeof:"function"}),pr=Zn({typeof:"string"}),ur={main:{onWorker:new cr,onReady:new cr,onBeforeRun:new cr,onBeforeRunAsync:new cr,onAfterRun:new cr,onAfterRunAsync:new cr,codeBeforeRun:new pr(['\n import builtins\n def input(prompt=""):\n raise Exception("\\n ".join([\n "input() doesn\'t work when PyScript runs in the main thread.",\n "Consider using the worker attribute: https://pyscript.github.io/docs/2023.11.2/user-guide/workers/"\n ]))\n\n builtins.input = input\n del builtins\n del input\n']),codeBeforeRunAsync:new pr,codeAfterRun:new pr,codeAfterRunAsync:new pr},worker:{onReady:new cr,onBeforeRun:new cr,onBeforeRunAsync:new cr,onAfterRun:new cr,onAfterRunAsync:new cr,codeBeforeRun:new pr,codeBeforeRunAsync:new pr,codeAfterRun:new pr,codeAfterRunAsync:new pr}},fr=({tagName:e})=>"SCRIPT"===e,[{PyWorker:dr,hooks:hr,config:mr,whenDefined:yr},_r]=e("@pyscript/core",{PyWorker:function(e,t){const n=gr.get("py"),r=Sn.call(new xn(null,n),e,{type:"pyodide",...t});return Ge(r.sync,Jn),r},hooks:ur,config:{},whenDefined:An}),gr=new Map;for(const[e,t]of On){if(_r)break;const n=(t,n,r)=>{n?r.then((()=>nt(t,e,"done"))):nt(t,e,"done")},{config:s,plugins:o,error:a}=Un.get(e);let i=0;const l=(t=e)=>`${t}-${i++}`,c=async(t,n,s)=>{if(t.hasAttribute("src"))try{return await Cn(t.getAttribute("src")).then(r)}catch(e){n.stderr(e)}if(s)return Ue(t.textContent);const o=Ue(Je(t.innerHTML));return console.warn(`Deprecated: use <script type="${e}"> for an always safe content parsing:\n`,o),o};let p,u=!1;const f=({XWorker:e,interpreter:t,io:n})=>{u||(u=!0,t.registerJsModule("_pyscript",{PyWorker:function(...t){const r=e(...t);return r.onerror=({error:e})=>n.stderr(e),r},get target(){return fr(p)?p.target.id:p.id}}))};a||o.then((()=>{const r=new Map,o={main:{...ir(sr),async onReady(t,s){f(t);for(const e of sr("onReady"))await e(t,s);if(r.has(s)){let{message:e}=r.get(s);r.delete(s);const n=e===nn;return e=`(${$n.CONFLICTING_CODE}) ${e} for `,e+=s.cloneNode(n).outerHTML,void t.io.stderr(e)}if(fr(s)){const{attributes:{async:r,target:o}}=s,a=!!o?.value,i=a?sn(s,o.value):document.createElement("script-py");if(!a){const{head:e,body:t}=document;e.contains(s)?t.append(i):s.after(i)}i.id||(i.id=l()),Xe(s,"target",{value:i}),nt(s,e,"ready"),n(s,r,t["run"+(r?"Async":"")](await c(s,t.io,!0)))}else s._wrap.resolve(t);console.debug("[pyscript/main] PyScript Ready")},onWorker(e,t){Ge(t.sync,Jn);for(const n of sr("onWorker"))n(e,t)},onBeforeRun(e,t){p=t,Yn(sr,e,t,"onBeforeRun")},onBeforeRunAsync:(e,t)=>(p=t,Yn(sr,e,t,"onBeforeRunAsync")),onAfterRun(e,t){Yn(sr,e,t,"onAfterRun")},onAfterRunAsync:(e,t)=>Yn(sr,e,t,"onAfterRunAsync")},worker:{...ir(or),get onReady(){return lr(this,"onReady")},get onBeforeRun(){return lr(this,"onBeforeRun")},get onBeforeRunAsync(){return lr(this,"onBeforeRunAsync")},get onAfterRun(){return lr(this,"onAfterRun")},get onAfterRunAsync(){return lr(this,"onAfterRunAsync")}}};gr.set(e,o),Rn(e,{config:s,interpreter:t,hooks:o,env:`${e}-script`,version:s?.interpreter,onerror(e,t){r.set(t,e)}}),customElements.define(`${e}-script`,class extends HTMLElement{constructor(){Ge(super(),{_wrap:Promise.withResolvers(),srcCode:"",executed:!1})}get id(){return super.id||(super.id=l())}set id(e){super.id=e}async connectedCallback(){if(!this.executed){this.executed=!0;const t=this.hasAttribute("async"),{io:r,run:s,runAsync:o}=await this._wrap.promise;this.srcCode=await c(this,r,!this.childElementCount),this.replaceChildren(),this.style.display="block",nt(this,e,"ready"),n(this,t,(t?o:s)(this.srcCode))}}})})),mr[e]=structuredClone(s)}export{xn as H,On as T,Sn as X,dr as a,mr as b,yr as c,Ue as d,hr as e};
|
3
|
-
//# sourceMappingURL=core-P7cR7OAZ.js.map
|