From 3c2ca6d68c4c57bbff995a01a151e63f70e8e9a4 Mon Sep 17 00:00:00 2001 From: Fabio Pliger Date: Mon, 18 Apr 2022 23:10:41 -0500 Subject: [PATCH] fix error when trying to output to non existing element --- pyscriptjs/examples/bokeh.html | 2 +- pyscriptjs/examples/bokeh_interactive.html | 2 +- pyscriptjs/src/components/pyrepl.ts | 2 +- pyscriptjs/src/components/pyscript.ts | 2 +- pyscriptjs/src/interpreter.ts | 4 ++-- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pyscriptjs/examples/bokeh.html b/pyscriptjs/examples/bokeh.html index 1265580..f4de7d4 100644 --- a/pyscriptjs/examples/bokeh.html +++ b/pyscriptjs/examples/bokeh.html @@ -23,7 +23,7 @@

Bokeh Example

- + import json import pyodide diff --git a/pyscriptjs/examples/bokeh_interactive.html b/pyscriptjs/examples/bokeh_interactive.html index f47d896..cc12319 100644 --- a/pyscriptjs/examples/bokeh_interactive.html +++ b/pyscriptjs/examples/bokeh_interactive.html @@ -23,7 +23,7 @@

Bokeh Example

- + import asyncio import json import pyodide diff --git a/pyscriptjs/src/components/pyrepl.ts b/pyscriptjs/src/components/pyrepl.ts index dde75e3..e18392b 100644 --- a/pyscriptjs/src/components/pyrepl.ts +++ b/pyscriptjs/src/components/pyrepl.ts @@ -188,7 +188,7 @@ export class PyRepl extends BaseEvalElement { this.errorElement = this.outputElement; } } - + this.appendChild(mainDiv); this.editor.focus(); diff --git a/pyscriptjs/src/components/pyscript.ts b/pyscriptjs/src/components/pyscript.ts index 4fb9fef..758038f 100644 --- a/pyscriptjs/src/components/pyscript.ts +++ b/pyscriptjs/src/components/pyscript.ts @@ -247,7 +247,7 @@ export class PyScript extends BaseEvalElement { } getSourceFromElement(): string { - return this.code; + return htmlDecode(this.code); } } diff --git a/pyscriptjs/src/interpreter.ts b/pyscriptjs/src/interpreter.ts index e90b945..76a5ff1 100644 --- a/pyscriptjs/src/interpreter.ts +++ b/pyscriptjs/src/interpreter.ts @@ -22,6 +22,8 @@ class PyScript: if append: child = document.createElement('div'); element = document.querySelector(f'#{element_id}'); + if not element: + return exec_id = exec_id or element.childElementCount + 1 element_id = child.id = f"{element_id}-{exec_id}"; element.appendChild(child); @@ -34,11 +36,9 @@ class PyScript: img_str = 'data:image/png;base64,' + base64.b64encode(buf.read()).decode('UTF-8') document.getElementById(element_id).innerHTML = f'
' elif hasattr(value, "startswith") and value.startswith("data:image"): - console.log(f"DATA/IMAGE: {value}") document.getElementById(element_id).innerHTML = f'
' else: document.getElementById(element_id).innerHTML = value; - console.log(f"ELSE: {append} ==> {element_id} --> {value}") @staticmethod def run_until_complete(f):