Skip to content

Commit ea63e36

Browse files
authored
Merge pull request pyscript#42 from anaconda/auto-id
Auto-generate element ID to ensure output is rendered
2 parents dc9e7e6 + 1be8aa3 commit ea63e36

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

pyscriptjs/src/components/base.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { pyodideLoaded, loadedEnvironments, componentDetailsNavOpen, mode } from '../stores';
2-
2+
import { guidGenerator } from '../utils';
33
// Premise used to connect to the first available pyodide interpreter
44
let pyodideReadyPromise;
55
let environments;
@@ -27,7 +27,6 @@ type PyodideInterface = {
2727
registerJsModule(name: string, module: object): void
2828
}
2929

30-
3130
export class BaseEvalElement extends HTMLElement {
3231
shadow: ShadowRoot;
3332
wrapper: HTMLElement;
@@ -46,6 +45,8 @@ export class BaseEvalElement extends HTMLElement {
4645
this.shadow = this.attachShadow({ mode: 'open'});
4746
this.wrapper = document.createElement('slot');
4847
this.shadow.appendChild(this.wrapper);
48+
if (!this.id)
49+
this.id = this.constructor.name+"-"+guidGenerator()
4950
}
5051

5152
addToOutput(s: string) {

pyscriptjs/src/components/pyscript.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,9 @@ export class PyScript extends BaseEvalElement {
140140
// to create a new output div to output to
141141

142142
// Let's check if we have an id first and create one if not
143-
if (!this.id){
144-
this.id = `pyid-${Date.now()}`
145-
}
146143
this.outputElement = document.createElement('div');
147-
// this.outputElement.classList.add("output");
148-
this.outputElement.hidden = true;
149-
this.outputElement.id = this.id + "-" + this.childElementCount;
144+
const exec_id = this.getAttribute("exec-id");
145+
this.outputElement.id = this.id + (exec_id ? "-"+exec_id : "");
150146

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

pyscriptjs/src/utils.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,11 @@ function ltrim(code: string): string {
3434
return code
3535
}
3636

37-
export {addClasses, getLastPath, ltrim, htmlDecode}
37+
function guidGenerator(): string {
38+
var S4 = function(): string {
39+
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
40+
};
41+
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
42+
}
43+
44+
export {addClasses, getLastPath, ltrim, htmlDecode, guidGenerator}

0 commit comments

Comments
 (0)