diff --git a/pyscriptjs/src/components/pyscript.ts b/pyscriptjs/src/components/pyscript.ts index 30aef4ae..c3df6b02 100644 --- a/pyscriptjs/src/components/pyscript.ts +++ b/pyscriptjs/src/components/pyscript.ts @@ -288,16 +288,37 @@ export class PyScript extends HTMLElement { await this._register_esm(pyodide) // debugger try { - // @ts-ignore - const 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) {