mirror of
https://github.com/pyscript/pyscript.git
synced 2022-05-01 19:47:48 +03:00
Merge pull request #20 from anaconda/mattpap/allow_pyscript_block_offset
Allow to indent `<py-script>` blocks
This commit is contained in:
@@ -288,16 +288,37 @@ export class PyScript extends HTMLElement {
|
|||||||
await this._register_esm(pyodide)
|
await this._register_esm(pyodide)
|
||||||
// debugger
|
// debugger
|
||||||
try {
|
try {
|
||||||
// @ts-ignore
|
function ltrim(code: string): string {
|
||||||
const source = htmlDecode(this.editor.state.doc.toString());
|
const lines = code.split("\n")
|
||||||
let output;
|
if (lines.length == 0)
|
||||||
if (source.includes("asyncio")){
|
return code
|
||||||
output = await pyodide.runPythonAsync(source);
|
|
||||||
}else{
|
const lengths = lines
|
||||||
output = pyodide.runPython(source);
|
.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) {
|
if (this.hasAttribute('auto-generate') && this.parentElement.lastChild === this) {
|
||||||
|
|||||||
Reference in New Issue
Block a user