@pyscript/core 0.4.51 → 0.4.52

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.
Files changed (42) hide show
  1. package/dist/{codemirror-kHGMDdMq.js → codemirror-BZcZV62h.js} +2 -2
  2. package/dist/codemirror-BZcZV62h.js.map +1 -0
  3. package/dist/{codemirror_lang-python-PBvh5MWJ.js → codemirror_lang-python-BPBYxtKZ.js} +2 -2
  4. package/dist/{codemirror_lang-python-PBvh5MWJ.js.map → codemirror_lang-python-BPBYxtKZ.js.map} +1 -1
  5. package/dist/core-2JDOwv1q.js +3 -0
  6. package/dist/core-2JDOwv1q.js.map +1 -0
  7. package/dist/core.js +1 -1
  8. package/dist/{deprecations-manager-CQv1VsU_.js → deprecations-manager-DIqpiVXg.js} +2 -2
  9. package/dist/{deprecations-manager-CQv1VsU_.js.map → deprecations-manager-DIqpiVXg.js.map} +1 -1
  10. package/dist/{error-DueleOMN.js → error-BGZL6Fui.js} +2 -2
  11. package/dist/{error-DueleOMN.js.map → error-BGZL6Fui.js.map} +1 -1
  12. package/dist/index-C2QxsDsk.js +2 -0
  13. package/dist/index-C2QxsDsk.js.map +1 -0
  14. package/dist/{mpy-DOTPCj0o.js → mpy-B0zAxV1g.js} +2 -2
  15. package/dist/{mpy-DOTPCj0o.js.map → mpy-B0zAxV1g.js.map} +1 -1
  16. package/dist/{py-DoxIVXsQ.js → py-C0ESszMr.js} +2 -2
  17. package/dist/{py-DoxIVXsQ.js.map → py-C0ESszMr.js.map} +1 -1
  18. package/dist/py-editor-PTkeu-Zz.js +2 -0
  19. package/dist/py-editor-PTkeu-Zz.js.map +1 -0
  20. package/dist/py-terminal-Q9SMt9_A.js +2 -0
  21. package/dist/py-terminal-Q9SMt9_A.js.map +1 -0
  22. package/package.json +2 -2
  23. package/src/config.js +4 -2
  24. package/src/core.js +3 -2
  25. package/src/plugins/py-editor.js +15 -9
  26. package/src/plugins/py-terminal.js +2 -2
  27. package/src/stdlib/pyscript/__init__.py +0 -3
  28. package/src/stdlib/pyscript.js +2 -3
  29. package/types/config.d.ts +2 -2
  30. package/types/core.d.ts +2 -1
  31. package/types/stdlib/pyscript.d.ts +0 -1
  32. package/dist/codemirror-kHGMDdMq.js.map +0 -1
  33. package/dist/core-BQAbLvre.js +0 -3
  34. package/dist/core-BQAbLvre.js.map +0 -1
  35. package/dist/index-lTKU5_hG.js +0 -2
  36. package/dist/index-lTKU5_hG.js.map +0 -1
  37. package/dist/py-editor-D3FDoYNU.js +0 -2
  38. package/dist/py-editor-D3FDoYNU.js.map +0 -1
  39. package/dist/py-terminal-DMP3XcAx.js +0 -2
  40. package/dist/py-terminal-DMP3XcAx.js.map +0 -1
  41. package/dist.zip +0 -0
  42. package/src/stdlib/pyscript/workers.py +0 -15
@@ -1,2 +1,2 @@
1
- import{e,d as t}from"./core-BQAbLvre.js";const r=new WeakSet,n=({interpreter:e,io:t,run:r,type:n},{sync:i})=>{if("py"!==n||!i.is_pyterminal())return;r(["from polyscript import currentScript as _","__terminal__ = _.terminal","del _"].join(";"));let o="";const{pyterminal_read:a,pyterminal_write:d}=i,s=new TextDecoder,l={isatty:!1,write:e=>(o=s.decode(e),d(o),e.length)};t.stderr=e=>{d(String(e.message||e))},e.setStdout(l),e.setStderr(l),e.setStdin({isatty:!1,stdin:()=>a(o)})};var i=async i=>{const[{Terminal:o},{Readline:a},{FitAddon:d},{WebLinksAddon:s}]=await Promise.all([import("./xterm-BY7uk_OU.js"),import("./xterm-readline-DjufFwfn.js"),import("./xterm_addon-fit--gyF3PcZ.js"),import("./xterm_addon-web-links-Cnej-nJ6.js")]),l=new a,m=e=>{let r=i;const n=i.getAttribute("target");if(n){if(r=document.getElementById(n)||document.querySelector(n),!r)throw new Error(`Unknown target ${n}`)}else r=document.createElement("py-terminal"),r.style.display="block",i.after(r);const a=new o({theme:{background:"#191A19",foreground:"#F5F2E7"},...e}),m=new d;return a.loadAddon(m),a.loadAddon(l),a.loadAddon(new s),a.open(r),m.fit(),a.focus(),t(i,{terminal:{value:a},process:{value:async e=>{for(const t of e.split(/(?:\r\n|\r|\n)/)){a.paste(`${t}`),a.write("\r\n");do{await new Promise((e=>setTimeout(e,0)))}while(!l.activeRead?.resolve);l.activeRead.resolve(t)}}}}),a};i.hasAttribute("worker")?(e.main.onWorker.add((function t(n,i){r.has(i)||(r.add(i),e.main.onWorker.delete(t),m({disableStdin:!1,cursorBlink:!0,cursorStyle:"block"}),i.sync.is_pyterminal=()=>!0,i.sync.pyterminal_read=l.read.bind(l),i.sync.pyterminal_write=l.write.bind(l))})),e.worker.onReady.add(n)):e.main.onReady.add((function t({interpreter:r,io:n,run:i,type:o}){if("py"!==o)return;console.warn("py-terminal is read only on main thread"),e.main.onReady.delete(t),globalThis.__py_terminal__=m({disableStdin:!0,cursorBlink:!1,cursorStyle:"underline"}),i("from js import __py_terminal__ as __terminal__"),delete globalThis.__py_terminal__,n.stderr=e=>{l.write(String(e.message||e))};let a="";const d=new TextDecoder,s={isatty:!1,write:e=>(a=d.decode(e),l.write(a),e.length)};r.setStdout(s),r.setStderr(s),r.setStdin({isatty:!1,stdin:()=>l.read(a)})}))};export{i as default};
2
- //# sourceMappingURL=py-DoxIVXsQ.js.map
1
+ import{e,d as t}from"./core-2JDOwv1q.js";const r=new WeakSet,n=({interpreter:e,io:t,run:r,type:n},{sync:i})=>{if("py"!==n||!i.is_pyterminal())return;r(["from polyscript import currentScript as _","__terminal__ = _.terminal","del _"].join(";"));let o="";const{pyterminal_read:a,pyterminal_write:d}=i,s=new TextDecoder,l={isatty:!1,write:e=>(o=s.decode(e),d(o),e.length)};t.stderr=e=>{d(String(e.message||e))},e.setStdout(l),e.setStderr(l),e.setStdin({isatty:!1,stdin:()=>a(o)})};var i=async i=>{const[{Terminal:o},{Readline:a},{FitAddon:d},{WebLinksAddon:s}]=await Promise.all([import("./xterm-BY7uk_OU.js"),import("./xterm-readline-DjufFwfn.js"),import("./xterm_addon-fit--gyF3PcZ.js"),import("./xterm_addon-web-links-Cnej-nJ6.js")]),l=new a,m=e=>{let r=i;const n=i.getAttribute("target");if(n){if(r=document.getElementById(n)||document.querySelector(n),!r)throw new Error(`Unknown target ${n}`)}else r=document.createElement("py-terminal"),r.style.display="block",i.after(r);const a=new o({theme:{background:"#191A19",foreground:"#F5F2E7"},...e}),m=new d;return a.loadAddon(m),a.loadAddon(l),a.loadAddon(new s),a.open(r),m.fit(),a.focus(),t(i,{terminal:{value:a},process:{value:async e=>{for(const t of e.split(/(?:\r\n|\r|\n)/)){a.paste(`${t}`),a.write("\r\n");do{await new Promise((e=>setTimeout(e,0)))}while(!l.activeRead?.resolve);l.activeRead.resolve(t)}}}}),a};i.hasAttribute("worker")?(e.main.onWorker.add((function t(n,i){r.has(i)||(r.add(i),e.main.onWorker.delete(t),m({disableStdin:!1,cursorBlink:!0,cursorStyle:"block"}),i.sync.is_pyterminal=()=>!0,i.sync.pyterminal_read=l.read.bind(l),i.sync.pyterminal_write=l.write.bind(l))})),e.worker.onReady.add(n)):e.main.onReady.add((function t({interpreter:r,io:n,run:i,type:o}){if("py"!==o)return;console.warn("py-terminal is read only on main thread"),e.main.onReady.delete(t),globalThis.__py_terminal__=m({disableStdin:!0,cursorBlink:!1,cursorStyle:"underline"}),i("from js import __py_terminal__ as __terminal__"),delete globalThis.__py_terminal__,n.stderr=e=>{l.write(String(e.message||e))};let a="";const d=new TextDecoder,s={isatty:!1,write:e=>(a=d.decode(e),l.write(a),e.length)};r.setStdout(s),r.setStderr(s),r.setStdin({isatty:!1,stdin:()=>l.read(a)})}))};export{i as default};
2
+ //# sourceMappingURL=py-C0ESszMr.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"py-DoxIVXsQ.js","sources":["../src/plugins/py-terminal/py.js"],"sourcesContent":["// PyScript py-terminal plugin\nimport { hooks } from \"../../core.js\";\nimport { defineProperties } from \"polyscript/exports\";\n\nconst bootstrapped = new WeakSet();\n\n// this callback will be serialized as string and it never needs\n// to be invoked multiple times. Each xworker here is bootstrapped\n// only once thanks to the `sync.is_pyterminal()` check.\nconst workerReady = ({ interpreter, io, run, type }, { sync }) => {\n if (type !== \"py\" || !sync.is_pyterminal()) return;\n\n run(\n [\n \"from polyscript import currentScript as _\",\n \"__terminal__ = _.terminal\",\n \"del _\",\n ].join(\";\"),\n );\n\n let data = \"\";\n const { pyterminal_read, pyterminal_write } = sync;\n const decoder = new TextDecoder();\n const generic = {\n isatty: false,\n write(buffer) {\n data = decoder.decode(buffer);\n pyterminal_write(data);\n return buffer.length;\n },\n };\n\n io.stderr = (error) => {\n pyterminal_write(String(error.message || error));\n };\n\n interpreter.setStdout(generic);\n interpreter.setStderr(generic);\n interpreter.setStdin({\n isatty: false,\n stdin: () => pyterminal_read(data),\n });\n};\n\nexport default async (element) => {\n // lazy load these only when a valid terminal is found\n const [{ Terminal }, { Readline }, { FitAddon }, { WebLinksAddon }] =\n await Promise.all([\n import(/* webpackIgnore: true */ \"../../3rd-party/xterm.js\"),\n import(\n /* webpackIgnore: true */ \"../../3rd-party/xterm-readline.js\"\n ),\n import(\n /* webpackIgnore: true */ \"../../3rd-party/xterm_addon-fit.js\"\n ),\n import(\n /* webpackIgnore: true */ \"../../3rd-party/xterm_addon-web-links.js\"\n ),\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.loadAddon(new WebLinksAddon());\n terminal.open(target);\n fitAddon.fit();\n terminal.focus();\n defineProperties(element, {\n terminal: { value: terminal },\n process: {\n value: async (code) => {\n for (const line of code.split(/(?:\\r\\n|\\r|\\n)/)) {\n terminal.paste(`${line}`);\n terminal.write(\"\\r\\n\");\n do {\n await new Promise((resolve) =>\n setTimeout(resolve, 0),\n );\n } while (!readline.activeRead?.resolve);\n readline.activeRead.resolve(line);\n }\n },\n },\n });\n return terminal;\n };\n\n // branch logic for the worker\n if (element.hasAttribute(\"worker\")) {\n // add a hook on the main thread to setup all sync helpers\n // also bootstrapping the XTerm target on main *BUT* ...\n hooks.main.onWorker.add(function worker(_, xworker) {\n // ... as multiple workers will add multiple callbacks\n // be sure no xworker is ever initialized twice!\n if (bootstrapped.has(xworker)) return;\n bootstrapped.add(xworker);\n\n // still cleanup this callback for future scripts/workers\n hooks.main.onWorker.delete(worker);\n\n init({\n disableStdin: false,\n cursorBlink: true,\n cursorStyle: \"block\",\n });\n\n xworker.sync.is_pyterminal = () => true;\n xworker.sync.pyterminal_read = readline.read.bind(readline);\n xworker.sync.pyterminal_write = readline.write.bind(readline);\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({ interpreter, io, run, type }) {\n if (type !== \"py\") return;\n\n console.warn(\"py-terminal is read only on main thread\");\n hooks.main.onReady.delete(main);\n\n // on main, it's easy to trash and clean the current terminal\n globalThis.__py_terminal__ = init({\n disableStdin: true,\n cursorBlink: false,\n cursorStyle: \"underline\",\n });\n run(\"from js import __py_terminal__ as __terminal__\");\n delete globalThis.__py_terminal__;\n\n io.stderr = (error) => {\n readline.write(String(error.message || error));\n };\n\n let data = \"\";\n const decoder = new TextDecoder();\n const generic = {\n isatty: false,\n write(buffer) {\n data = decoder.decode(buffer);\n readline.write(data);\n return buffer.length;\n },\n };\n interpreter.setStdout(generic);\n interpreter.setStderr(generic);\n interpreter.setStdin({\n isatty: false,\n stdin: () => readline.read(data),\n });\n });\n }\n};\n"],"names":["bootstrapped","WeakSet","workerReady","interpreter","io","run","type","sync","is_pyterminal","join","data","pyterminal_read","pyterminal_write","decoder","TextDecoder","generic","isatty","write","buffer","decode","length","stderr","error","String","message","setStdout","setStderr","setStdin","stdin","py","async","element","Terminal","Readline","FitAddon","WebLinksAddon","Promise","all","import","readline","init","options","target","selector","getAttribute","document","getElementById","querySelector","Error","createElement","style","display","after","terminal","theme","background","foreground","fitAddon","loadAddon","open","fit","focus","defineProperties","value","process","code","line","split","paste","resolve","setTimeout","activeRead","hasAttribute","hooks","main","onWorker","add","worker","_","xworker","has","delete","disableStdin","cursorBlink","cursorStyle","read","bind","onReady","console","warn","globalThis","__py_terminal__"],"mappings":"yCAIA,MAAMA,EAAe,IAAIC,QAKnBC,EAAc,EAAGC,cAAaC,KAAIC,MAAKC,SAAUC,WACnD,GAAa,OAATD,IAAkBC,EAAKC,gBAAiB,OAE5CH,EACI,CACI,4CACA,4BACA,SACFI,KAAK,MAGX,IAAIC,EAAO,GACX,MAAMC,gBAAEA,EAAeC,iBAAEA,GAAqBL,EACxCM,EAAU,IAAIC,YACdC,EAAU,CACZC,QAAQ,EACRC,MAAMC,IACFR,EAAOG,EAAQM,OAAOD,GACtBN,EAAiBF,GACVQ,EAAOE,SAItBhB,EAAGiB,OAAUC,IACTV,EAAiBW,OAAOD,EAAME,SAAWF,GAAO,EAGpDnB,EAAYsB,UAAUV,GACtBZ,EAAYuB,UAAUX,GACtBZ,EAAYwB,SAAS,CACjBX,QAAQ,EACRY,MAAO,IAAMjB,EAAgBD,IAC/B,EAGN,IAAemB,EAAAC,MAAOC,IAElB,OAAOC,SAAEA,IAAYC,SAAEA,IAAYC,SAAEA,IAAYC,cAAEA,UACzCC,QAAQC,IAAI,CACdC,OAAiC,uBACjCA,OAC8B,gCAE9BA,OAC8B,iCAE9BA,OAC8B,yCAIhCC,EAAW,IAAIN,EAIfO,EAAQC,IACV,IAAIC,EAASX,EACb,MAAMY,EAAWZ,EAAQa,aAAa,UACt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A,IACzD,GAAa,OAATA,EAAe,OAEnBkF,QAAQC,KAAK,2CACbhB,EAAMC,KAAKa,QAAQN,OAAOP,GAG1BgB,WAAWC,gBAAkBnD,EAAK,CAC9B0C,cAAc,EACdC,aAAa,EACbC,YAAa,cAEjB/E,EAAI,yDACGqF,WAAWC,gBAElBvF,EAAGiB,OAAUC,IACTiB,EAAStB,MAAMM,OAAOD,EAAME,SAAWF,GAAO,EAGlD,IAAIZ,EAAO,GACX,MAAMG,EAAU,IAAIC,YACdC,EAAU,CACZC,QAAQ,EACRC,MAAMC,IACFR,EAAOG,EAAQM,OAAOD,GACtBqB,EAAStB,MAAMP,GACRQ,EAAOE,SAGtBjB,EAAYsB,UAAUV,GACtBZ,EAAYuB,UAAUX,GACtBZ,EAAYwB,SAAS,CACjBX,QAAQ,EACRY,MAAO,IAAMW,EAAS8C,KAAK3E,IAE3C,GACK"}
1
+ {"version":3,"file":"py-C0ESszMr.js","sources":["../src/plugins/py-terminal/py.js"],"sourcesContent":["// PyScript py-terminal plugin\nimport { hooks } from \"../../core.js\";\nimport { defineProperties } from \"polyscript/exports\";\n\nconst bootstrapped = new WeakSet();\n\n// this callback will be serialized as string and it never needs\n// to be invoked multiple times. Each xworker here is bootstrapped\n// only once thanks to the `sync.is_pyterminal()` check.\nconst workerReady = ({ interpreter, io, run, type }, { sync }) => {\n if (type !== \"py\" || !sync.is_pyterminal()) return;\n\n run(\n [\n \"from polyscript import currentScript as _\",\n \"__terminal__ = _.terminal\",\n \"del _\",\n ].join(\";\"),\n );\n\n let data = \"\";\n const { pyterminal_read, pyterminal_write } = sync;\n const decoder = new TextDecoder();\n const generic = {\n isatty: false,\n write(buffer) {\n data = decoder.decode(buffer);\n pyterminal_write(data);\n return buffer.length;\n },\n };\n\n io.stderr = (error) => {\n pyterminal_write(String(error.message || error));\n };\n\n interpreter.setStdout(generic);\n interpreter.setStderr(generic);\n interpreter.setStdin({\n isatty: false,\n stdin: () => pyterminal_read(data),\n });\n};\n\nexport default async (element) => {\n // lazy load these only when a valid terminal is found\n const [{ Terminal }, { Readline }, { FitAddon }, { WebLinksAddon }] =\n await Promise.all([\n import(/* webpackIgnore: true */ \"../../3rd-party/xterm.js\"),\n import(\n /* webpackIgnore: true */ \"../../3rd-party/xterm-readline.js\"\n ),\n import(\n /* webpackIgnore: true */ \"../../3rd-party/xterm_addon-fit.js\"\n ),\n import(\n /* webpackIgnore: true */ \"../../3rd-party/xterm_addon-web-links.js\"\n ),\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.loadAddon(new WebLinksAddon());\n terminal.open(target);\n fitAddon.fit();\n terminal.focus();\n defineProperties(element, {\n terminal: { value: terminal },\n process: {\n value: async (code) => {\n for (const line of code.split(/(?:\\r\\n|\\r|\\n)/)) {\n terminal.paste(`${line}`);\n terminal.write(\"\\r\\n\");\n do {\n await new Promise((resolve) =>\n setTimeout(resolve, 0),\n );\n } while (!readline.activeRead?.resolve);\n readline.activeRead.resolve(line);\n }\n },\n },\n });\n return terminal;\n };\n\n // branch logic for the worker\n if (element.hasAttribute(\"worker\")) {\n // add a hook on the main thread to setup all sync helpers\n // also bootstrapping the XTerm target on main *BUT* ...\n hooks.main.onWorker.add(function worker(_, xworker) {\n // ... as multiple workers will add multiple callbacks\n // be sure no xworker is ever initialized twice!\n if (bootstrapped.has(xworker)) return;\n bootstrapped.add(xworker);\n\n // still cleanup this callback for future scripts/workers\n hooks.main.onWorker.delete(worker);\n\n init({\n disableStdin: false,\n cursorBlink: true,\n cursorStyle: \"block\",\n });\n\n xworker.sync.is_pyterminal = () => true;\n xworker.sync.pyterminal_read = readline.read.bind(readline);\n xworker.sync.pyterminal_write = readline.write.bind(readline);\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({ interpreter, io, run, type }) {\n if (type !== \"py\") return;\n\n console.warn(\"py-terminal is read only on main thread\");\n hooks.main.onReady.delete(main);\n\n // on main, it's easy to trash and clean the current terminal\n globalThis.__py_terminal__ = init({\n disableStdin: true,\n cursorBlink: false,\n cursorStyle: \"underline\",\n });\n run(\"from js import __py_terminal__ as __terminal__\");\n delete globalThis.__py_terminal__;\n\n io.stderr = (error) => {\n readline.write(String(error.message || error));\n };\n\n let data = \"\";\n const decoder = new TextDecoder();\n const generic = {\n isatty: false,\n write(buffer) {\n data = decoder.decode(buffer);\n readline.write(data);\n return buffer.length;\n },\n };\n interpreter.setStdout(generic);\n interpreter.setStderr(generic);\n interpreter.setStdin({\n isatty: false,\n stdin: () => readline.read(data),\n });\n });\n }\n};\n"],"names":["bootstrapped","WeakSet","workerReady","interpreter","io","run","type","sync","is_pyterminal","join","data","pyterminal_read","pyterminal_write","decoder","TextDecoder","generic","isatty","write","buffer","decode","length","stderr","error","String","message","setStdout","setStderr","setStdin","stdin","py","async","element","Terminal","Readline","FitAddon","WebLinksAddon","Promise","all","import","readline","init","options","target","selector","getAttribute","document","getElementById","querySelector","Error","createElement","style","display","after","terminal","theme","background","foreground","fitAddon","loadAddon","open","fit","focus","defineProperties","value","process","code","line","split","paste","resolve","setTimeout","activeRead","hasAttribute","hooks","main","onWorker","add","worker","_","xworker","has","delete","disableStdin","cursorBlink","cursorStyle","read","bind","onReady","console","warn","globalThis","__py_terminal__"],"mappings":"yCAIA,MAAMA,EAAe,IAAIC,QAKnBC,E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,EAAYwB,SAAS,CACjBX,QAAQ,EACRY,MAAO,IAAMW,EAAS8C,KAAK3E,IAE3C,GACK"}
@@ -0,0 +1,2 @@
1
+ import{T as e,d as t,a as r,r as n,o,X as s,H as i,s as a}from"./core-2JDOwv1q.js";let c=0;const l=e=>`${e}-editor-${c++}`,d=new Map,u=new Map,p={worker:{codeBeforeRun:()=>a,onReady:({runAsync:e,io:t},{sync:r})=>{t.stdout=t.buffered(r.write),t.stderr=t.buffered(r.writeErr),r.revoke(),r.runAsync=e}}};async function m({currentTarget:e}){const{env:t,pySrc:r,outDiv:a}=this,c=!!e;if(c&&(e.disabled=!0,a.innerHTML=""),!d.has(t)){const e=URL.createObjectURL(new Blob([""])),r={type:this.interpreter},{config:a}=this;if(a){if(r.configURL=n(a),a.endsWith(".toml")){const[{parse:e},t]=await Promise.all([import("./toml-CvAfdf9_.js"),fetch(a).then((e=>e.text()))]);r.config=e(t)}else a.endsWith(".json")?r.config=await fetch(a).then((e=>e.json())):(r.configURL=n("./config.txt"),r.config=JSON.parse(a));r.version=o(r.config)}else r.config={};const c=s.call(new i(null,p),e,r),{sync:l}=c,{promise:u,resolve:m}=Promise.withResolvers();d.set(t,u),l.revoke=()=>{URL.revokeObjectURL(e),m(c)}}return d.get(t).then((t=>{t.onerror=({error:e})=>{c&&(a.innerHTML+=`<span style='color:red'>${e.message||e}</span>\n`),console.error(e)};const n=()=>{c&&(e.disabled=!1)},{sync:o}=t;o.write=e=>{c&&(a.innerText+=`${e}\n`)},o.writeErr=e=>{c&&(a.innerHTML+=`<span style='color:red'>${e}</span>\n`)},o.runAsync(r).then(n,n)}))}const f=(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",(async t=>{r.blur(),await e.handleEvent(t)})),r})(e,t),o=document.createElement("div");return o.addEventListener("keydown",(e=>{e.stopPropagation()})),r.append(n,o),r},g=(e,t)=>{const r=document.createElement("div");r.className=`${t}-editor-box`;const n=f(e,t),o=(e=>{const t=document.createElement("div");return t.className=`${e}-editor-output`,t.id=`${l(e)}-output`,t})(t);return r.append(n,o),[r,o,n.querySelector("button")]},h=async(e,n,o)=>{const[{basicSetup:s,EditorView:i},{Compartment:a},{python:c},{indentUnit:d},{keymap:p},{defaultKeymap:f,indentWithTab:h}]=await Promise.all([import("./codemirror-BZcZV62h.js"),import("./codemirror_state-D1qTXrff.js"),import("./codemirror_lang-python-BPBYxtKZ.js"),import("./codemirror_language-C0ccoJVB.js").then((function(e){return e.x})),import("./codemirror_view-CvBNngGG.js").then((function(e){return e.q})),import("./codemirror_commands-BNzatDRz.js")]);let v=e.hasAttribute("setup");const y=e.hasAttribute("config"),b=`${o}-${e.getAttribute("env")||l(n)}`;if(y&&u.has(b))throw new SyntaxError(u.get(b)?`duplicated config for env: ${b}`:`unable to add a config to the env: ${b}`);u.set(b,y);let w=e.src?await fetch(e.src).then((e=>e.text())):e.textContent;const E={handleEvent:m,interpreter:o,env:b,config:y&&e.getAttribute("config"),get pySrc(){return v?w:R.state.doc.toString()},get outDiv(){return v?null:S}};let $;t(e,{target:{get:()=>$},handleEvent:{get:()=>E.handleEvent,set:e=>{E.handleEvent=e===m?m:async r=>{const{currentTarget:n}=r;t(r,{code:{value:E.pySrc}}),!1!==await e(r)&&await m.call(E,{currentTarget:n})}}},code:{get:()=>E.pySrc,set:e=>{v||R.update([R.state.update({changes:{from:0,to:R.state.doc.length,insert:e}})])}},process:{value(e){const t=v,r=w;v=!0,w=e;const n=()=>{v=t,w=r};return E.handleEvent({currentTarget:null}).then(n,n)}}});const x=()=>{const t=new Event(`${n}-editor`,{bubbles:!0});e.dispatchEvent(t)};if(v)return await E.handleEvent({currentTarget:null}),void x();const A=e.getAttribute("target");if(A){if($=document.getElementById(A)||document.querySelector(A),!$)throw new Error(`Unknown target ${A}`)}else $=document.createElement(`${n}-editor`),$.style.display="block",e.after($);$.id||($.id=l(n)),$.hasAttribute("exec-id")||$.setAttribute("exec-id",0),$.hasAttribute("root")||$.setAttribute("root",$.id);const[L,S,C]=g(E,n);L.dataset.env=e.hasAttribute("env")?b:o;const T=L.querySelector(`.${n}-editor-input > div`).attachShadow({mode:"open"});T.innerHTML="<style> :host { all: initial; }</style>",$.appendChild(L);const k=r(e.textContent).trim(),j=/^([ \t]+)/m.test(k)?RegExp.$1:" ",M=()=>C.click(),R=new i({extensions:[d.of(j),(new a).of(c()),p.of([...f,{key:"Ctrl-Enter",run:M,preventDefault:!0},{key:"Cmd-Enter",run:M,preventDefault:!0},{key:"Shift-Enter",run:M,preventDefault:!0},h]),s],foldGutter:!0,gutters:["CodeMirror-linenumbers","CodeMirror-foldgutter"],parent:T,doc:k});R.focus(),x()};let v=0,y=Promise.resolve();const b=()=>{v=0,w()},w=()=>{if(!v){v=setTimeout(b,250);for(const[t,r]of e){const e=`script[type="${t}-editor"]`;for(const n of document.querySelectorAll(e))n.type+="-active",y=y.then((()=>h(n,t,r)))}return y}};new MutationObserver(w).observe(document,{childList:!0,subtree:!0});var E=w();export{E as default};
2
+ //# sourceMappingURL=py-editor-PTkeu-Zz.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"py-editor-PTkeu-Zz.js","sources":["../src/plugins/py-editor.js"],"sourcesContent":["// PyScript py-editor plugin\nimport { Hook, XWorker, dedent, defineProperties } from \"polyscript/exports\";\nimport { TYPES, offline_interpreter, relative_url, stdlib } 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();\nconst configs = new Map();\n\nconst hooks = {\n worker: {\n codeBeforeRun: () => stdlib,\n // works on both Pyodide and MicroPython\n onReady: ({ runAsync, io }, { sync }) => {\n io.stdout = io.buffered(sync.write);\n io.stderr = io.buffered(sync.writeErr);\n sync.revoke();\n sync.runAsync = runAsync;\n },\n },\n};\n\nasync function execute({ currentTarget }) {\n const { env, pySrc, outDiv } = this;\n const hasRunButton = !!currentTarget;\n\n if (hasRunButton) {\n currentTarget.disabled = true;\n outDiv.innerHTML = \"\";\n }\n\n if (!envs.has(env)) {\n const srcLink = URL.createObjectURL(new Blob([\"\"]));\n const details = { type: this.interpreter };\n const { config } = this;\n if (config) {\n details.configURL = relative_url(config);\n if (config.endsWith(\".toml\")) {\n const [\n { parse },\n toml,\n ] = await Promise.all([\n import(/* webpackIgnore: true */ \"../3rd-party/toml.js\"),\n fetch(config).then((r) => r.text()),\n ]);\n details.config = parse(toml);\n }\n else if (config.endsWith(\".json\")) {\n details.config = await fetch(config).then((r) => r.json());\n }\n else {\n details.configURL = relative_url('./config.txt');\n details.config = JSON.parse(config);\n }\n details.version = offline_interpreter(details.config);\n } else {\n details.config = {};\n }\n\n const xworker = XWorker.call(new Hook(null, hooks), srcLink, details);\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 return envs.get(env).then((xworker) => {\n xworker.onerror = ({ error }) => {\n if (hasRunButton) {\n outDiv.innerHTML += `<span style='color:red'>${\n error.message || error\n }</span>\\n`;\n }\n console.error(error);\n };\n\n const enable = () => {\n if (hasRunButton) currentTarget.disabled = false;\n };\n const { sync } = xworker;\n sync.write = (str) => {\n if (hasRunButton) outDiv.innerText += `${str}\\n`;\n };\n sync.writeErr = (str) => {\n if (hasRunButton) {\n outDiv.innerHTML += `<span style='color:red'>${str}</span>\\n`;\n }\n };\n sync.runAsync(pySrc).then(enable, enable);\n });\n}\n\nconst makeRunButton = (handler, 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\", async (event) => {\n runButton.blur();\n await handler.handleEvent(event);\n });\n return runButton;\n};\n\nconst makeEditorDiv = (handler, 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(handler, 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 = (handler, type) => {\n const boxDiv = document.createElement(\"div\");\n boxDiv.className = `${type}-editor-box`;\n\n const editorDiv = makeEditorDiv(handler, type);\n const outDiv = makeOutDiv(type);\n boxDiv.append(editorDiv, outDiv);\n\n return [boxDiv, outDiv, editorDiv.querySelector(\"button\")];\n};\n\nconst init = async (script, type, interpreter) => {\n const [\n { basicSetup, EditorView },\n { Compartment },\n { python },\n { indentUnit },\n { keymap },\n { defaultKeymap, indentWithTab },\n ] = await Promise.all([\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 let isSetup = script.hasAttribute(\"setup\");\n const hasConfig = script.hasAttribute(\"config\");\n const env = `${interpreter}-${script.getAttribute(\"env\") || getID(type)}`;\n\n if (hasConfig && configs.has(env)) {\n throw new SyntaxError(\n configs.get(env)\n ? `duplicated config for env: ${env}`\n : `unable to add a config to the env: ${env}`,\n );\n }\n\n configs.set(env, hasConfig);\n\n let source = script.src\n ? await fetch(script.src).then((b) => b.text())\n : script.textContent;\n const context = {\n // allow the listener to be overridden at distance\n handleEvent: execute,\n interpreter,\n env,\n config: hasConfig && script.getAttribute(\"config\"),\n get pySrc() {\n return isSetup ? source : editor.state.doc.toString();\n },\n get outDiv() {\n return isSetup ? null : outDiv;\n },\n };\n\n let target;\n defineProperties(script, {\n target: { get: () => target },\n handleEvent: {\n get: () => context.handleEvent,\n set: (callback) => {\n // do not bother with logic if it was set back as its original handler\n if (callback === execute) context.handleEvent = execute;\n // in every other case be sure that if the listener override returned\n // `false` nothing happens, otherwise keep doing what it always did\n else {\n context.handleEvent = async (event) => {\n // trap the currentTarget ASAP (if any)\n // otherwise it gets lost asynchronously\n const { currentTarget } = event;\n // augment a code snapshot before invoking the override\n defineProperties(event, {\n code: { value: context.pySrc },\n });\n // avoid executing the default handler if the override returned `false`\n if ((await callback(event)) !== false)\n await execute.call(context, { currentTarget });\n };\n }\n },\n },\n code: {\n get: () => context.pySrc,\n set: (insert) => {\n if (isSetup) return;\n editor.update([\n editor.state.update({\n changes: {\n from: 0,\n to: editor.state.doc.length,\n insert,\n },\n }),\n ]);\n },\n },\n process: {\n /**\n * Simulate a setup node overriding the source to evaluate.\n * @param {string} code the Python code to evaluate.\n * @returns {Promise<...>} fulfill once code has been evaluated.\n */\n value(code) {\n const wasSetup = isSetup;\n const wasSource = source;\n isSetup = true;\n source = code;\n const restore = () => {\n isSetup = wasSetup;\n source = wasSource;\n };\n return context\n .handleEvent({ currentTarget: null })\n .then(restore, restore);\n },\n },\n });\n\n const notify = () => {\n const event = new Event(`${type}-editor`, { bubbles: true });\n script.dispatchEvent(event);\n };\n\n if (isSetup) {\n await context.handleEvent({ currentTarget: null });\n notify();\n return;\n }\n\n const selector = script.getAttribute(\"target\");\n\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 // @see https://github.com/JeffersGlass/mkdocs-pyscript/blob/main/mkdocs_pyscript/js/makeblocks.js\n const [boxDiv, outDiv, runButton] = makeBoxDiv(context, 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 = /^([ \\t]+)/m.test(doc) ? RegExp.$1 : \" \";\n\n const listener = () => runButton.click();\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 // @see https://codemirror.net/examples/tab/\n indentWithTab,\n ]),\n basicSetup,\n ],\n foldGutter: true,\n gutters: [\"CodeMirror-linenumbers\", \"CodeMirror-foldgutter\"],\n parent,\n doc,\n });\n\n editor.focus();\n notify();\n};\n\n// avoid too greedy MutationObserver operations at distance\nlet timeout = 0;\n\n// avoid delayed initialization\nlet queue = Promise.resolve();\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 = () => {\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 by changing the type as active\n script.type += \"-active\";\n // don't await in here or multiple calls might happen\n // while the first script is being initialized\n queue = queue.then(() => init(script, type, interpreter));\n }\n }\n return queue;\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","configs","hooks","worker","codeBeforeRun","stdlib","onReady","runAsync","io","sync","stdout","buffered","write","stderr","writeErr","revoke","async","execute","currentTarget","env","pySrc","outDiv","this","hasRunButton","disabled","innerHTML","has","srcLink","URL","createObjectURL","Blob","details","interpreter","config","configURL","relative_url","endsWith","parse","toml","Promise","all","import","fetch","then","r","text","json","JSON","version","offline_interpreter","xworker","XWorker","call","Hook","promise","resolve","withResolvers","set","revokeObjectURL","get","onerror","error","message","console","enable","str","innerText","makeEditorDiv","handler","editorDiv","document","createElement","className","setAttribute","runButton","addEventListener","event","blur","handleEvent","makeRunButton","editorShadowContainer","stopPropagation","append","makeBoxDiv","boxDiv","makeOutDiv","querySelector","init","script","basicSetup","EditorView","Compartment","python","indentUnit","keymap","defaultKeymap","indentWithTab","n","x","q","isSetup","hasAttribute","hasConfig","getAttribute","SyntaxError","source","src","b","textContent","context","editor","state","doc","toString","target","defineProperties","callback","code","value","insert","update","changes","from","to","length","process","wasSetup","wasSource","restore","notify","Event","bubbles","dispatchEvent","selector","getElementById","Error","style","display","after","dataset","parent","attachShadow","mode","appendChild","dedent","trim","indentation","test","RegExp","$1","listener","click","extensions","of","key","run","preventDefault","foldGutter","gutters","focus","timeout","queue","resetTimeout","pyEditor","setTimeout","TYPES","querySelectorAll","MutationObserver","observe","childList","subtree","pyEditor$1"],"mappings":"mFAMA,IAAIA,EAAK,EACT,MAAMC,EAASC,GAAS,GAAGA,YAAeF,MAEpCG,EAAO,IAAIC,IACXC,EAAU,IAAID,IAEdE,EAAQ,CACVC,OAAQ,CACJC,cAAe,IAAMC,EAErBC,QAAS,EAAGC,WAAUC,OAAQC,WAC1BD,EAAGE,OAASF,EAAGG,SAASF,EAAKG,OAC7BJ,EAAGK,OAASL,EAAGG,SAASF,EAAKK,UAC7BL,EAAKM,SACLN,EAAKF,SAAWA,CAAQ,IAKpCS,eAAeC,GAAQC,cAAEA,IACrB,MAAMC,IAAEA,EAAGC,MAAEA,EAAKC,OAAEA,GAAWC,KACzBC,IAAiBL,EAOvB,GALIK,IACAL,EAAcM,UAAW,EACzBH,EAAOI,UAAY,KAGlB1B,EAAK2B,IAAIP,GAAM,CAChB,MAAMQ,EAAUC,IAAIC,gBAAgB,IAAIC,KAAK,CAAC,MACxCC,EAAU,CAAEjC,KAAMwB,KAAKU,cACvBC,OAAEA,GAAWX,KACnB,GA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gBAAgB,GACnD,CAAEF,IAAK,cAAeC,IAAKL,EAAUM,gBAAgB,GAErD5D,IAEJP,GAEJoE,YAAY,EACZC,QAAS,CAAC,yBAA0B,yBACpClB,SACA5B,QAGJF,EAAOiD,QACP9B,GAAQ,EAIZ,IAAI+B,EAAU,EAGVC,EAAQ1H,QAAQgB,UAGpB,MAAM2G,EAAe,KACjBF,EAAU,EACVG,GAAU,EAIRA,EAAW,KACb,IAAIH,EAAJ,CACAA,EAAUI,WAAWF,EAAc,KACnC,IAAK,MAAOpK,EAAMkC,KAAgBqI,EAAO,CACrC,MAAMhC,EAAW,gBAAgBvI,aACjC,IAAK,MAAM0F,KAAUlB,SAASgG,iBAAiBjC,GAE3C7C,EAAO1F,MAAQ,UAGfmK,EAAQA,EAAMtH,MAAK,IAAM4C,EAAKC,EAAQ1F,EAAMkC,IAEnD,CACD,OAAOiI,CAZa,CAYR,EAGhB,IAAIM,iBAAiBJ,GAAUK,QAAQlG,SAAU,CAC7CmG,WAAW,EACXC,SAAS,IAIb,IAAAC,EAAeR"}
@@ -0,0 +1,2 @@
1
+ import{T as t,c as e,r}from"./core-2JDOwv1q.js";import{notify as s}from"./error-BGZL6Fui.js";const o=[],a=new WeakSet,n=t=>{throw s(t),new Error(t)},m=({attributes:{worker:t}})=>!t;let c=!0;for(const s of t.keys()){const t=`script[type="${s}"][terminal],${s}-script[terminal]`;o.push(t),e.set(t,(async t=>{const e=document.querySelectorAll(o.join(","));if([].filter.call(e,m).length>1&&n("You can use at most 1 main terminal"),c&&(c=!1,document.head.append(Object.assign(document.createElement("link"),{rel:"stylesheet",href:r("./xterm.css",import.meta.url)}))),a.has(t))return;a.add(t);const i=e=>e.default(t);"mpy"===s?await import("./mpy-B0zAxV1g.js").then(i):await import("./py-C0ESszMr.js").then(i)}))}
2
+ //# sourceMappingURL=py-terminal-Q9SMt9_A.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"py-terminal-Q9SMt9_A.js","sources":["../src/plugins/py-terminal.js"],"sourcesContent":["// PyScript py-terminal plugin\nimport { TYPES, relative_url } from \"../core.js\";\nimport { notify } from \"./error.js\";\nimport { customObserver } from \"polyscript/exports\";\n\n// will contain all valid selectors\nconst SELECTORS = [];\n\n// avoid processing same elements twice\nconst processed = new WeakSet();\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 onceOnMain = ({ attributes: { worker } }) => !worker;\n\nlet addStyle = true;\n\nfor (const type of TYPES.keys()) {\n const selector = `script[type=\"${type}\"][terminal],${type}-script[terminal]`;\n SELECTORS.push(selector);\n customObserver.set(selector, async (element) => {\n // we currently support only one terminal on main as in \"classic\"\n const terminals = document.querySelectorAll(SELECTORS.join(\",\"));\n if ([].filter.call(terminals, onceOnMain).length > 1)\n notifyAndThrow(\"You can use at most 1 main terminal\");\n\n // import styles lazily\n if (addStyle) {\n addStyle = false;\n document.head.append(\n Object.assign(document.createElement(\"link\"), {\n rel: \"stylesheet\",\n href: relative_url(\"./xterm.css\", import.meta.url),\n }),\n );\n }\n\n if (processed.has(element)) return;\n processed.add(element);\n\n const bootstrap = (module) => module.default(element);\n\n // we can't be smart with template literals for the dynamic import\n // or bundlers are incapable of producing multiple files around\n if (type === \"mpy\") {\n await import(/* webpackIgnore: true */ \"./py-terminal/mpy.js\").then(\n bootstrap,\n );\n } else {\n await import(/* webpackIgnore: true */ \"./py-terminal/py.js\").then(\n bootstrap,\n );\n }\n });\n}\n"],"names":["SELECTORS","processed","WeakSet","notifyAndThrow","message","notify","Error","onceOnMain","attributes","worker","addStyle","type","TYPES","keys","selector","push","customObserver","set","async","element","terminals","document","querySelectorAll","join","filter","call","length","head","append","Object","assign","createElement","rel","href","relative_url","url","has","add","bootstrap","module","default","import","then"],"mappings":"6FAMA,MAAMA,EAAY,GAGZC,EAAY,IAAIC,QAIhBC,EAAkBC,IAEpB,MADAC,EAAOD,GACD,IAAIE,MAAMF,EAAQ,EAGtBG,EAAa,EAAGC,YAAcC,cAAgBA,EAEpD,IAAIC,GAAW,EAEf,IAAK,MAAMC,KAAQC,EAAMC,OAAQ,CAC7B,MAAMC,EAAW,gBAAgBH,iBAAoBA,qBACrDX,EAAUe,KAAKD,GACfE,EAAeC,IAAIH,GAAUI,MAAOC,IAEhC,MAAMC,EAAYC,SAASC,iBAAiBtB,EAAUuB,KAAK,MAe3D,GAdI,GAAGC,OAAOC,KAAKL,EAAWb,GAAYmB,OAAS,GAC/CvB,EAAe,uCAGfO,IACAA,GAAW,EACXW,SAASM,KAAKC,OACVC,OAAOC,OAAOT,SAASU,cAAc,QAAS,CAC1CC,IAAK,aACLC,KAAMC,EAAa,0BAA2BC,SAKtDlC,EAAUmC,IAAIjB,GAAU,OAC5BlB,EAAUoC,IAAIlB,GAEd,MAAMmB,EAAaC,GAAWA,EAAOC,QAAQrB,GAIhC,QAATR,QACM8B,OAAiC,qBAAwBC,KAC3DJ,SAGEG,OAAiC,oBAAuBC,KAC1DJ,EAEP,GAET"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pyscript/core",
3
- "version": "0.4.51",
3
+ "version": "0.4.52",
4
4
  "type": "module",
5
5
  "description": "PyScript",
6
6
  "module": "./index.js",
@@ -62,7 +62,7 @@
62
62
  "@webreflection/toml-j0.4": "^1.1.3",
63
63
  "@xterm/addon-fit": "^0.10.0",
64
64
  "@xterm/addon-web-links": "^0.11.0",
65
- "bun": "^1.1.15",
65
+ "bun": "^1.1.16",
66
66
  "chokidar": "^3.6.0",
67
67
  "codemirror": "^6.0.1",
68
68
  "eslint": "^9.5.0",
package/src/config.js CHANGED
@@ -45,6 +45,8 @@ const configDetails = async (config, type) => {
45
45
 
46
46
  const conflictError = (reason) => new Error(`(${CONFLICTING_CODE}): ${reason}`);
47
47
 
48
+ const relative_url = (url, base = location.href) => new URL(url, base).href;
49
+
48
50
  const syntaxError = (type, url, { message }) => {
49
51
  let str = `(${BAD_CONFIG}): Invalid ${type}`;
50
52
  if (url) str += ` @ ${url}`;
@@ -108,7 +110,7 @@ for (const [TYPE] of TYPES) {
108
110
  if (!error && config) {
109
111
  try {
110
112
  const { json, toml, text, url } = await configDetails(config, type);
111
- if (url) configURL = new URL(url, location.href).href;
113
+ if (url) configURL = relative_url(url);
112
114
  config = text;
113
115
  if (json || type === "json") {
114
116
  try {
@@ -153,4 +155,4 @@ for (const [TYPE] of TYPES) {
153
155
  configs.set(TYPE, { config: parsed, configURL, plugins, error });
154
156
  }
155
157
 
156
- export default configs;
158
+ export { configs, relative_url };
package/src/core.js CHANGED
@@ -19,7 +19,7 @@ import {
19
19
 
20
20
  import "./all-done.js";
21
21
  import TYPES from "./types.js";
22
- import configs from "./config.js";
22
+ import { configs, relative_url } from "./config.js";
23
23
  import sync from "./sync.js";
24
24
  import bootstrapNodeAndPlugins from "./plugins-helper.js";
25
25
  import { ErrorCode } from "./exceptions.js";
@@ -84,6 +84,7 @@ const [
84
84
 
85
85
  export {
86
86
  TYPES,
87
+ relative_url,
87
88
  exportedPyWorker as PyWorker,
88
89
  exportedMPWorker as MPWorker,
89
90
  exportedHooks as hooks,
@@ -92,7 +93,7 @@ export {
92
93
  };
93
94
 
94
95
  export const offline_interpreter = (config) =>
95
- config?.interpreter && new URL(config.interpreter, location.href).href;
96
+ config?.interpreter && relative_url(config.interpreter);
96
97
 
97
98
  const hooked = new Map();
98
99
 
@@ -1,6 +1,6 @@
1
1
  // PyScript py-editor plugin
2
2
  import { Hook, XWorker, dedent, defineProperties } from "polyscript/exports";
3
- import { TYPES, offline_interpreter, stdlib } from "../core.js";
3
+ import { TYPES, offline_interpreter, relative_url, stdlib } from "../core.js";
4
4
 
5
5
  const 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>`;
6
6
 
@@ -37,11 +37,19 @@ async function execute({ currentTarget }) {
37
37
  const details = { type: this.interpreter };
38
38
  const { config } = this;
39
39
  if (config) {
40
- details.configURL = config;
41
- const { parse } = config.endsWith(".toml")
42
- ? await import(/* webpackIgnore: true */ "../3rd-party/toml.js")
43
- : JSON;
44
- details.config = parse(await fetch(config).then((r) => r.text()));
40
+ details.configURL = relative_url(config);
41
+ if (config.endsWith(".toml")) {
42
+ const [{ parse }, toml] = await Promise.all([
43
+ import(/* webpackIgnore: true */ "../3rd-party/toml.js"),
44
+ fetch(config).then((r) => r.text()),
45
+ ]);
46
+ details.config = parse(toml);
47
+ } else if (config.endsWith(".json")) {
48
+ details.config = await fetch(config).then((r) => r.json());
49
+ } else {
50
+ details.configURL = relative_url("./config.txt");
51
+ details.config = JSON.parse(config);
52
+ }
45
53
  details.version = offline_interpreter(details.config);
46
54
  } else {
47
55
  details.config = {};
@@ -175,9 +183,7 @@ const init = async (script, type, interpreter) => {
175
183
  handleEvent: execute,
176
184
  interpreter,
177
185
  env,
178
- config:
179
- hasConfig &&
180
- new URL(script.getAttribute("config"), location.href).href,
186
+ config: hasConfig && script.getAttribute("config"),
181
187
  get pySrc() {
182
188
  return isSetup ? source : editor.state.doc.toString();
183
189
  },
@@ -1,5 +1,5 @@
1
1
  // PyScript py-terminal plugin
2
- import { TYPES } from "../core.js";
2
+ import { TYPES, relative_url } from "../core.js";
3
3
  import { notify } from "./error.js";
4
4
  import { customObserver } from "polyscript/exports";
5
5
 
@@ -35,7 +35,7 @@ for (const type of TYPES.keys()) {
35
35
  document.head.append(
36
36
  Object.assign(document.createElement("link"), {
37
37
  rel: "stylesheet",
38
- href: new URL("./xterm.css", import.meta.url),
38
+ href: relative_url("./xterm.css", import.meta.url),
39
39
  }),
40
40
  );
41
41
  }
@@ -46,9 +46,6 @@ from pyscript.magic_js import (
46
46
  from pyscript.storage import Storage, storage
47
47
  from pyscript.websocket import WebSocket
48
48
 
49
- if not RUNNING_IN_WORKER:
50
- from pyscript.workers import workers
51
-
52
49
  try:
53
50
  from pyscript.event_handling import when
54
51
  except:
@@ -1,7 +1,7 @@
1
1
  // ⚠️ This file is an artifact: DO NOT MODIFY
2
2
  export default {
3
3
  "pyscript": {
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 polyscript import lazy_py_modules as py_import\nfrom pyscript.display import HTML, display\nfrom pyscript.fetch import fetch\nfrom pyscript.magic_js import (\n RUNNING_IN_WORKER,\n PyWorker,\n config,\n current_target,\n document,\n js_import,\n js_modules,\n sync,\n window,\n)\nfrom pyscript.storage import Storage, storage\nfrom pyscript.websocket import WebSocket\n\nif not RUNNING_IN_WORKER:\n from pyscript.workers import workers\n\ntry:\n from pyscript.event_handling import when\nexcept:\n # TODO: should we remove this? Or at the very least, we should capture\n # the traceback otherwise it's very hard to debug\n from pyscript.util import NotSupported\n\n when = NotSupported(\n \"pyscript.when\", \"pyscript.when currently not available with this interpreter\"\n )\n",
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 polyscript import lazy_py_modules as py_import\nfrom pyscript.display import HTML, display\nfrom pyscript.fetch import fetch\nfrom pyscript.magic_js import (\n RUNNING_IN_WORKER,\n PyWorker,\n config,\n current_target,\n document,\n js_import,\n js_modules,\n sync,\n window,\n)\nfrom pyscript.storage import Storage, storage\nfrom pyscript.websocket import WebSocket\n\ntry:\n from pyscript.event_handling import when\nexcept:\n # TODO: should we remove this? Or at the very least, we should capture\n # the traceback otherwise it's very hard to debug\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 \"savefig\": \"image/png\",\n \"_repr_javascript_\": \"application/javascript\",\n \"_repr_json_\": \"application/json\",\n \"_repr_latex\": \"text/latex\",\n \"_repr_png_\": \"image/png\",\n \"_repr_jpeg_\": \"image/jpeg\",\n \"_repr_pdf_\": \"application/pdf\",\n \"_repr_svg_\": \"image/svg+xml\",\n \"_repr_markdown_\": \"text/markdown\",\n \"_repr_html_\": \"text/html\",\n \"__repr__\": \"text/plain\",\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 _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\ntry:\n from pyodide.ffi.wrappers import add_event_listener\n\nexcept ImportError:\n\n def add_event_listener(el, event_type, func):\n el.addEventListener(event_type, func)\n\n\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 try:\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 else:\n wrapper = func\n\n except AttributeError:\n # TODO: this is very ugly hack to get micropython working because inspect.signature\n # doesn't exist, but we need to actually properly replace inspect.signature.\n # It may be actually better to not try any magic for now and raise the error\n def wrapper(*args, **kwargs):\n try:\n return func(*args, **kwargs)\n except TypeError as e:\n if \"takes\" in str(e) and \"positional arguments\" in str(e):\n return func()\n\n raise\n\n for el in elements:\n add_event_listener(el, event_type, wrapper)\n\n return func\n\n return decorator\n",
7
7
  "fetch.py": "import json\n\nimport js\nfrom pyscript.util import as_bytearray\n\n\n### wrap the response to grant Pythonic results\nclass _Response:\n def __init__(self, response):\n self._response = response\n\n # grant access to response.ok and other fields\n def __getattr__(self, attr):\n return getattr(self._response, attr)\n\n # exposed methods with Pythonic results\n async def arrayBuffer(self):\n buffer = await self._response.arrayBuffer()\n # works in Pyodide\n if hasattr(buffer, \"to_py\"):\n return buffer.to_py()\n # shims in MicroPython\n return memoryview(as_bytearray(buffer))\n\n async def blob(self):\n return await self._response.blob()\n\n async def bytearray(self):\n buffer = await self._response.arrayBuffer()\n return as_bytearray(buffer)\n\n async def json(self):\n return json.loads(await self.text())\n\n async def text(self):\n return await self._response.text()\n\n\n### allow direct await to _Response methods\nclass _DirectResponse:\n @staticmethod\n def setup(promise, response):\n promise._response = _Response(response)\n return promise._response\n\n def __init__(self, promise):\n self._promise = promise\n promise._response = None\n promise.arrayBuffer = self.arrayBuffer\n promise.blob = self.blob\n promise.bytearray = self.bytearray\n promise.json = self.json\n promise.text = self.text\n\n async def _response(self):\n if not self._promise._response:\n await self._promise\n return self._promise._response\n\n async def arrayBuffer(self):\n response = await self._response()\n return await response.arrayBuffer()\n\n async def blob(self):\n response = await self._response()\n return await response.blob()\n\n async def bytearray(self):\n response = await self._response()\n return await response.bytearray()\n\n async def json(self):\n response = await self._response()\n return await response.json()\n\n async def text(self):\n response = await self._response()\n return await response.text()\n\n\ndef fetch(url, **kw):\n # workaround Pyodide / MicroPython dict <-> js conversion\n options = js.JSON.parse(json.dumps(kw))\n awaited = lambda response, *args: _DirectResponse.setup(promise, response)\n promise = js.fetch(url, options).then(awaited)\n _DirectResponse(promise)\n return promise\n",
@@ -16,8 +16,7 @@ export default {
16
16
  "elements.py": "import inspect\nimport sys\n\ntry:\n from typing import Any\nexcept ImportError:\n Any = \"Any\"\n\ntry:\n import warnings\nexcept ImportError:\n # TODO: For now it probably means we are in MicroPython. We should figure\n # out the \"right\" way to handle this. For now we just ignore the warning\n # and logging to console\n class warnings:\n @staticmethod\n def warn(*args, **kwargs):\n print(\"WARNING: \", *args, **kwargs)\n\n\ntry:\n from functools import cached_property\nexcept ImportError:\n # TODO: same comment about micropython as above\n cached_property = property\n\ntry:\n from pyodide.ffi import JsProxy\nexcept ImportError:\n # TODO: same comment about micropython as above\n def JsProxy(obj):\n return obj\n\n\nfrom pyscript import document, window\n\n# from pyscript.web import dom as pydom\n\n#: A flag to show if MicroPython is the current Python interpreter.\nis_micropython = \"MicroPython\" in sys.version\n\n\ndef getmembers_static(cls):\n \"\"\"Cross-interpreter implementation of inspect.getmembers_static.\"\"\"\n\n if is_micropython: # pragma: no cover\n return [(name, getattr(cls, name)) for name, _ in inspect.getmembers(cls)]\n\n return inspect.getmembers_static(cls)\n\n\nclass JSProperty:\n \"\"\"JS property descriptor that directly maps to the property with the same\n name in the underlying JS component.\"\"\"\n\n def __init__(self, name: str, allow_nones: bool = False):\n self.name = name\n self.allow_nones = allow_nones\n\n def __get__(self, obj, objtype=None):\n return getattr(obj._js, self.name)\n\n def __set__(self, obj, value):\n if not self.allow_nones and value is None:\n return\n setattr(obj._js, self.name, value)\n\n\n# ------ TODO: REMOVE!!!! pydom elements\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 # TODO: This should actually return the correct class (== to tagName)\n self._parent = Element(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 inspect.isclass(JsProxy) and 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 text(self):\n return self._js.textContent\n\n @text.setter\n def text(self, value):\n self._js.textContent = 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 def snap(\n self,\n to: BaseElement | str = None,\n width: int | None = None,\n height: int | None = None,\n ):\n \"\"\"\n Captures a snapshot of a video element. (Only available for video elements)\n\n Inputs:\n\n * to: element where to save the snapshot of the video frame to\n * width: width of the image\n * height: height of the image\n\n Output:\n (Element) canvas element where the video frame snapshot was drawn into\n \"\"\"\n if self._js.tagName != \"VIDEO\":\n raise AttributeError(\"Snap method is only available for video Elements\")\n\n if to is None:\n canvas = self.create(\"canvas\")\n if width is None:\n width = self._js.width\n if height is None:\n height = self._js.height\n canvas._js.width = width\n canvas._js.height = height\n\n elif isinstance(to, Element):\n if to._js.tagName != \"CANVAS\":\n raise TypeError(\"Element to snap to must a canvas.\")\n canvas = to\n elif getattr(to, \"tagName\", \"\") == \"CANVAS\":\n canvas = Element(to)\n elif isinstance(to, str):\n # TODO (fpliger): This needs a better fix but doing a local import here for a quick fix\n from pyscript.web import dom\n\n canvas = dom[to][0]\n if canvas._js.tagName != \"CANVAS\":\n raise TypeError(\"Element to snap to must a be canvas.\")\n\n canvas.draw(self, width, height)\n\n return canvas\n\n def download(self, filename: str = \"snapped.png\") -> None:\n \"\"\"Download the current element (only available for canvas elements) with the filename\n provided in input.\n\n Inputs:\n * filename (str): name of the file being downloaded\n\n Output:\n None\n \"\"\"\n if self._js.tagName != \"CANVAS\":\n raise AttributeError(\n \"The download method is only available for canvas Elements\"\n )\n\n link = self.create(\"a\")\n link._js.download = filename\n link._js.href = self._js.toDataURL()\n link._js.click()\n\n def draw(self, what, width, height):\n \"\"\"Draw `what` on the current element (only available for canvas elements).\n\n Inputs:\n\n * what (canvas image source): An element to draw into the context. The specification permits any canvas\n image source, specifically, an HTMLImageElement, an SVGImageElement, an HTMLVideoElement,\n an HTMLCanvasElement, an ImageBitmap, an OffscreenCanvas, or a VideoFrame.\n \"\"\"\n if self._js.tagName != \"CANVAS\":\n raise AttributeError(\n \"The draw method is only available for canvas Elements\"\n )\n\n if isinstance(what, Element):\n what = what._js\n\n # https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage\n self._js.getContext(\"2d\").drawImage(what, 0, 0, width, height)\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\n# --------- END OF PYDOM STUFF ------\n\n\nclass ElementBase(Element):\n tag = \"div\"\n\n # GLOBAL ATTRIBUTES\n # These are attribute that all elements have (this list is a subset of the official one)\n # We are trying to capture the most used ones\n accesskey = JSProperty(\"accesskey\")\n autofocus = JSProperty(\"autofocus\")\n autocapitalize = JSProperty(\"autocapitalize\")\n className = JSProperty(\"className\")\n contenteditable = JSProperty(\"contenteditable\")\n draggable = JSProperty(\"draggable\")\n enterkeyhint = JSProperty(\"enterkeyhint\")\n hidden = JSProperty(\"hidden\")\n id = JSProperty(\"id\")\n lang = JSProperty(\"lang\")\n nonce = JSProperty(\"nonce\")\n part = JSProperty(\"part\")\n popover = JSProperty(\"popover\")\n slot = JSProperty(\"slot\")\n spellcheck = JSProperty(\"spellcheck\")\n tabindex = JSProperty(\"tabindex\")\n title = JSProperty(\"title\")\n translate = JSProperty(\"translate\")\n virtualkeyboardpolicy = JSProperty(\"virtualkeyboardpolicy\")\n\n def __init__(self, style=None, **kwargs):\n super().__init__(document.createElement(self.tag))\n\n # set all the style properties provided in input\n if isinstance(style, dict):\n for key, value in style.items():\n self.style[key] = value\n elif style is None:\n pass\n else:\n raise ValueError(\n f\"Style should be a dictionary, received {style} (type {type(style)}) instead.\"\n )\n\n # IMPORTANT!!! This is used to auto-harvest all input arguments and set them as properties\n self._init_properties(**kwargs)\n\n def _init_properties(self, **kwargs):\n \"\"\"Set all the properties (of type JSProperties) provided in input as properties\n of the class instance.\n\n Args:\n **kwargs: The properties to set\n \"\"\"\n # Look at all the properties of the class and see if they were provided in kwargs\n for attr_name, attr in getmembers_static(self.__class__):\n # For each one, actually check if it is a property of the class and set it\n if isinstance(attr, JSProperty) and attr_name in kwargs:\n try:\n setattr(self, attr_name, kwargs[attr_name])\n except Exception as e:\n print(f\"Error setting {attr_name} to {kwargs[attr_name]}: {e}\")\n raise\n\n\nclass TextElementBase(ElementBase):\n def __init__(self, content=None, style=None, **kwargs):\n super().__init__(style=style, **kwargs)\n\n # If it's an element, append the element\n if isinstance(content, Element):\n self.append(content)\n # If it's a list of elements\n elif isinstance(content, list):\n for item in content:\n self.append(item)\n # If the content wasn't set just ignore\n elif content is None:\n pass\n else:\n # Otherwise, set content as the html of the element\n self.html = content\n\n\n# IMPORTANT: For all HTML components defined below, we are not mapping all\n# available attributes, just the global and the most common ones.\n# If you need to access a specific attribute, you can always use the `_js.<attribute>`\nclass a(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a\"\"\"\n\n tag = \"a\"\n\n download = JSProperty(\"download\")\n href = JSProperty(\"href\")\n referrerpolicy = JSProperty(\"referrerpolicy\")\n rel = JSProperty(\"rel\")\n target = JSProperty(\"target\")\n type = JSProperty(\"type\")\n\n\nclass abbr(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/abbr\"\"\"\n\n tag = \"abbr\"\n\n\nclass address(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/address\"\"\"\n\n tag = \"address\"\n\n\nclass area(ElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/area\"\"\"\n\n tag = \"area\"\n\n alt = JSProperty(\"alt\")\n coords = JSProperty(\"coords\")\n download = JSProperty(\"download\")\n href = JSProperty(\"href\")\n ping = JSProperty(\"ping\")\n referrerpolicy = JSProperty(\"referrerpolicy\")\n rel = JSProperty(\"rel\")\n shape = JSProperty(\"shape\")\n target = JSProperty(\"target\")\n\n\nclass article(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/article\"\"\"\n\n tag = \"article\"\n\n\nclass aside(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/aside\"\"\"\n\n tag = \"aside\"\n\n\nclass audio(ElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio\"\"\"\n\n tag = \"audio\"\n\n autoplay = JSProperty(\"autoplay\")\n controls = JSProperty(\"controls\")\n controlslist = JSProperty(\"controlslist\")\n crossorigin = JSProperty(\"crossorigin\")\n disableremoteplayback = JSProperty(\"disableremoteplayback\")\n loop = JSProperty(\"loop\")\n muted = JSProperty(\"muted\")\n preload = JSProperty(\"preload\")\n src = JSProperty(\"src\")\n\n\nclass b(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/b\"\"\"\n\n tag = \"b\"\n\n\nclass blockquote(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/blockquote\"\"\"\n\n tag = \"blockquote\"\n\n cite = JSProperty(\"cite\")\n\n\nclass br(ElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/br\"\"\"\n\n tag = \"br\"\n\n\nclass button(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button\"\"\"\n\n tag = \"button\"\n\n autofocus = JSProperty(\"autofocus\")\n disabled = JSProperty(\"disabled\")\n form = JSProperty(\"form\")\n formaction = JSProperty(\"formaction\")\n formenctype = JSProperty(\"formenctype\")\n formmethod = JSProperty(\"formmethod\")\n formnovalidate = JSProperty(\"formnovalidate\")\n formtarget = JSProperty(\"formtarget\")\n name = JSProperty(\"name\")\n type = JSProperty(\"type\")\n value = JSProperty(\"value\")\n\n\nclass canvas(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/canvas\"\"\"\n\n tag = \"canvas\"\n\n height = JSProperty(\"height\")\n width = JSProperty(\"width\")\n\n\nclass caption(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/caption\"\"\"\n\n tag = \"caption\"\n\n\nclass cite(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/cite\"\"\"\n\n tag = \"cite\"\n\n\nclass code(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/code\"\"\"\n\n tag = \"code\"\n\n\nclass data(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/data\"\"\"\n\n tag = \"data\"\n\n value = JSProperty(\"value\")\n\n\nclass datalist(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/datalist\"\"\"\n\n tag = \"datalist\"\n\n\nclass dd(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dd\"\"\"\n\n tag = \"dd\"\n\n\nclass del_(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/del\"\"\"\n\n tag = \"del\"\n\n cite = JSProperty(\"cite\")\n datetime = JSProperty(\"datetime\")\n\n\nclass details(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/details\"\"\"\n\n tag = \"details\"\n\n open = JSProperty(\"open\")\n\n\nclass dialog(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dialog\"\"\"\n\n tag = \"dialog\"\n\n open = JSProperty(\"open\")\n\n\nclass div(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/div\"\"\"\n\n tag = \"div\"\n\n\nclass dl(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dl\"\"\"\n\n tag = \"dl\"\n\n value = JSProperty(\"value\")\n\n\nclass dt(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dt\"\"\"\n\n tag = \"dt\"\n\n\nclass em(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/em\"\"\"\n\n tag = \"em\"\n\n\nclass embed(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/embed\"\"\"\n\n tag = \"embed\"\n\n height = JSProperty(\"height\")\n src = JSProperty(\"src\")\n type = JSProperty(\"type\")\n width = JSProperty(\"width\")\n\n\nclass fieldset(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/fieldset\"\"\"\n\n tag = \"fieldset\"\n\n disabled = JSProperty(\"disabled\")\n form = JSProperty(\"form\")\n name = JSProperty(\"name\")\n\n\nclass figcaption(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/figcaption\"\"\"\n\n tag = \"figcaption\"\n\n\nclass figure(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/figure\"\"\"\n\n tag = \"figure\"\n\n\nclass footer(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/footer\"\"\"\n\n tag = \"footer\"\n\n\nclass form(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form\"\"\"\n\n tag = \"form\"\n\n accept_charset = JSProperty(\"accept-charset\")\n action = JSProperty(\"action\")\n autocapitalize = JSProperty(\"autocapitalize\")\n autocomplete = JSProperty(\"autocomplete\")\n enctype = JSProperty(\"enctype\")\n name = JSProperty(\"name\")\n method = JSProperty(\"method\")\n nonvalidate = JSProperty(\"nonvalidate\")\n rel = JSProperty(\"rel\")\n target = JSProperty(\"target\")\n\n\nclass h1(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/h1\"\"\"\n\n tag = \"h1\"\n\n\nclass h2(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/h2\"\"\"\n\n tag = \"h2\"\n\n\nclass h3(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/h3\"\"\"\n\n tag = \"h3\"\n\n\nclass h4(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/h4\"\"\"\n\n tag = \"h4\"\n\n\nclass h5(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/h5\"\"\"\n\n tag = \"h5\"\n\n\nclass h6(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/h6\"\"\"\n\n tag = \"h6\"\n\n\nclass header(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/header\"\"\"\n\n tag = \"header\"\n\n\nclass hgroup(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/hgroup\"\"\"\n\n tag = \"hgroup\"\n\n\nclass hr(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/hr\"\"\"\n\n tag = \"hr\"\n\n\nclass i(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/i\"\"\"\n\n tag = \"i\"\n\n\nclass iframe(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe\"\"\"\n\n tag = \"iframe\"\n\n allow = JSProperty(\"allow\")\n allowfullscreen = JSProperty(\"allowfullscreen\")\n height = JSProperty(\"height\")\n loading = JSProperty(\"loading\")\n name = JSProperty(\"name\")\n referrerpolicy = JSProperty(\"referrerpolicy\")\n sandbox = JSProperty(\"sandbox\")\n src = JSProperty(\"src\")\n srcdoc = JSProperty(\"srcdoc\")\n width = JSProperty(\"width\")\n\n\nclass img(ElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img\"\"\"\n\n tag = \"img\"\n\n alt = JSProperty(\"alt\")\n crossorigin = JSProperty(\"crossorigin\")\n decoding = JSProperty(\"decoding\")\n fetchpriority = JSProperty(\"fetchpriority\")\n height = JSProperty(\"height\")\n ismap = JSProperty(\"ismap\")\n loading = JSProperty(\"loading\")\n referrerpolicy = JSProperty(\"referrerpolicy\")\n sizes = JSProperty(\"sizes\")\n src = JSProperty(\"src\")\n width = JSProperty(\"width\")\n\n\n# NOTE: Input is a reserved keyword in Python, so we use input_ instead\nclass input_(ElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input\"\"\"\n\n tag = \"input\"\n\n accept = JSProperty(\"accept\")\n alt = JSProperty(\"alt\")\n autofocus = JSProperty(\"autofocus\")\n capture = JSProperty(\"capture\")\n checked = JSProperty(\"checked\")\n dirname = JSProperty(\"dirname\")\n disabled = JSProperty(\"disabled\")\n form = JSProperty(\"form\")\n formaction = JSProperty(\"formaction\")\n formenctype = JSProperty(\"formenctype\")\n formmethod = JSProperty(\"formmethod\")\n formnovalidate = JSProperty(\"formnovalidate\")\n formtarget = JSProperty(\"formtarget\")\n height = JSProperty(\"height\")\n list = JSProperty(\"list\")\n max = JSProperty(\"max\")\n maxlength = JSProperty(\"maxlength\")\n min = JSProperty(\"min\")\n minlength = JSProperty(\"minlength\")\n multiple = JSProperty(\"multiple\")\n name = JSProperty(\"name\")\n pattern = JSProperty(\"pattern\")\n placeholder = JSProperty(\"placeholder\")\n popovertarget = JSProperty(\"popovertarget\")\n popovertargetaction = JSProperty(\"popovertargetaction\")\n readonly = JSProperty(\"readonly\")\n required = JSProperty(\"required\")\n size = JSProperty(\"size\")\n src = JSProperty(\"src\")\n step = JSProperty(\"step\")\n type = JSProperty(\"type\")\n value = JSProperty(\"value\")\n width = JSProperty(\"width\")\n\n\nclass ins(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ins\"\"\"\n\n tag = \"ins\"\n\n cite = JSProperty(\"cite\")\n datetime = JSProperty(\"datetime\")\n\n\nclass kbd(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/kbd\"\"\"\n\n tag = \"kbd\"\n\n\nclass label(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label\"\"\"\n\n tag = \"label\"\n\n for_ = JSProperty(\"for\")\n\n\nclass legend(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/legend\"\"\"\n\n tag = \"legend\"\n\n\nclass li(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/li\"\"\"\n\n tag = \"li\"\n\n value = JSProperty(\"value\")\n\n\nclass link(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link\"\"\"\n\n tag = \"link\"\n\n as_ = JSProperty(\"as\")\n crossorigin = JSProperty(\"crossorigin\")\n disabled = JSProperty(\"disabled\")\n fetchpriority = JSProperty(\"fetchpriority\")\n href = JSProperty(\"href\")\n imagesizes = JSProperty(\"imagesizes\")\n imagesrcset = JSProperty(\"imagesrcset\")\n integrity = JSProperty(\"integrity\")\n media = JSProperty(\"media\")\n rel = JSProperty(\"rel\")\n referrerpolicy = JSProperty(\"referrerpolicy\")\n sizes = JSProperty(\"sizes\")\n title = JSProperty(\"title\")\n type = JSProperty(\"type\")\n\n\nclass main(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/main\"\"\"\n\n tag = \"main\"\n\n\nclass map_(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/map\"\"\"\n\n tag = \"map\"\n\n name = JSProperty(\"name\")\n\n\nclass mark(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/mark\"\"\"\n\n tag = \"mark\"\n\n\nclass menu(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/menu\"\"\"\n\n tag = \"menu\"\n\n\nclass meter(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meter\"\"\"\n\n tag = \"meter\"\n\n form = JSProperty(\"form\")\n high = JSProperty(\"high\")\n low = JSProperty(\"low\")\n max = JSProperty(\"max\")\n min = JSProperty(\"min\")\n optimum = JSProperty(\"optimum\")\n value = JSProperty(\"value\")\n\n\nclass nav(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/nav\"\"\"\n\n tag = \"nav\"\n\n\nclass object_(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/object\"\"\"\n\n tag = \"object\"\n\n data = JSProperty(\"data\")\n form = JSProperty(\"form\")\n height = JSProperty(\"height\")\n name = JSProperty(\"name\")\n type = JSProperty(\"type\")\n usemap = JSProperty(\"usemap\")\n width = JSProperty(\"width\")\n\n\nclass ol(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ol\"\"\"\n\n tag = \"ol\"\n\n reversed = JSProperty(\"reversed\")\n start = JSProperty(\"start\")\n type = JSProperty(\"type\")\n\n\nclass optgroup(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/optgroup\"\"\"\n\n tag = \"optgroup\"\n\n disabled = JSProperty(\"disabled\")\n label = JSProperty(\"label\")\n\n\nclass option(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/option\"\"\"\n\n tag = \"option\"\n\n disabled = JSProperty(\"value\")\n label = JSProperty(\"label\")\n selected = JSProperty(\"selected\")\n value = JSProperty(\"value\")\n\n\nclass output(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/output\"\"\"\n\n tag = \"output\"\n\n for_ = JSProperty(\"for\")\n form = JSProperty(\"form\")\n name = JSProperty(\"name\")\n\n\nclass p(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/p\"\"\"\n\n tag = \"p\"\n\n\nclass picture(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/picture\"\"\"\n\n tag = \"picture\"\n\n\nclass pre(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre\"\"\"\n\n tag = \"pre\"\n\n\nclass progress(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/progress\"\"\"\n\n tag = \"progress\"\n\n max = JSProperty(\"max\")\n value = JSProperty(\"value\")\n\n\nclass q(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/q\"\"\"\n\n tag = \"q\"\n\n cite = JSProperty(\"cite\")\n\n\nclass s(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/s\"\"\"\n\n tag = \"s\"\n\n\nclass script(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script\"\"\"\n\n tag = \"script\"\n\n # Let's add async manually since it's a reserved keyword in Python\n async_ = JSProperty(\"async\")\n blocking = JSProperty(\"blocking\")\n crossorigin = JSProperty(\"crossorigin\")\n defer = JSProperty(\"defer\")\n fetchpriority = JSProperty(\"fetchpriority\")\n integrity = JSProperty(\"integrity\")\n nomodule = JSProperty(\"nomodule\")\n nonce = JSProperty(\"nonce\")\n referrerpolicy = JSProperty(\"referrerpolicy\")\n src = JSProperty(\"src\")\n type = JSProperty(\"type\")\n\n\nclass section(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/section\"\"\"\n\n tag = \"section\"\n\n\nclass select(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select\"\"\"\n\n tag = \"select\"\n\n\nclass small(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/small\"\"\"\n\n tag = \"small\"\n\n\nclass source(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/source\"\"\"\n\n tag = \"source\"\n\n media = JSProperty(\"media\")\n sizes = JSProperty(\"sizes\")\n src = JSProperty(\"src\")\n srcset = JSProperty(\"srcset\")\n type = JSProperty(\"type\")\n\n\nclass span(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/span\"\"\"\n\n tag = \"span\"\n\n\nclass strong(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/strong\"\"\"\n\n tag = \"strong\"\n\n\nclass style(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/style\"\"\"\n\n tag = \"style\"\n\n blocking = JSProperty(\"blocking\")\n media = JSProperty(\"media\")\n nonce = JSProperty(\"nonce\")\n title = JSProperty(\"title\")\n\n\nclass sub(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/sub\"\"\"\n\n tag = \"sub\"\n\n\nclass summary(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/summary\"\"\"\n\n tag = \"summary\"\n\n\nclass sup(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/sup\"\"\"\n\n tag = \"sup\"\n\n\nclass table(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/table\"\"\"\n\n tag = \"table\"\n\n\nclass tbody(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/tbody\"\"\"\n\n tag = \"tbody\"\n\n\nclass td(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/td\"\"\"\n\n tag = \"td\"\n\n colspan = JSProperty(\"colspan\")\n headers = JSProperty(\"headers\")\n rowspan = JSProperty(\"rowspan\")\n\n\nclass template(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template\"\"\"\n\n tag = \"template\"\n\n shadowrootmode = JSProperty(\"shadowrootmode\")\n\n\nclass textarea(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/textarea\"\"\"\n\n tag = \"textarea\"\n\n autocapitalize = JSProperty(\"autocapitalize\")\n autocomplete = JSProperty(\"autocomplete\")\n autofocus = JSProperty(\"autofocus\")\n cols = JSProperty(\"cols\")\n dirname = JSProperty(\"dirname\")\n disabled = JSProperty(\"disabled\")\n form = JSProperty(\"form\")\n maxlength = JSProperty(\"maxlength\")\n minlength = JSProperty(\"minlength\")\n name = JSProperty(\"name\")\n placeholder = JSProperty(\"placeholder\")\n readonly = JSProperty(\"readonly\")\n required = JSProperty(\"required\")\n rows = JSProperty(\"rows\")\n spellcheck = JSProperty(\"spellcheck\")\n wrap = JSProperty(\"wrap\")\n\n\nclass tfoot(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/tfoot\"\"\"\n\n tag = \"tfoot\"\n\n\nclass th(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/th\"\"\"\n\n tag = \"th\"\n\n\nclass thead(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/thead\"\"\"\n\n tag = \"thead\"\n\n\nclass time(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/time\"\"\"\n\n tag = \"time\"\n\n datetime = JSProperty(\"datetime\")\n\n\nclass title(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/title\"\"\"\n\n tag = \"title\"\n\n\nclass tr(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/tr\"\"\"\n\n tag = \"tr\"\n\n abbr = JSProperty(\"abbr\")\n colspan = JSProperty(\"colspan\")\n headers = JSProperty(\"headers\")\n rowspan = JSProperty(\"rowspan\")\n scope = JSProperty(\"scope\")\n\n\nclass track(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/track\"\"\"\n\n tag = \"track\"\n\n default = JSProperty(\"default\")\n kind = JSProperty(\"kind\")\n label = JSProperty(\"label\")\n src = JSProperty(\"src\")\n srclang = JSProperty(\"srclang\")\n\n\nclass u(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/u\"\"\"\n\n tag = \"u\"\n\n\nclass ul(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ul\"\"\"\n\n tag = \"ul\"\n\n\nclass var(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/var\"\"\"\n\n tag = \"var\"\n\n\nclass video(TextElementBase):\n \"\"\"Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video\"\"\"\n\n tag = \"video\"\n\n autoplay = JSProperty(\"autoplay\")\n controls = JSProperty(\"controls\")\n crossorigin = JSProperty(\"crossorigin\")\n disablepictureinpicture = JSProperty(\"disablepictureinpicture\")\n disableremoteplayback = JSProperty(\"disableremoteplayback\")\n height = JSProperty(\"height\")\n loop = JSProperty(\"loop\")\n muted = JSProperty(\"muted\")\n playsinline = JSProperty(\"playsinline\")\n poster = JSProperty(\"poster\")\n preload = JSProperty(\"preload\")\n src = JSProperty(\"src\")\n width = JSProperty(\"width\")\n\n\n# Custom Elements\nclass grid(TextElementBase):\n tag = \"div\"\n\n def __init__(self, layout, content=None, gap=None, **kwargs):\n super().__init__(content, **kwargs)\n self.style[\"display\"] = \"grid\"\n self.style[\"grid-template-columns\"] = layout\n\n # TODO: This should be a property\n if not gap is None:\n self.style[\"gap\"] = gap\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",
17
17
  "media.py": "from pyodide.ffi import to_js\nfrom pyscript import window\n\n\nclass Device:\n \"\"\"Device represents a media input or output device, such as a microphone,\n camera, or headset.\n \"\"\"\n\n def __init__(self, device):\n self._js = device\n\n @property\n def id(self):\n return self._js.deviceId\n\n @property\n def group(self):\n return self._js.groupId\n\n @property\n def kind(self):\n return self._js.kind\n\n @property\n def label(self):\n return self._js.label\n\n def __getitem__(self, key):\n return getattr(self, key)\n\n @classmethod\n async def load(cls, audio=False, video=True):\n \"\"\"Load the device stream.\"\"\"\n options = window.Object.new()\n options.audio = audio\n if isinstance(video, bool):\n options.video = video\n else:\n # TODO: Think this can be simplified but need to check it on the pyodide side\n\n # TODO: this is pyodide specific. shouldn't be!\n options.video = window.Object.new()\n for k in video:\n setattr(\n options.video,\n k,\n to_js(video[k], dict_converter=window.Object.fromEntries),\n )\n\n stream = await window.navigator.mediaDevices.getUserMedia(options)\n return stream\n\n async def get_stream(self):\n key = self.kind.replace(\"input\", \"\").replace(\"output\", \"\")\n options = {key: {\"deviceId\": {\"exact\": self.id}}}\n\n return await self.load(**options)\n\n\nasync def list_devices() -> list[dict]:\n \"\"\"\n Return the list of the currently available media input and output devices,\n such as microphones, cameras, headsets, and so forth.\n\n Output:\n\n list(dict) - list of dictionaries representing the available media devices.\n Each dictionary has the following keys:\n * deviceId: a string that is an identifier for the represented device\n that is persisted across sessions. It is un-guessable by other\n applications and unique to the origin of the calling application.\n It is reset when the user clears cookies (for Private Browsing, a\n different identifier is used that is not persisted across sessions).\n\n * groupId: a string that is a group identifier. Two devices have the same\n group identifier if they belong to the same physical device — for\n example a monitor with both a built-in camera and a microphone.\n\n * kind: an enumerated value that is either \"videoinput\", \"audioinput\"\n or \"audiooutput\".\n\n * label: a string describing this device (for example \"External USB\n Webcam\").\n\n Note: the returned list will omit any devices that are blocked by the document\n Permission Policy: microphone, camera, speaker-selection (for output devices),\n and so on. Access to particular non-default devices is also gated by the\n Permissions API, and the list will omit devices for which the user has not\n granted explicit permission.\n \"\"\"\n # https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/enumerateDevices\n return [\n Device(obj) for obj in await window.navigator.mediaDevices.enumerateDevices()\n ]\n"
18
18
  },
19
- "websocket.py": "import js\nfrom pyscript.util import as_bytearray\n\ncode = \"code\"\nprotocols = \"protocols\"\nreason = \"reason\"\n\n\nclass EventMessage:\n def __init__(self, event):\n self._event = event\n\n def __getattr__(self, attr):\n value = getattr(self._event, attr)\n\n if attr == \"data\" and not isinstance(value, str):\n if hasattr(value, \"to_py\"):\n return value.to_py()\n # shims in MicroPython\n return memoryview(as_bytearray(value))\n\n return value\n\n\nclass WebSocket(object):\n CONNECTING = 0\n OPEN = 1\n CLOSING = 2\n CLOSED = 3\n\n def __init__(self, **kw):\n url = kw[\"url\"]\n if protocols in kw:\n socket = js.WebSocket.new(url, kw[protocols])\n else:\n socket = js.WebSocket.new(url)\n object.__setattr__(self, \"_ws\", socket)\n\n for t in [\"onclose\", \"onerror\", \"onmessage\", \"onopen\"]:\n if t in kw:\n socket[t] = kw[t]\n\n def __getattr__(self, attr):\n return getattr(self._ws, attr)\n\n def __setattr__(self, attr, value):\n if attr == \"onmessage\":\n self._ws[attr] = lambda e: value(EventMessage(e))\n else:\n self._ws[attr] = value\n\n def close(self, **kw):\n if code in kw and reason in kw:\n self._ws.close(kw[code], kw[reason])\n elif code in kw:\n self._ws.close(kw[code])\n else:\n self._ws.close()\n\n def send(self, data):\n if isinstance(data, str):\n self._ws.send(data)\n else:\n buffer = js.Uint8Array.new(len(data))\n for pos, b in enumerate(data):\n buffer[pos] = b\n self._ws.send(buffer)\n",
20
- "workers.py": "import js as _js\n\nfrom polyscript import workers as _workers\n\n_get = _js.Reflect.get\n\n# this solves an inconsistency between Pyodide and MicroPython\n# @see https://github.com/pyscript/pyscript/issues/2106\nclass _ReadOnlyProxy:\n def __getitem__(self, name):\n return _get(_workers, name)\n def __getattr__(self, name):\n return _get(_workers, name)\n\nworkers = _ReadOnlyProxy()\n"
19
+ "websocket.py": "import js\nfrom pyscript.util import as_bytearray\n\ncode = \"code\"\nprotocols = \"protocols\"\nreason = \"reason\"\n\n\nclass EventMessage:\n def __init__(self, event):\n self._event = event\n\n def __getattr__(self, attr):\n value = getattr(self._event, attr)\n\n if attr == \"data\" and not isinstance(value, str):\n if hasattr(value, \"to_py\"):\n return value.to_py()\n # shims in MicroPython\n return memoryview(as_bytearray(value))\n\n return value\n\n\nclass WebSocket(object):\n CONNECTING = 0\n OPEN = 1\n CLOSING = 2\n CLOSED = 3\n\n def __init__(self, **kw):\n url = kw[\"url\"]\n if protocols in kw:\n socket = js.WebSocket.new(url, kw[protocols])\n else:\n socket = js.WebSocket.new(url)\n object.__setattr__(self, \"_ws\", socket)\n\n for t in [\"onclose\", \"onerror\", \"onmessage\", \"onopen\"]:\n if t in kw:\n socket[t] = kw[t]\n\n def __getattr__(self, attr):\n return getattr(self._ws, attr)\n\n def __setattr__(self, attr, value):\n if attr == \"onmessage\":\n self._ws[attr] = lambda e: value(EventMessage(e))\n else:\n self._ws[attr] = value\n\n def close(self, **kw):\n if code in kw and reason in kw:\n self._ws.close(kw[code], kw[reason])\n elif code in kw:\n self._ws.close(kw[code])\n else:\n self._ws.close()\n\n def send(self, data):\n if isinstance(data, str):\n self._ws.send(data)\n else:\n buffer = js.Uint8Array.new(len(data))\n for pos, b in enumerate(data):\n buffer[pos] = b\n self._ws.send(buffer)\n"
21
20
  },
22
21
  "pyweb": {
23
22
  "__init__.py": "from .pydom import JSProperty\nfrom .pydom import dom as pydom\n",
package/types/config.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export default configs;
2
- declare const configs: Map<any, any>;
1
+ export const configs: Map<any, any>;
2
+ export function relative_url(url: any, base?: string): string;
package/types/core.d.ts CHANGED
@@ -3,6 +3,7 @@ import { stdlib } from "./stdlib.js";
3
3
  import { optional } from "./stdlib.js";
4
4
  import { inputFailure } from "./hooks.js";
5
5
  import TYPES from "./types.js";
6
+ import { relative_url } from "./config.js";
6
7
  /**
7
8
  * A `Worker` facade able to bootstrap on the worker thread only a PyScript module.
8
9
  * @param {string} file the python file to run ina worker.
@@ -54,4 +55,4 @@ declare const exportedHooks: {
54
55
  };
55
56
  declare const exportedConfig: {};
56
57
  declare const exportedWhenDefined: (type: string) => Promise<any>;
57
- export { stdlib, optional, inputFailure, TYPES, exportedPyWorker as PyWorker, exportedMPWorker as MPWorker, exportedHooks as hooks, exportedConfig as config, exportedWhenDefined as whenDefined };
58
+ export { stdlib, optional, inputFailure, TYPES, relative_url, exportedPyWorker as PyWorker, exportedMPWorker as MPWorker, exportedHooks as hooks, exportedConfig as config, exportedWhenDefined as whenDefined };
@@ -16,7 +16,6 @@ declare namespace _default {
16
16
  "media.py": string;
17
17
  };
18
18
  "websocket.py": string;
19
- "workers.py": string;
20
19
  };
21
20
  let pyweb: {
22
21
  "__init__.py": string;