diff --git a/lib/pyscript.typ b/lib/pyscript.typ index 4c546e4..d05b3d2 100644 --- a/lib/pyscript.typ +++ b/lib/pyscript.typ @@ -2,15 +2,26 @@ #let state-pyscript-version = state("state-pyscript-version", none) #let state-pyscript-headers = state("state-pyscript-headers", (:)) -#let pyscript(body) = { +#let pyscript( + repl: false, + body +) = { show raw.where(block: true, lang: "python"): it => { state-use-pyscript.update(x => true) it - html.elem("script", attrs: ( - type: "py", - terminal: "", - worker: "", - ), it.text) + let script = it.text; + if repl { + script = "import code\n" + script + "\ncode.interact(local=globals())" + } + html.elem( + "script", + attrs: ( + type: "py", + terminal: "", + worker: "", + ), + script + ) } body } diff --git a/test_template/main.typ b/test_template/main.typ index cf28bab..1088745 100644 --- a/test_template/main.typ +++ b/test_template/main.typ @@ -63,9 +63,10 @@ // Pyscript: pyscript-headers: ( "remote-2026.3.1": { + html.elem("script", attrs: (src: "./mini-coi.js")) html.elem("script", attrs: (type: "module", src: "https://pyscript.net/releases/2026.3.1/core.js")) - html.elem("link", attrs: (rel: "stylesheet", href: "https://pyscript.net/releases/2026.3.1/core.js")) - } + html.elem("link", attrs: (rel: "stylesheet", href: "https://pyscript.net/releases/2026.3.1/core.css")) + }, ), pyscript-version: "remote-2026.3.1", ) @@ -86,6 +87,22 @@ print(i) ``` ] +#pyscript(repl: true)[ + ```python + ret = [] + for i in range(16, 31): + if i % 3 == 0 and i % 5 == 0: + ret.append("plopliplop") + elif i % 3 == 0: + ret.append("plop") + elif i % 5 == 0: + ret.append("plip") + else: + ret.append(i) + # >>> print(ret) + # [16, 17, 'plop', 19, 'plip', 'plop', 22, 23, 'plop', 'plip', 26, 'plop', 28, 29, 'plopliplop'] + ``` +] #summ.card diff --git a/test_template/mini-coi.js b/test_template/mini-coi.js new file mode 100644 index 0000000..dcf8870 --- /dev/null +++ b/test_template/mini-coi.js @@ -0,0 +1,32 @@ +/*! coi-serviceworker v0.1.7 - Guido Zuidhof and contributors, licensed under MIT */ +/*! mini-coi - Andrea Giammarchi and contributors, licensed under MIT */ +/** FEATURE DETECTION VERSION - COMPATIBLE WITH SERVERS THAT DO NOT SUPPORT COI */ +(({ document: d, navigator: { serviceWorker: s } }) => { + if (d) { + try { new SharedArrayBuffer(4, { maxByteLength: 8 }) } + catch (_) { + const { currentScript: c } = d; + s.register(c.src, { scope: c.getAttribute('scope') || '.' }).then(r => { + r.addEventListener('updatefound', () => location.reload()); + if (r.active && !s.controller) location.reload(); + }); + } + } + else { + addEventListener('install', () => skipWaiting()); + addEventListener('activate', e => e.waitUntil(clients.claim())); + addEventListener('fetch', e => { + const { request: r } = e; + if (r.cache === 'only-if-cached' && r.mode !== 'same-origin') return; + e.respondWith(fetch(r).then(r => { + const { body, status, statusText } = r; + if (!status || status > 399) return r; + const h = new Headers(r.headers); + h.set('Cross-Origin-Opener-Policy', 'same-origin'); + h.set('Cross-Origin-Embedder-Policy', 'require-corp'); + h.set('Cross-Origin-Resource-Policy', 'cross-origin'); + return new Response(status == 204 ? null : body, { status, statusText, headers: h }); + })); + }); + } +})(self);