Skip to content

Commit 190ae39

Browse files
committed
Auto-generate element ID to ensure output is rendered
1 parent dc9e7e6 commit 190ae39

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

pyscriptjs/src/components/base.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ type PyodideInterface = {
2727
registerJsModule(name: string, module: object): void
2828
}
2929

30+
export function guidGenerator(): string {
31+
var S4 = function(): string {
32+
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
33+
};
34+
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
35+
}
3036

3137
export class BaseEvalElement extends HTMLElement {
3238
shadow: ShadowRoot;
@@ -46,6 +52,8 @@ export class BaseEvalElement extends HTMLElement {
4652
this.shadow = this.attachShadow({ mode: 'open'});
4753
this.wrapper = document.createElement('slot');
4854
this.shadow.appendChild(this.wrapper);
55+
if (!this.id)
56+
this.id = this.constructor.name+"-"+guidGenerator()
4957
}
5058

5159
addToOutput(s: string) {

pyscriptjs/src/components/pyscript.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,8 @@ export class PyScript extends BaseEvalElement {
144144
this.id = `pyid-${Date.now()}`
145145
}
146146
this.outputElement = document.createElement('div');
147-
// this.outputElement.classList.add("output");
148-
this.outputElement.hidden = true;
149-
this.outputElement.id = this.id + "-" + this.childElementCount;
147+
const exec_id = this.getAttribute("exec-id");
148+
this.outputElement.id = this.id + (exec_id ? "-"+exec_id : "");
150149

151150
// add the output div id if there's not output pre-defined
152151
mainDiv.appendChild(this.outputElement);

0 commit comments

Comments
 (0)