mirror of
https://github.com/pyscript/pyscript.git
synced 2022-05-01 19:47:48 +03:00
clean pyscript.ts
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { BaseEvalElement } from './base';
|
import { BaseEvalElement } from './base';
|
||||||
import { addClasses, ltrim, htmlDecode } from '../utils';
|
import { addClasses, htmlDecode } from '../utils';
|
||||||
|
|
||||||
export class PyInputBox extends BaseEvalElement {
|
export class PyInputBox extends BaseEvalElement {
|
||||||
shadow: ShadowRoot;
|
shadow: ShadowRoot;
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { keymap } 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 { componentDetailsNavOpen, currentComponentDetails, loadedEnvironments, mode, pyodideLoaded } from '../stores';
|
import { componentDetailsNavOpen, loadedEnvironments, mode, pyodideLoaded } from '../stores';
|
||||||
import { addClasses } from '../utils';
|
import { addClasses } from '../utils';
|
||||||
import { BaseEvalElement } from './base';
|
import { BaseEvalElement } from './base';
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,7 @@
|
|||||||
import { EditorState } from '@codemirror/basic-setup';
|
|
||||||
import { python } from '@codemirror/lang-python';
|
|
||||||
import { StateCommand } from '@codemirror/state';
|
|
||||||
import { keymap } from '@codemirror/view';
|
|
||||||
import { defaultKeymap } from '@codemirror/commands';
|
|
||||||
import { oneDarkTheme } from '@codemirror/theme-one-dark';
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
addInitializer,
|
addInitializer,
|
||||||
addPostInitializer,
|
addPostInitializer,
|
||||||
addToScriptsQueue,
|
addToScriptsQueue,
|
||||||
componentDetailsNavOpen,
|
|
||||||
loadedEnvironments,
|
loadedEnvironments,
|
||||||
mode,
|
mode,
|
||||||
pyodideLoaded,
|
pyodideLoaded,
|
||||||
@@ -21,7 +13,6 @@ import { BaseEvalElement } from './base';
|
|||||||
let pyodideReadyPromise;
|
let pyodideReadyPromise;
|
||||||
let environments;
|
let environments;
|
||||||
let currentMode;
|
let currentMode;
|
||||||
let handlersCollected = false;
|
|
||||||
|
|
||||||
pyodideLoaded.subscribe(value => {
|
pyodideLoaded.subscribe(value => {
|
||||||
pyodideReadyPromise = value;
|
pyodideReadyPromise = value;
|
||||||
@@ -30,11 +21,6 @@ loadedEnvironments.subscribe(value => {
|
|||||||
environments = value;
|
environments = value;
|
||||||
});
|
});
|
||||||
|
|
||||||
let propertiesNavOpen;
|
|
||||||
componentDetailsNavOpen.subscribe(value => {
|
|
||||||
propertiesNavOpen = value;
|
|
||||||
});
|
|
||||||
|
|
||||||
mode.subscribe(value => {
|
mode.subscribe(value => {
|
||||||
currentMode = value;
|
currentMode = value;
|
||||||
});
|
});
|
||||||
@@ -45,44 +31,6 @@ type PyodideInterface = {
|
|||||||
registerJsModule(name: string, module: object): void;
|
registerJsModule(name: string, module: object): void;
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: This should be used as base for generic scripts that need exectutoin
|
|
||||||
// from PyScript to initializers, etc...
|
|
||||||
class Script {
|
|
||||||
source: string;
|
|
||||||
state: string;
|
|
||||||
output: string;
|
|
||||||
|
|
||||||
constructor(source: string, output: string) {
|
|
||||||
this.output = output;
|
|
||||||
this.source = source;
|
|
||||||
this.state = 'waiting';
|
|
||||||
}
|
|
||||||
|
|
||||||
async evaluate() {
|
|
||||||
console.log('evaluate');
|
|
||||||
const pyodide = await pyodideReadyPromise;
|
|
||||||
// debugger
|
|
||||||
try {
|
|
||||||
// let source = this.editor.state.doc.toString();
|
|
||||||
let output;
|
|
||||||
if (this.source.includes('asyncio')) {
|
|
||||||
output = await pyodide.runPythonAsync(this.source);
|
|
||||||
} else {
|
|
||||||
output = pyodide.runPython(this.source);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.output) {
|
|
||||||
// this.editorOut.innerHTML = s;
|
|
||||||
}
|
|
||||||
// if (output !== undefined){
|
|
||||||
// this.addToOutput(output);
|
|
||||||
// }
|
|
||||||
} catch (err) {
|
|
||||||
console.log('OOOPS, this happened: ', err);
|
|
||||||
// this.addToOutput(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class PyScript extends BaseEvalElement {
|
export class PyScript extends BaseEvalElement {
|
||||||
constructor() {
|
constructor() {
|
||||||
@@ -207,7 +155,6 @@ async function initHandlers() {
|
|||||||
// // pyodide.runPython(handlerCode);
|
// // pyodide.runPython(handlerCode);
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
handlersCollected = true;
|
|
||||||
|
|
||||||
matches = document.querySelectorAll('[pys-onKeyDown]');
|
matches = document.querySelectorAll('[pys-onKeyDown]');
|
||||||
for (const el of matches) {
|
for (const el of matches) {
|
||||||
@@ -222,7 +169,7 @@ async function mountElements() {
|
|||||||
console.log('Collecting nodes to be mounted into python namespace...');
|
console.log('Collecting nodes to be mounted into python namespace...');
|
||||||
const pyodide = await pyodideReadyPromise;
|
const pyodide = await pyodideReadyPromise;
|
||||||
const matches: NodeListOf<HTMLElement> = document.querySelectorAll('[py-mount]');
|
const matches: NodeListOf<HTMLElement> = document.querySelectorAll('[py-mount]');
|
||||||
let output;
|
|
||||||
let source = '';
|
let source = '';
|
||||||
for (const el of matches) {
|
for (const el of matches) {
|
||||||
let mountName = el.getAttribute('py-mount');
|
let mountName = el.getAttribute('py-mount');
|
||||||
|
|||||||
Reference in New Issue
Block a user