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)
|
||||
// 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) {
|
||||
|
||||
Reference in New Issue
Block a user