Commit d87744ca authored by Romain Casati's avatar Romain Casati
Browse files

Python 3: multiple URL lookup for Pyodide.

parent 29ccac6d
import { KernelBase } from '@basthon/kernel-base';
const PYODIDE_VERSION = "0.16.1";
declare global {
interface Window {
......@@ -17,13 +18,15 @@ export class KernelPython3Old extends KernelBase {
/**
* Where to find pyodide.js (private).
*/
private _pyodideUrl = "https://cdn.jsdelivr.net/pyodide/v0.16.1/full/pyodide.js";
private _pyodideURLs = [`https://cdn.jsdelivr.net/pyodide/v{PYODIDE_VERSION}/full/pyodide.js`];;
private __kernel__: any = null;
public pythonVersion: string = "";
constructor(options: any) {
super(options);
this._pyodideUrl = options?.pyodideUrl ?? this._pyodideUrl;
// for locally installed Pyodide
this._pyodideURLs.unshift(`${this.basthonRoot()}/pyodide/pyodide.js`);
this._pyodideURLs = options?.pyodideURLs ?? this._pyodideURLs;
}
/**
......@@ -62,18 +65,23 @@ export class KernelPython3Old extends KernelBase {
* Start the Basthon kernel asynchronously.
*/
public async launch() {
/* testing if Pyodide is installed locally */
try {
const url = this.basthonRoot() + "/pyodide/pyodide.js";
const response = await fetch(url, { method: "HEAD" });
if (response.ok) this._pyodideUrl = url;
} catch (e) { }
let pyodideURL: string = this._pyodideURLs[0];
for(let url of this._pyodideURLs) {
url = url.replace("{PYODIDE_VERSION}", PYODIDE_VERSION);
try {
const response = await fetch(url, { method: "HEAD" });
if (response.ok) {
pyodideURL = url;
break;
}
} catch (e) { }
}
// forcing Pyodide to look at the right location for other files
window.languagePluginUrl = this._pyodideUrl.substr(0, this._pyodideUrl.lastIndexOf('/')) + '/';
window.languagePluginUrl = pyodideURL.substr(0, pyodideURL.lastIndexOf('/')) + '/';
try {
await KernelPython3Old.loadScript(this._pyodideUrl);
await KernelPython3Old.loadScript(pyodideURL);
} catch (error) {
console.log(error);
console.error("Can't load pyodide.js");
......
......@@ -18,13 +18,15 @@ export class KernelPython3 extends KernelBase {
/**
* Where to find pyodide.js (private).
*/
private _pyodideUrl = `https://cdn.jsdelivr.net/pyodide/v${PYODIDE_VERSION}/full/pyodide.js`;
private _pyodideURLs = [`https://cdn.jsdelivr.net/pyodide/v{PYODIDE_VERSION}/full/pyodide.js`];
private __kernel__: any = null;
public pythonVersion: string = "";
constructor(options: any) {
super(options);
this._pyodideUrl = options?.pyodideUrl ?? this._pyodideUrl;
// for locally installed Pyodide
this._pyodideURLs.unshift(`${this.basthonRoot()}/pyodide/pyodide.js`);
this._pyodideURLs = options?.pyodideURLs ?? this._pyodideURLs;
}
/**
......@@ -60,15 +62,20 @@ export class KernelPython3 extends KernelBase {
* Start the Basthon kernel asynchronously.
*/
public async launch() {
/* testing if Pyodide is installed locally */
try {
const url = this.basthonRoot() + "/pyodide/pyodide.js";
const response = await fetch(url, { method: "HEAD" });
if (response.ok) this._pyodideUrl = url;
} catch (e) { }
let pyodideURL: string = this._pyodideURLs[0];
for(let url of this._pyodideURLs) {
url = url.replace("{PYODIDE_VERSION}", PYODIDE_VERSION);
try {
const response = await fetch(url, { method: "HEAD" });
if (response.ok) {
pyodideURL = url;
break;
}
} catch (e) { }
}
try {
await KernelPython3.loadScript(this._pyodideUrl);
await KernelPython3.loadScript(pyodideURL);
} catch (error) {
console.log(error);
console.error("Can't load pyodide.js");
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment