mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-22 11:45:28 -05:00
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:
@@ -1,10 +1,9 @@
|
||||
import * as jsyaml from 'js-yaml';
|
||||
import { BaseEvalElement } from './base';
|
||||
import { appConfig } from '../stores';
|
||||
import type { Runtime, AppConfig } from '../runtime';
|
||||
import { PyodideRuntime } from '../pyodide';
|
||||
|
||||
const DEFAULT_RUNTIME: Runtime = new PyodideRuntime();
|
||||
import type { AppConfig } from '../runtime';
|
||||
import { Runtime } from '../runtime';
|
||||
import { PyodideRuntime, DEFAULT_RUNTIME_CONFIG } from '../pyodide';
|
||||
|
||||
/**
|
||||
* Configures general metadata about the PyScript application such
|
||||
@@ -34,15 +33,14 @@ export class PyConfig extends BaseEvalElement {
|
||||
if (loadedValues === undefined) {
|
||||
this.values = {
|
||||
autoclose_loader: true,
|
||||
runtimes: [DEFAULT_RUNTIME_CONFIG]
|
||||
};
|
||||
} else {
|
||||
// eslint-disable-next-line
|
||||
// @ts-ignore
|
||||
this.values = loadedValues;
|
||||
}
|
||||
if (this.values.runtimes === undefined) {
|
||||
this.values.runtimes = [DEFAULT_RUNTIME];
|
||||
}
|
||||
|
||||
appConfig.set(this.values);
|
||||
console.log('config set', this.values);
|
||||
|
||||
@@ -61,7 +59,8 @@ export class PyConfig extends BaseEvalElement {
|
||||
|
||||
loadRuntimes() {
|
||||
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
|
||||
script.src = runtime.src; // set its src to the provided URL
|
||||
script.addEventListener('load', () => {
|
||||
|
||||
@@ -6,24 +6,41 @@ import { loadPyodide } from 'pyodide';
|
||||
// @ts-ignore
|
||||
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 {
|
||||
src = 'https://cdn.jsdelivr.net/pyodide/v0.21.1/full/pyodide.js';
|
||||
name = 'pyodide-default';
|
||||
lang = 'python';
|
||||
src: string;
|
||||
name?: string;
|
||||
lang?: string;
|
||||
interpreter: PyodideInterface;
|
||||
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> {
|
||||
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()) {
|
||||
indexURL = [process.cwd(), 'node_modules', 'pyodide'].join('/')
|
||||
indexURL = [process.cwd(), 'node_modules', 'pyodide'].join('/');
|
||||
}
|
||||
this.interpreter = await loadPyodide({
|
||||
stdout: console.log,
|
||||
stderr: console.log,
|
||||
fullStdLib: false,
|
||||
indexURL
|
||||
indexURL,
|
||||
});
|
||||
|
||||
this.globals = this.interpreter.globals;
|
||||
|
||||
Reference in New Issue
Block a user