make runtime an object of Runtime class when it is just an object (#729)

* make runtime an object of Runtime class when it is just an object

* fix constructor

* remove explicit string type

* define default values out of pyodide runtime and use default config instead of initializing a DEFAULT_RUNTIME instance

Co-authored-by: Fabio Pliger <fabio.pliger@gmail.com>
This commit is contained in:
Madhur Tandon
2022-08-27 01:49:03 +05:30
committed by GitHub
parent 9de154595a
commit db806a5df9
2 changed files with 31 additions and 15 deletions

View File

@@ -1,10 +1,9 @@
import * as jsyaml from 'js-yaml'; import * as jsyaml from 'js-yaml';
import { BaseEvalElement } from './base'; import { BaseEvalElement } from './base';
import { appConfig } from '../stores'; import { appConfig } from '../stores';
import type { Runtime, AppConfig } from '../runtime'; import type { AppConfig } from '../runtime';
import { PyodideRuntime } from '../pyodide'; import { Runtime } from '../runtime';
import { PyodideRuntime, DEFAULT_RUNTIME_CONFIG } from '../pyodide';
const DEFAULT_RUNTIME: Runtime = new PyodideRuntime();
/** /**
* Configures general metadata about the PyScript application such * Configures general metadata about the PyScript application such
@@ -34,15 +33,14 @@ export class PyConfig extends BaseEvalElement {
if (loadedValues === undefined) { if (loadedValues === undefined) {
this.values = { this.values = {
autoclose_loader: true, autoclose_loader: true,
runtimes: [DEFAULT_RUNTIME_CONFIG]
}; };
} else { } else {
// eslint-disable-next-line // eslint-disable-next-line
// @ts-ignore // @ts-ignore
this.values = loadedValues; this.values = loadedValues;
} }
if (this.values.runtimes === undefined) {
this.values.runtimes = [DEFAULT_RUNTIME];
}
appConfig.set(this.values); appConfig.set(this.values);
console.log('config set', this.values); console.log('config set', this.values);
@@ -61,7 +59,8 @@ export class PyConfig extends BaseEvalElement {
loadRuntimes() { loadRuntimes() {
console.log('Initializing runtimes...'); console.log('Initializing runtimes...');
for (const runtime of this.values.runtimes) { for (let runtime of this.values.runtimes) {
runtime = new PyodideRuntime(runtime.src, runtime.name, runtime.lang);
const script = document.createElement('script'); // create a script DOM node const script = document.createElement('script'); // create a script DOM node
script.src = runtime.src; // set its src to the provided URL script.src = runtime.src; // set its src to the provided URL
script.addEventListener('load', () => { script.addEventListener('load', () => {

View File

@@ -6,24 +6,41 @@ import { loadPyodide } from 'pyodide';
// @ts-ignore // @ts-ignore
import pyscript from './python/pyscript.py'; import pyscript from './python/pyscript.py';
export const DEFAULT_RUNTIME_CONFIG = {
src: 'https://cdn.jsdelivr.net/pyodide/v0.21.1/full/pyodide.js',
name: 'pyodide-default',
lang: 'python'
};
export class PyodideRuntime extends Runtime { export class PyodideRuntime extends Runtime {
src = 'https://cdn.jsdelivr.net/pyodide/v0.21.1/full/pyodide.js'; src: string;
name = 'pyodide-default'; name?: string;
lang = 'python'; lang?: string;
interpreter: PyodideInterface; interpreter: PyodideInterface;
globals: any; globals: any;
constructor(
src = 'https://cdn.jsdelivr.net/pyodide/v0.21.1/full/pyodide.js',
name = 'pyodide-default',
lang = 'python',
) {
super();
this.src = src;
this.name = name;
this.lang = lang;
}
async loadInterpreter(): Promise<void> { async loadInterpreter(): Promise<void> {
console.log('creating pyodide runtime'); console.log('creating pyodide runtime');
let indexURL: string = this.src.substring(0, this.src.length - "/pyodide.js".length) let indexURL: string = this.src.substring(0, this.src.length - '/pyodide.js'.length);
if (typeof process === 'object' && inJest()) { if (typeof process === 'object' && inJest()) {
indexURL = [process.cwd(), 'node_modules', 'pyodide'].join('/') indexURL = [process.cwd(), 'node_modules', 'pyodide'].join('/');
} }
this.interpreter = await loadPyodide({ this.interpreter = await loadPyodide({
stdout: console.log, stdout: console.log,
stderr: console.log, stderr: console.log,
fullStdLib: false, fullStdLib: false,
indexURL indexURL,
}); });
this.globals = this.interpreter.globals; this.globals = this.interpreter.globals;