8000 add mode and minimal very rudimentary support for async exec in pyscript · evolution99/pyscript@b5680bd · GitHub
[go: up one dir, main page]

Skip to content

Commit b5680bd

Browse files
committed
add mode and minimal very rudimentary support for async exec in pyscript
1 parent 72be1aa commit b5680bd

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

pyscriptjs/src/components/pyscript.ts

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import { keymap, ViewUpdate } from "@codemirror/view";
66
import { defaultKeymap } from "@codemirror/commands";
77
import { oneDarkTheme } from "@codemirror/theme-one-dark";
88

9-
import { pyodideLoaded, loadedEnvironments, componentDetailsNavOpen, currentComponentDetails } from '../stores';
9+
import { pyodideLoaded, loadedEnvironments, componentDetailsNavOpen, currentComponentDetails, mode, addToScriptsQueue } from '../stores';
1010
import { addClasses } from '../utils';
1111

1212
// Premise used to connect to the first available pyodide interpreter
1313
let pyodideReadyPromise;
1414
let environments;
15+
let currentMode;
16+
1517
pyodideLoaded.subscribe(value => {
1618
pyodideReadyPromise = value;
1719
});
@@ -24,6 +26,10 @@ componentDetailsNavOpen.subscribe(value => {
2426
propertiesNavOpen = value;
2527
});
2628

29+
mode.subscribe(value => {
30+
currentMode = value;
31+
});
32+
2733
function createCmdHandler(el){
2834
// Creates a codemirror cmd handler that calls the el.evaluate when an event
2935
// triggers that specific cmd
@@ -127,20 +133,31 @@ export class PyScript extends HTMLElement {
127133
{key: "source", value: "self"}
128134
])
129135
}
130-
136+
131137
addClasses(this.btnConfig, buttonClasses);
132138
addClasses(this.btnConfig, ["bg-blue-500"])
133139
eDiv.appendChild(this.btnConfig);
134-
135-
// Editor Div
136-
this.editorOut = document.createElement('div');
137-
this.editorOut.classList.add("output");
138-
this.editorOut.hidden = true;
139140

140141
mainDiv.appendChild(eDiv);
141142
mainDiv.appendChild(this.editorNode);
142-
mainDiv.appendChild(this.editorOut);
143-
this.appendChild(mainDiv);
143+
144+
if (this.hasAttribute('target')) {
145+
this.editorOut = document.getElementById(this.getAttribute('target'));
146+
}else{
147+
// Editor Output Div
148+
this.editorOut = document.createElement('div');
149+
this.editorOut.classList.add("output");
150+
this.editorOut.hidden = true;
151+
152+
// add the output div id there's not target
153+
mainDiv.appendChild(this.editorOut);
154+
}
155+
156+
if (currentMode=="edit"){
157+
this.appendChild(mainDiv);
158+
}else{
159+
addToScriptsQueue(this);
160+
}
144161

145162
console.log('connected');
146163
}
@@ -156,7 +173,14 @@ export class PyScript extends HTMLElement {
156173
// debugger
157174
try {
158175
// @ts-ignore
159-
let output = pyodide.runPython(this.editor.state.doc.toString());
176+
let source = this.editor.state.doc.toString();
177+
let output;
178+
if (source.includes("asyncio")){
179+
output = pyodide.runPythonAsync(source);
180+
}else{
181+
output = pyodide.runPython(source);
182+
}
183+
160184
if (output !== undefined){
161185
this.addToOutput(output);
162186
}
@@ -176,5 +200,3 @@ export class PyScript extends HTMLElement {
176200

177201
}
178202
}
179-
180-

0 commit comments

Comments
 (0)
0