Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 67be46f

Browse files
committed
add support for paths on py-env so that we can load local (sort of) scripts into the environment
1 parent 1966f1c commit 67be46f

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

pyscriptjs/src/components/pyenv.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as jsyaml from 'js-yaml';
22

33
import { pyodideLoaded, loadedEnvironments, mode, addInitializer } from '../stores';
4-
import { loadPackage } from '../interpreter';
4+
import { loadPackage, loadFromFile } from '../interpreter';
55

66
// Premise used to connect to the first available pyodide interpreter
77
let pyodideReadyPromise;
@@ -37,13 +37,36 @@ export class PyEnv extends HTMLElement {
3737
this.code = this.innerHTML;
3838
this.innerHTML = '';
3939

40-
let env = this.environment = jsyaml.load(this.code);
40+
let env = [];
41+
let paths = [];
42+
this.environment = jsyaml.load(this.code);
43+
for (let entry of this.environment) {
44+
if (typeof entry == "string" ){
45+
env.push(entry);
46+
}
47+
else if (entry.hasOwnProperty('paths')){
48+
for (let path of entry.paths) {
49+
paths.push(path);
50+
}
51+
}
52+
}
53+
4154
async function loadEnv() {
4255
let pyodide = await pyodideReadyPromise;
4356
await loadPackage(env, pyodide);
4457
console.log("enviroment loaded")
4558
}
59+
60+
async function loadPaths() {
61+
let pyodide = await pyodideReadyPromise;
62+
for (let singleFile of paths) {
63+
await loadFromFile(singleFile, pyodide);
64+
}
65+
console.log("paths loaded")
66+
}
4667
addInitializer(loadEnv);
68+
addInitializer(loadPaths);
4769
console.log("enviroment loading...", env)
70+
4871
}
4972
}

pyscriptjs/src/interpreter.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { getLastPath } from "./utils";
2+
13
// @ts-nocheck
24
// @ts-ignore
35
let pyodideReadyPromise;
@@ -129,4 +131,19 @@ let loadPackage = async function(package_name: string[] | string, runtime: any):
129131
await runtime.loadPackage(package_name);
130132
}
131133

132-
export {loadInterpreter, pyodideReadyPromise, loadPackage}
134+
let loadFromFile = async function(s: string, runtime: any): Promise<any> {
135+
let filename = getLastPath(s);
136+
await runtime.runPythonAsync(`
137+
from pyodide.http import pyfetch
138+
from pyodide import eval_code
139+
response = await pyfetch("`+s+`")
140+
content = await response.bytes()
141+
142+
with open("`+filename+`", "wb") as f:
143+
f.write(content)
144+
`)
145+
146+
runtime.pyimport(filename.replace(".py", ""));
147+
}
148+
149+
export {loadInterpreter, pyodideReadyPromise, loadPackage, loadFromFile}

pyscriptjs/src/utils.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11

2-
export function addClasses(element: HTMLElement, classes: Array<string>){
2+
function addClasses(element: HTMLElement, classes: Array<string>){
33
for (let entry of classes) {
44
element.classList.add(entry);
55
}
66
}
7+
8+
const getLastPath = function (str) {
9+
return str.split('\\').pop().split('/').pop();
10+
}
11+
12+
export {addClasses, getLastPath}

0 commit comments

Comments
 (0)