From 8d24a7bd26a868464c0a0c766a952efd4e8aa234 Mon Sep 17 00:00:00 2001 From: webreflection Date: Wed, 6 May 2026 11:54:07 +0200 Subject: [PATCH 1/5] PyEditor changes / improvements --- core/package-lock.json | 4 ++-- core/package.json | 2 +- core/src/plugins/py-editor.js | 28 ++++++++++++++++++---------- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/core/package-lock.json b/core/package-lock.json index 1ba6ccb3e68..d4b639fdd7e 100644 --- a/core/package-lock.json +++ b/core/package-lock.json @@ -1,12 +1,12 @@ { "name": "@pyscript/core", - "version": "0.7.18", + "version": "0.7.23", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@pyscript/core", - "version": "0.7.18", + "version": "0.7.23", "license": "APACHE-2.0", "dependencies": { "@ungap/with-resolvers": "^0.1.0", diff --git a/core/package.json b/core/package.json index e6844e85633..af10b16858b 100644 --- a/core/package.json +++ b/core/package.json @@ -1,6 +1,6 @@ { "name": "@pyscript/core", - "version": "0.7.18", + "version": "0.7.23", "type": "module", "description": "PyScript", "module": "./index.js", diff --git a/core/src/plugins/py-editor.js b/core/src/plugins/py-editor.js index 9fc326a21a5..7e6f86ede8f 100644 --- a/core/src/plugins/py-editor.js +++ b/core/src/plugins/py-editor.js @@ -38,7 +38,7 @@ const getRelatedScript = (target, type) => { return editor?.parentNode?.previousElementSibling; }; -async function execute({ currentTarget, script }) { +async function execute({ currentTarget, script }, onBeforeRun = '') { const { env, pySrc, outDiv } = this; const hasRunButton = !!currentTarget; @@ -152,7 +152,8 @@ async function execute({ currentTarget, script }) { console.error(str); } }; - sync.runAsync(pySrc).then(enable, enable); + if (onBeforeRun) onBeforeRun += ';'; + sync.runAsync(onBeforeRun + pySrc).then(enable, enable); }); } @@ -176,11 +177,11 @@ const makeRunButton = (handler, type) => { runButton.innerHTML = RUN_BUTTON; runButton.setAttribute("aria-label", "Python Script Run Button"); runButton.addEventListener("click", async (event) => { + const script = getRelatedScript(runButton, type); if ( runButton.classList.contains("running") && confirm("Stop evaluating this code?") ) { - const script = getRelatedScript(runButton, type); if (script) { const env = script.getAttribute("env"); // remove the bootstrapped env which could be one or shared @@ -205,7 +206,7 @@ const makeRunButton = (handler, type) => { return; } runButton.blur(); - await handler.handleEvent(event); + await handler.handleEvent(event, script?.getAttribute("onbeforerun") || ""); }); return runButton; }; @@ -276,7 +277,9 @@ const init = async (script, type, interpreter) => { }).onmessage = ({ target }) => target.terminate(); } - if (hasConfig && configs.has(env)) { + // allow bootstrap with same env for repeated editor creation + // only if `env-override` is explicitly set as attribute + if (hasConfig && configs.has(env) && !script.hasAttribute("env-override")) { throw new SyntaxError( configs.get(env) ? `duplicated config for env: ${env}` @@ -328,7 +331,7 @@ const init = async (script, type, interpreter) => { // in every other case be sure that if the listener override returned // `false` nothing happens, otherwise keep doing what it always did else { - context.handleEvent = async (event) => { + context.handleEvent = async (event, onBeforeRun) => { // trap the currentTarget ASAP (if any) // otherwise it gets lost asynchronously const { currentTarget } = event; @@ -338,7 +341,7 @@ const init = async (script, type, interpreter) => { }); // avoid executing the default handler if the override returned `false` if ((await callback(event)) !== false) - await execute.call(context, { currentTarget }); + await execute.call(context, { currentTarget }, onBeforeRun); }; } }, @@ -388,8 +391,7 @@ const init = async (script, type, interpreter) => { }; if (isSetup) { - await context.handleEvent({ currentTarget: null, script }); - notifyEditor(); + context.handleEvent({ currentTarget: null, script }).then(notifyEditor); return; } @@ -417,7 +419,13 @@ const init = async (script, type, interpreter) => { const inputChild = boxDiv.querySelector(`.${type}-editor-input > div`); const parent = inputChild.attachShadow({ mode: "open" }); // avoid inheriting styles from the outer component - parent.innerHTML = ``; + const styles = [':host { all: initial; }']; + const rows = script.getAttribute("rows"); + if (rows) { + const maxHeight = Math.floor(parseInt(rows) * 18.5) + 'px'; + styles.push(`.cm-editor { height: auto; max-height: ${maxHeight}; }`); + } + parent.innerHTML = ``; target.appendChild(boxDiv); From 290b622323efc1d6bdfa6111d351120541774f28 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 6 May 2026 14:06:22 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- core/src/plugins/py-editor.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/core/src/plugins/py-editor.js b/core/src/plugins/py-editor.js index 7e6f86ede8f..9583b2bb833 100644 --- a/core/src/plugins/py-editor.js +++ b/core/src/plugins/py-editor.js @@ -38,7 +38,7 @@ const getRelatedScript = (target, type) => { return editor?.parentNode?.previousElementSibling; }; -async function execute({ currentTarget, script }, onBeforeRun = '') { +async function execute({ currentTarget, script }, onBeforeRun = "") { const { env, pySrc, outDiv } = this; const hasRunButton = !!currentTarget; @@ -152,7 +152,7 @@ async function execute({ currentTarget, script }, onBeforeRun = '') { console.error(str); } }; - if (onBeforeRun) onBeforeRun += ';'; + if (onBeforeRun) onBeforeRun += ";"; sync.runAsync(onBeforeRun + pySrc).then(enable, enable); }); } @@ -206,7 +206,10 @@ const makeRunButton = (handler, type) => { return; } runButton.blur(); - await handler.handleEvent(event, script?.getAttribute("onbeforerun") || ""); + await handler.handleEvent( + event, + script?.getAttribute("onbeforerun") || "", + ); }); return runButton; }; @@ -341,7 +344,11 @@ const init = async (script, type, interpreter) => { }); // avoid executing the default handler if the override returned `false` if ((await callback(event)) !== false) - await execute.call(context, { currentTarget }, onBeforeRun); + await execute.call( + context, + { currentTarget }, + onBeforeRun, + ); }; } }, @@ -419,10 +426,10 @@ const init = async (script, type, interpreter) => { const inputChild = boxDiv.querySelector(`.${type}-editor-input > div`); const parent = inputChild.attachShadow({ mode: "open" }); // avoid inheriting styles from the outer component - const styles = [':host { all: initial; }']; + const styles = [":host { all: initial; }"]; const rows = script.getAttribute("rows"); if (rows) { - const maxHeight = Math.floor(parseInt(rows) * 18.5) + 'px'; + const maxHeight = Math.floor(parseInt(rows) * 18.5) + "px"; styles.push(`.cm-editor { height: auto; max-height: ${maxHeight}; }`); } parent.innerHTML = ``; From c2dd1f23d8625b255b8d1bc0aa246d7d799703e1 Mon Sep 17 00:00:00 2001 From: webreflection Date: Wed, 6 May 2026 18:16:15 +0200 Subject: [PATCH 3/5] cleanup the env if forced --- core/src/plugins/py-editor.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/core/src/plugins/py-editor.js b/core/src/plugins/py-editor.js index 9583b2bb833..de5c51c40db 100644 --- a/core/src/plugins/py-editor.js +++ b/core/src/plugins/py-editor.js @@ -282,12 +282,19 @@ const init = async (script, type, interpreter) => { // allow bootstrap with same env for repeated editor creation // only if `env-override` is explicitly set as attribute - if (hasConfig && configs.has(env) && !script.hasAttribute("env-override")) { - throw new SyntaxError( - configs.get(env) - ? `duplicated config for env: ${env}` - : `unable to add a config to the env: ${env}`, - ); + if (hasConfig && configs.has(env)) { + if (script.hasAttribute("env-override")) { + // in this case we need to bootstrap the env again + // because otherwise each env would leak + envs.delete(env); + } + else { + throw new SyntaxError( + configs.get(env) + ? `duplicated config for env: ${env}` + : `unable to add a config to the env: ${env}`, + ); + } } configs.set(env, hasConfig); From 90e3260aa24e4a330cace1da842fadcb65e39870 Mon Sep 17 00:00:00 2001 From: webreflection Date: Thu, 7 May 2026 16:58:33 +0200 Subject: [PATCH 4/5] added a way to specify a print target output --- core/package-lock.json | 4 ++-- core/package.json | 2 +- core/src/plugins/py-editor.js | 7 ++++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/core/package-lock.json b/core/package-lock.json index d4b639fdd7e..aaf34cbee24 100644 --- a/core/package-lock.json +++ b/core/package-lock.json @@ -1,12 +1,12 @@ { "name": "@pyscript/core", - "version": "0.7.23", + "version": "0.7.24", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@pyscript/core", - "version": "0.7.23", + "version": "0.7.24", "license": "APACHE-2.0", "dependencies": { "@ungap/with-resolvers": "^0.1.0", diff --git a/core/package.json b/core/package.json index af10b16858b..5139b6b4072 100644 --- a/core/package.json +++ b/core/package.json @@ -1,6 +1,6 @@ { "name": "@pyscript/core", - "version": "0.7.23", + "version": "0.7.24", "type": "module", "description": "PyScript", "module": "./index.js", diff --git a/core/src/plugins/py-editor.js b/core/src/plugins/py-editor.js index de5c51c40db..2a90c8a6757 100644 --- a/core/src/plugins/py-editor.js +++ b/core/src/plugins/py-editor.js @@ -239,12 +239,13 @@ const makeOutDiv = (type) => { return outDiv; }; -const makeBoxDiv = (handler, type) => { +const makeBoxDiv = (handler, type, output) => { const boxDiv = document.createElement("div"); boxDiv.className = `${type}-editor-box`; const editorDiv = makeEditorDiv(handler, type); - const outDiv = makeOutDiv(type); + const outDiv = output ? document.getElementById(output) : makeOutDiv(type); + if (output) outDiv.classList.add(`${type}-editor-output`); boxDiv.append(editorDiv, outDiv); return [boxDiv, outDiv, editorDiv.querySelector("button")]; @@ -427,7 +428,7 @@ const init = async (script, type, interpreter) => { if (!target.hasAttribute("root")) target.setAttribute("root", target.id); // @see https://github.com/JeffersGlass/mkdocs-pyscript/blob/main/mkdocs_pyscript/js/makeblocks.js - const [boxDiv, outDiv, runButton] = makeBoxDiv(context, type); + const [boxDiv, outDiv, runButton] = makeBoxDiv(context, type, script.getAttribute("output")); boxDiv.dataset.env = script.hasAttribute("env") ? env : interpreter; const inputChild = boxDiv.querySelector(`.${type}-editor-input > div`); From ad99087e70a2133e0f831b80ce06be0b4a73b834 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 7 May 2026 14:59:37 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- core/src/plugins/py-editor.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/core/src/plugins/py-editor.js b/core/src/plugins/py-editor.js index 2a90c8a6757..71c15f31904 100644 --- a/core/src/plugins/py-editor.js +++ b/core/src/plugins/py-editor.js @@ -288,8 +288,7 @@ const init = async (script, type, interpreter) => { // in this case we need to bootstrap the env again // because otherwise each env would leak envs.delete(env); - } - else { + } else { throw new SyntaxError( configs.get(env) ? `duplicated config for env: ${env}` @@ -428,7 +427,11 @@ const init = async (script, type, interpreter) => { if (!target.hasAttribute("root")) target.setAttribute("root", target.id); // @see https://github.com/JeffersGlass/mkdocs-pyscript/blob/main/mkdocs_pyscript/js/makeblocks.js - const [boxDiv, outDiv, runButton] = makeBoxDiv(context, type, script.getAttribute("output")); + const [boxDiv, outDiv, runButton] = makeBoxDiv( + context, + type, + script.getAttribute("output"), + ); boxDiv.dataset.env = script.hasAttribute("env") ? env : interpreter; const inputChild = boxDiv.querySelector(`.${type}-editor-input > div`);