mirror of
https://github.com/pyscript/pyscript.git
synced 2022-05-01 19:47:48 +03:00
add mode and minimal very rudimentary support for async exec in pyscript
This commit is contained in:
@@ -6,12 +6,14 @@ import { keymap, ViewUpdate } from "@codemirror/view";
|
|||||||
import { defaultKeymap } from "@codemirror/commands";
|
import { defaultKeymap } from "@codemirror/commands";
|
||||||
import { oneDarkTheme } from "@codemirror/theme-one-dark";
|
import { oneDarkTheme } from "@codemirror/theme-one-dark";
|
||||||
|
|
||||||
import { pyodideLoaded, loadedEnvironments, componentDetailsNavOpen, currentComponentDetails } from '../stores';
|
import { pyodideLoaded, loadedEnvironments, componentDetailsNavOpen, currentComponentDetails, mode, addToScriptsQueue } from '../stores';
|
||||||
import { addClasses } from '../utils';
|
import { addClasses } from '../utils';
|
||||||
|
|
||||||
// Premise used to connect to the first available pyodide interpreter
|
// Premise used to connect to the first available pyodide interpreter
|
||||||
let pyodideReadyPromise;
|
let pyodideReadyPromise;
|
||||||
let environments;
|
let environments;
|
||||||
|
let currentMode;
|
||||||
|
|
||||||
pyodideLoaded.subscribe(value => {
|
pyodideLoaded.subscribe(value => {
|
||||||
pyodideReadyPromise = value;
|
pyodideReadyPromise = value;
|
||||||
});
|
});
|
||||||
@@ -24,6 +26,10 @@ componentDetailsNavOpen.subscribe(value => {
|
|||||||
propertiesNavOpen = value;
|
propertiesNavOpen = value;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
mode.subscribe(value => {
|
||||||
|
currentMode = value;
|
||||||
|
});
|
||||||
|
|
||||||
function createCmdHandler(el){
|
function createCmdHandler(el){
|
||||||
// Creates a codemirror cmd handler that calls the el.evaluate when an event
|
// Creates a codemirror cmd handler that calls the el.evaluate when an event
|
||||||
// triggers that specific cmd
|
// triggers that specific cmd
|
||||||
@@ -127,20 +133,31 @@ export class PyScript extends HTMLElement {
|
|||||||
{key: "source", value: "self"}
|
{key: "source", value: "self"}
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
addClasses(this.btnConfig, buttonClasses);
|
addClasses(this.btnConfig, buttonClasses);
|
||||||
addClasses(this.btnConfig, ["bg-blue-500"])
|
addClasses(this.btnConfig, ["bg-blue-500"])
|
||||||
eDiv.appendChild(this.btnConfig);
|
eDiv.appendChild(this.btnConfig);
|
||||||
|
|
||||||
// Editor Div
|
|
||||||
this.editorOut = document.createElement('div');
|
|
||||||
this.editorOut.classList.add("output");
|
|
||||||
this.editorOut.hidden = true;
|
|
||||||
|
|
||||||
mainDiv.appendChild(eDiv);
|
mainDiv.appendChild(eDiv);
|
||||||
mainDiv.appendChild(this.editorNode);
|
mainDiv.appendChild(this.editorNode);
|
||||||
mainDiv.appendChild(this.editorOut);
|
|
||||||
this.appendChild(mainDiv);
|
if (this.hasAttribute('target')) {
|
||||||
|
this.editorOut = document.getElementById(this.getAttribute('target'));
|
||||||
|
}else{
|
||||||
|
// Editor Output Div
|
||||||
|
this.editorOut = document.createElement('div');
|
||||||
|
this.editorOut.classList.add("output");
|
||||||
|
this.editorOut.hidden = true;
|
||||||
|
|
||||||
|
// add the output div id there's not target
|
||||||
|
mainDiv.appendChild(this.editorOut);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentMode=="edit"){
|
||||||
|
this.appendChild(mainDiv);
|
||||||
|
}else{
|
||||||
|
addToScriptsQueue(this);
|
||||||
|
}
|
||||||
|
|
||||||
console.log('connected');
|
console.log('connected');
|
||||||
}
|
}
|
||||||
@@ -156,7 +173,14 @@ export class PyScript extends HTMLElement {
|
|||||||
// debugger
|
// debugger
|
||||||
try {
|
try {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
let output = pyodide.runPython(this.editor.state.doc.toString());
|
let source = this.editor.state.doc.toString();
|
||||||
|
let output;
|
||||||
|
if (source.includes("asyncio")){
|
||||||
|
output = pyodide.runPythonAsync(source);
|
||||||
|
}else{
|
||||||
|
output = pyodide.runPython(source);
|
||||||
|
}
|
||||||
|
|
||||||
if (output !== undefined){
|
if (output !== undefined){
|
||||||
this.addToOutput(output);
|
this.addToOutput(output);
|
||||||
}
|
}
|
||||||
@@ -176,5 +200,3 @@ export class PyScript extends HTMLElement {
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user