1
0
mirror of https://github.com/pyscript/pyscript.git synced 2022-05-01 19:47:48 +03:00

fix error when trying to output to non existing element

This commit is contained in:
Fabio Pliger
2022-04-18 23:10:41 -05:00
parent a6955b170a
commit 3c2ca6d68c
5 changed files with 6 additions and 6 deletions

View File

@@ -23,7 +23,7 @@
<h1>Bokeh Example</h1> <h1>Bokeh Example</h1>
<div id="myplot"></div> <div id="myplot"></div>
<py-script> <py-script id="mycode">
import json import json
import pyodide import pyodide

View File

@@ -23,7 +23,7 @@
<h1>Bokeh Example</h1> <h1>Bokeh Example</h1>
<div id="myplot"></div> <div id="myplot"></div>
<py-script> <py-script id="my">
import asyncio import asyncio
import json import json
import pyodide import pyodide

View File

@@ -188,7 +188,7 @@ export class PyRepl extends BaseEvalElement {
this.errorElement = this.outputElement; this.errorElement = this.outputElement;
} }
} }
this.appendChild(mainDiv); this.appendChild(mainDiv);
this.editor.focus(); this.editor.focus();

View File

@@ -247,7 +247,7 @@ export class PyScript extends BaseEvalElement {
} }
getSourceFromElement(): string { getSourceFromElement(): string {
return this.code; return htmlDecode(this.code);
} }
} }

View File

@@ -22,6 +22,8 @@ class PyScript:
if append: if append:
child = document.createElement('div'); child = document.createElement('div');
element = document.querySelector(f'#{element_id}'); element = document.querySelector(f'#{element_id}');
if not element:
return
exec_id = exec_id or element.childElementCount + 1 exec_id = exec_id or element.childElementCount + 1
element_id = child.id = f"{element_id}-{exec_id}"; element_id = child.id = f"{element_id}-{exec_id}";
element.appendChild(child); element.appendChild(child);
@@ -34,11 +36,9 @@ class PyScript:
img_str = 'data:image/png;base64,' + base64.b64encode(buf.read()).decode('UTF-8') img_str = 'data:image/png;base64,' + base64.b64encode(buf.read()).decode('UTF-8')
document.getElementById(element_id).innerHTML = f'<div><img id="plt" src="{img_str}"/></div>' document.getElementById(element_id).innerHTML = f'<div><img id="plt" src="{img_str}"/></div>'
elif hasattr(value, "startswith") and value.startswith("data:image"): elif hasattr(value, "startswith") and value.startswith("data:image"):
console.log(f"DATA/IMAGE: {value}")
document.getElementById(element_id).innerHTML = f'<div><img id="plt" src="{value}"/></div>' document.getElementById(element_id).innerHTML = f'<div><img id="plt" src="{value}"/></div>'
else: else:
document.getElementById(element_id).innerHTML = value; document.getElementById(element_id).innerHTML = value;
console.log(f"ELSE: {append} ==> {element_id} --> {value}")
@staticmethod @staticmethod
def run_until_complete(f): def run_until_complete(f):