mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-22 03:35:31 -05:00
PyodideRuntime should be one of the runtimes (#698)
* PyodideRuntime should be one of the runtimes * subsume interpreter into runtime API * fix eslint * add comments * move initializers, postInitializers, scriptsQueue, etc. to initialize() of Runtime Super Class * modify comment for initialize * small renaming * change id to default * fix pyscript.py import * try adding tests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add inlineDynamicImports option * Make jest happy about ESM modules * Attempt to make jest happy about pyodide * point to version in accordance with node module being used * fix base.ts * fix tests * fix indexURL path determination * edit pyodide.asm.js as a part of setup process * load runtime beforeAll tests * add test for loading a package * use only runPythonAsync underneath for pyodide * import PyodideInterface type directly from pyodide * add some comments Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Philipp Rudiger <prudiger@anaconda.com>
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
import { pyodideLoaded } from '../stores';
|
||||
import { runtimeLoaded } from '../stores';
|
||||
import { guidGenerator, addClasses, removeClasses } from '../utils';
|
||||
import type { PyodideInterface } from '../pyodide';
|
||||
// Premise used to connect to the first available pyodide interpreter
|
||||
let runtime: PyodideInterface;
|
||||
|
||||
import type { Runtime } from '../runtime';
|
||||
|
||||
// Global `Runtime` that implements the generic runtimes API
|
||||
let runtime: Runtime;
|
||||
let Element;
|
||||
|
||||
pyodideLoaded.subscribe(value => {
|
||||
runtimeLoaded.subscribe(value => {
|
||||
runtime = value;
|
||||
});
|
||||
|
||||
@@ -77,7 +79,7 @@ export class BaseEvalElement extends HTMLElement {
|
||||
return this.code;
|
||||
}
|
||||
|
||||
protected async _register_esm(pyodide: PyodideInterface): Promise<void> {
|
||||
protected async _register_esm(runtime: Runtime): Promise<void> {
|
||||
const imports: { [key: string]: unknown } = {};
|
||||
const nodes = document.querySelectorAll("script[type='importmap']");
|
||||
const importmaps: Array<any> = [];
|
||||
@@ -107,7 +109,7 @@ export class BaseEvalElement extends HTMLElement {
|
||||
}
|
||||
}
|
||||
|
||||
pyodide.registerJsModule('esm', imports);
|
||||
runtime.registerJsModule('esm', imports);
|
||||
}
|
||||
|
||||
async evaluate(): Promise<void> {
|
||||
@@ -119,20 +121,12 @@ export class BaseEvalElement extends HTMLElement {
|
||||
try {
|
||||
source = this.source ? await this.getSourceFromFile(this.source)
|
||||
: this.getSourceFromElement();
|
||||
const is_async = source.includes('asyncio')
|
||||
|
||||
this._register_esm(runtime);
|
||||
if (is_async) {
|
||||
<string>await runtime.runPythonAsync(
|
||||
`output_manager.change(out="${this.outputElement.id}", err="${this.errorElement.id}", append=${this.appendOutput ? 'True' : 'False'})`,
|
||||
);
|
||||
output = <string>await runtime.runPythonAsync(source);
|
||||
} else {
|
||||
output = <string>runtime.runPython(
|
||||
`output_manager.change(out="${this.outputElement.id}", err="${this.errorElement.id}", append=${this.appendOutput ? 'True' : 'False'})`,
|
||||
);
|
||||
output = <string>runtime.runPython(source);
|
||||
}
|
||||
<string>await runtime.run(
|
||||
`output_manager.change(out="${this.outputElement.id}", err="${this.errorElement.id}", append=${this.appendOutput ? 'True' : 'False'})`,
|
||||
);
|
||||
output = <string>await runtime.run(source);
|
||||
|
||||
if (output !== undefined) {
|
||||
if (Element === undefined) {
|
||||
@@ -145,8 +139,7 @@ export class BaseEvalElement extends HTMLElement {
|
||||
this.outputElement.style.display = 'block';
|
||||
}
|
||||
|
||||
is_async ? await runtime.runPythonAsync(`output_manager.revert()`)
|
||||
: await runtime.runPython(`output_manager.revert()`);
|
||||
await runtime.run(`output_manager.revert()`);
|
||||
|
||||
// check if this REPL contains errors, delete them and remove error classes
|
||||
const errorElements = document.querySelectorAll(`div[id^='${this.errorElement.id}'][error]`);
|
||||
@@ -191,10 +184,8 @@ export class BaseEvalElement extends HTMLElement {
|
||||
} // end evaluate
|
||||
|
||||
async eval(source: string): Promise<void> {
|
||||
const pyodide = runtime;
|
||||
|
||||
try {
|
||||
const output = await pyodide.runPythonAsync(source);
|
||||
const output = await runtime.run(source);
|
||||
if (output !== undefined) {
|
||||
console.log(output);
|
||||
}
|
||||
@@ -204,8 +195,8 @@ export class BaseEvalElement extends HTMLElement {
|
||||
} // end eval
|
||||
|
||||
runAfterRuntimeInitialized(callback: () => Promise<void>){
|
||||
pyodideLoaded.subscribe(value => {
|
||||
if ('runPythonAsync' in value) {
|
||||
runtimeLoaded.subscribe(value => {
|
||||
if ('run' in value) {
|
||||
setTimeout(async () => {
|
||||
await callback();
|
||||
}, 100);
|
||||
@@ -247,9 +238,9 @@ function createWidget(name: string, code: string, klass: string) {
|
||||
// this.proxy.connect();
|
||||
// this.registerWidget();
|
||||
// }, 2000);
|
||||
pyodideLoaded.subscribe(value => {
|
||||
runtimeLoaded.subscribe(value => {
|
||||
console.log('RUNTIME READY', value);
|
||||
if ('runPythonAsync' in value) {
|
||||
if ('run' in value) {
|
||||
runtime = value;
|
||||
setTimeout(async () => {
|
||||
await this.eval(this.code);
|
||||
@@ -263,16 +254,14 @@ function createWidget(name: string, code: string, klass: string) {
|
||||
}
|
||||
|
||||
registerWidget() {
|
||||
const pyodide = runtime;
|
||||
console.log('new widget registered:', this.name);
|
||||
pyodide.globals.set(this.id, this.proxy);
|
||||
runtime.globals.set(this.id, this.proxy);
|
||||
}
|
||||
|
||||
async eval(source: string): Promise<void> {
|
||||
const pyodide = runtime;
|
||||
try {
|
||||
const output = await pyodide.runPythonAsync(source);
|
||||
this.proxyClass = pyodide.globals.get(this.klass);
|
||||
const output = await runtime.run(source);
|
||||
this.proxyClass = runtime.globals.get(this.klass);
|
||||
if (output !== undefined) {
|
||||
console.log(output);
|
||||
}
|
||||
@@ -365,9 +354,8 @@ export class PyWidget extends HTMLElement {
|
||||
}
|
||||
|
||||
async eval(source: string): Promise<void> {
|
||||
const pyodide = runtime;
|
||||
try {
|
||||
const output = await pyodide.runPythonAsync(source);
|
||||
const output = await runtime.run(source);
|
||||
if (output !== undefined) {
|
||||
console.log(output);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user