From c93a98a15fe22186fbc36de16005552673353766 Mon Sep 17 00:00:00 2001 From: Mateusz Paprocki Date: Thu, 14 Apr 2022 10:23:03 +0200 Subject: [PATCH 1/3] Allow to indent blocks --- pyscriptjs/src/components/pyscript.ts | 39 ++++++++++++++++++++------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/pyscriptjs/src/components/pyscript.ts b/pyscriptjs/src/components/pyscript.ts index 80cfcbe..c435e3f 100644 --- a/pyscriptjs/src/components/pyscript.ts +++ b/pyscriptjs/src/components/pyscript.ts @@ -250,16 +250,37 @@ export class PyScript extends HTMLElement { let pyodide = await pyodideReadyPromise; // debugger try { - // @ts-ignore - let source = htmlDecode(this.editor.state.doc.toString()); - let output; - if (source.includes("asyncio")){ - output = await pyodide.runPythonAsync(source); - }else{ - output = pyodide.runPython(source); + function ltrim(code: string): string { + const lines = code.split("\n") + if (lines.length == 0) + return code + + const lengths = lines + .filter((line) => line.trim().length != 0) + .map((line) => { + const [prefix] = line.match(/^\s*/) + return prefix.length + }) + + const k = Math.min(...lengths) + + if (k != 0) + return lines.map((line) => line.substring(k)).join("\n") + else + return code } - if (output !== undefined){ - this.addToOutput(output); + + const str = this.editor.state.doc.toString() + const source = htmlDecode(ltrim(str)) + + let output + if (source.includes("asyncio")) + output = await pyodide.runPythonAsync(source) + else + output = pyodide.runPython(source) + + if (output !== undefined) { + this.addToOutput(output) } if (this.hasAttribute('auto-generate') && this.parentElement.lastChild === this) { From 0dc3b728c33d8929769fc93bf3b891f58eb7d08b Mon Sep 17 00:00:00 2001 From: Mateusz Paprocki Date: Sat, 16 Apr 2022 10:38:02 +0200 Subject: [PATCH 2/3] Allow `import d3` instead of `from esm import d3` --- pyscriptjs/examples/d3.html | 2 +- pyscriptjs/src/components/pyscript.ts | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pyscriptjs/examples/d3.html b/pyscriptjs/examples/d3.html index fcae36f..cf67ab7 100644 --- a/pyscriptjs/examples/d3.html +++ b/pyscriptjs/examples/d3.html @@ -109,7 +109,7 @@ for (const d of data) { from pyodide import create_proxy, to_js -from esm import d3 +import d3 fruits = [ dict(name="🍊", count=21), diff --git a/pyscriptjs/src/components/pyscript.ts b/pyscriptjs/src/components/pyscript.ts index c3df6b0..8fc626c 100644 --- a/pyscriptjs/src/components/pyscript.ts +++ b/pyscriptjs/src/components/pyscript.ts @@ -247,8 +247,6 @@ export class PyScript extends HTMLElement { } protected async _register_esm(pyodide: PyodideInterface): Promise { - const imports: {[key: string]: unknown} = {} - for (const node of document.querySelectorAll("script[type='importmap']")) { const importmap = (() => { try { @@ -265,17 +263,19 @@ export class PyScript extends HTMLElement { if (typeof name != "string" || typeof url != "string") continue + let exports: object try { // XXX: pyodide doesn't like Module(), failing with // "can't read 'name' of undefined" at import time - imports[name] = {...await import(url)} + exports = {...await import(url)} } catch { - console.error(`failed to fetch '${url}' for '${name}'`) + console.warn(`failed to fetch '${url}' for '${name}'`) + continue } + + pyodide.registerJsModule(name, exports) } } - - pyodide.registerJsModule("esm", imports) } async evaluate(): Promise { From 51c2efd6015cc611cbe306495ccf3b7a6bfc0c26 Mon Sep 17 00:00:00 2001 From: Sophia Yang <39501859+sophiamyang@users.noreply.github.com> Date: Sat, 16 Apr 2022 21:07:44 -0500 Subject: [PATCH 3/3] Update README.md fixed two typos --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3fb650f..be16ae4 100644 --- a/README.md +++ b/README.md @@ -19,8 +19,8 @@ To try PyScript, import the pyscript to your html page with: ``` At that point, you can then use PyScript components in your html page. PyScript currently implements the following elements: -* ``: that can be used to define python code that is execute withing the web page. The element itself is not rendered to the page and only used to add logic -* ``: creates a REPL component that is rendered to the page as a code editor and allows users to right code that can be executed +* ``: that can be used to define python code that is executable within the web page. The element itself is not rendered to the page and only used to add logic +* ``: creates a REPL component that is rendered to the page as a code editor and allows users to write code that can be executed Check out the `/examples` folder for more examples on how to use it, all you need to do is open them in Chrome.