From f6dfc5361e0e611e588a66944d60ea73b3297055 Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Thu, 22 Jun 2023 17:29:07 +0200 Subject: [PATCH] Implement PyScript custom + + + + + - + - + - + - + +
diff --git a/pyscript.core/test/micropython.html b/pyscript.core/test/micropython.html index e94794b7..205001f3 100644 --- a/pyscript.core/test/micropython.html +++ b/pyscript.core/test/micropython.html @@ -6,7 +6,7 @@ python - + - + diff --git a/pyscript.core/test/plugins/index.html b/pyscript.core/test/plugins/index.html index 4a23d509..99e24815 100644 --- a/pyscript.core/test/plugins/index.html +++ b/pyscript.core/test/plugins/index.html @@ -10,7 +10,7 @@ } + - +
+ + Something something about something ... +
[[fetch]] from = "../" to_folder = "./" files = ["a.py", "b.py"] + + import js import a, b @@ -29,8 +35,24 @@ 'Hello Web!' + # note the target is this element itself + display('second <py-script>') + + + # note this is late to the party simply because + # pyodide needs to be bootstrapped in the Worker too XWorker('../a.py') 'OK' + + + + diff --git a/pyscript.core/test/plugins/py-script.js b/pyscript.core/test/plugins/py-script.js deleted file mode 100644 index 8b05d7b2..00000000 --- a/pyscript.core/test/plugins/py-script.js +++ /dev/null @@ -1,72 +0,0 @@ -import { define } from "@pyscript/core"; - -// append ASAP CSS to avoid showing content -document.head.appendChild(document.createElement("style")).textContent = ` - py-script, py-config { - display: none; - } -`; - -// create a unique identifier when/if needed -let id = 0; -const getID = (prefix = "py-script") => `${prefix}-${id++}`; - -let bootstrap = true, - XWorker, - sharedRuntime; -const sharedPyodide = new Promise((resolve) => { - const pyConfig = document.querySelector("py-config"); - const config = pyConfig?.getAttribute("src") || pyConfig?.textContent; - define("py", { - config, - interpreter: "pyodide", - codeBeforeRunWorker: `print('codeBeforeRunWorker')`, - codeAfterRunWorker: `print('codeAfterRunWorker')`, - onBeforeRun(pyodide, node) { - pyodide.interpreter.globals.set("XWorker", XWorker); - console.log("onBeforeRun", sharedRuntime === pyodide, node); - }, - onAfterRun(pyodide, node) { - console.log("onAfterRun", sharedRuntime === pyodide, node); - }, - async onRuntimeReady(pyodide) { - // bootstrap the shared runtime once - // as each node as plugin gets onRuntimeReady called once - // because no custom-element is strictly needed - if (bootstrap) { - bootstrap = false; - sharedRuntime = pyodide; - XWorker = pyodide.XWorker; - pyodide.io.stdout = (message) => { - console.log("🐍", pyodide.type, message); - }; - // do any module / JS injection in here such as - // Element, display, and friends ... then: - resolve(pyodide); - } - }, - }); -}); - -/** @type {WeakSet} */ -const known = new WeakSet(); - -class PyScriptElement extends HTMLElement { - constructor() { - if (!super().id) this.id = getID(); - } - async connectedCallback() { - if (!known.has(this)) { - known.add(this); - // sharedPyodide contains various helpers including run and runAsync - const { run } = await sharedPyodide; - // do any stuff needed to finalize this element bootstrap - // (i.e. check src attribute and so on) - this.replaceChildren(run(this.textContent) || ""); - // reveal the node on the page - this.style.display = "block"; - } - } -} - -customElements.define("py-script", PyScriptElement); diff --git a/pyscript.core/test/py-events.html b/pyscript.core/test/py-events.html index 836c2086..89474441 100644 --- a/pyscript.core/test/py-events.html +++ b/pyscript.core/test/py-events.html @@ -6,7 +6,7 @@ python events - + - + - + + diff --git a/pyscript.core/test/test.html b/pyscript.core/test/test.html index e74fefef..e9e0b2e6 100644 --- a/pyscript.core/test/test.html +++ b/pyscript.core/test/test.html @@ -9,8 +9,9 @@ { "imports": { "basic-devtools": "../node_modules/basic-devtools/esm/index.js", - "coincident/structured": "../node_modules/coincident/structured.js", - "@ungap/with-resolvers": "../node_modules/@ungap/with-resolvers/index.js" + "coincident/window": "../node_modules/coincident/window.js", + "@ungap/with-resolvers": "../node_modules/@ungap/with-resolvers/index.js", + "@ungap/structured-clone/json": "../node_modules/@ungap/structured-clone/esm/json.js" } } diff --git a/pyscript.core/test/wasmoon.html b/pyscript.core/test/wasmoon.html index 3da2551e..a2ea615d 100644 --- a/pyscript.core/test/wasmoon.html +++ b/pyscript.core/test/wasmoon.html @@ -9,8 +9,9 @@ { "imports": { "basic-devtools": "../node_modules/basic-devtools/esm/index.js", - "coincident/structured": "../node_modules/coincident/structured.js", - "@ungap/with-resolvers": "../node_modules/@ungap/with-resolvers/index.js" + "coincident/window": "../node_modules/coincident/window.js", + "@ungap/with-resolvers": "../node_modules/@ungap/with-resolvers/index.js", + "@ungap/structured-clone/json": "../node_modules/@ungap/structured-clone/esm/json.js" } } diff --git a/pyscript.core/test/worker/index.html b/pyscript.core/test/worker/index.html index 6bd8d3b1..eb5d882d 100644 --- a/pyscript.core/test/worker/index.html +++ b/pyscript.core/test/worker/index.html @@ -8,8 +8,9 @@ { "imports": { "basic-devtools": "../../node_modules/basic-devtools/esm/index.js", - "coincident/structured": "../../node_modules/coincident/structured.js", + "coincident/window": "../../node_modules/coincident/window.js", "@ungap/with-resolvers": "../../node_modules/@ungap/with-resolvers/index.js", + "@ungap/structured-clone/json": "../../node_modules/@ungap/structured-clone/esm/json.js", "@pyscript/core": "../../esm/index.js" } } diff --git a/pyscript.core/test/worker/input.html b/pyscript.core/test/worker/input.html index a0282e42..53af9dc0 100644 --- a/pyscript.core/test/worker/input.html +++ b/pyscript.core/test/worker/input.html @@ -8,8 +8,9 @@ { "imports": { "basic-devtools": "../../node_modules/basic-devtools/esm/index.js", - "coincident/structured": "../../node_modules/coincident/structured.js", + "coincident/window": "../../node_modules/coincident/window.js", "@ungap/with-resolvers": "../../node_modules/@ungap/with-resolvers/index.js", + "@ungap/structured-clone/json": "../../node_modules/@ungap/structured-clone/esm/json.js", "@pyscript/core": "../../esm/index.js" } }