mirror of
https://github.com/pyscript/pyscript.git
synced 2022-05-01 19:47:48 +03:00
add support for paths on py-env so that we can load local (sort of) scripts into the environment
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import * as jsyaml from 'js-yaml';
|
||||
|
||||
import { pyodideLoaded, loadedEnvironments, mode, addInitializer } from '../stores';
|
||||
import { loadPackage } from '../interpreter';
|
||||
import { loadPackage, loadFromFile } from '../interpreter';
|
||||
|
||||
// Premise used to connect to the first available pyodide interpreter
|
||||
let pyodideReadyPromise;
|
||||
@@ -37,13 +37,36 @@ export class PyEnv extends HTMLElement {
|
||||
this.code = this.innerHTML;
|
||||
this.innerHTML = '';
|
||||
|
||||
let env = this.environment = jsyaml.load(this.code);
|
||||
let env = [];
|
||||
let paths = [];
|
||||
this.environment = jsyaml.load(this.code);
|
||||
for (let entry of this.environment) {
|
||||
if (typeof entry == "string" ){
|
||||
env.push(entry);
|
||||
}
|
||||
else if (entry.hasOwnProperty('paths')){
|
||||
for (let path of entry.paths) {
|
||||
paths.push(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function loadEnv() {
|
||||
let pyodide = await pyodideReadyPromise;
|
||||
await loadPackage(env, pyodide);
|
||||
console.log("enviroment loaded")
|
||||
}
|
||||
|
||||
async function loadPaths() {
|
||||
let pyodide = await pyodideReadyPromise;
|
||||
for (let singleFile of paths) {
|
||||
await loadFromFile(singleFile, pyodide);
|
||||
}
|
||||
console.log("paths loaded")
|
||||
}
|
||||
addInitializer(loadEnv);
|
||||
addInitializer(loadPaths);
|
||||
console.log("enviroment loading...", env)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { getLastPath } from "./utils";
|
||||
|
||||
// @ts-nocheck
|
||||
// @ts-ignore
|
||||
let pyodideReadyPromise;
|
||||
@@ -129,4 +131,19 @@ let loadPackage = async function(package_name: string[] | string, runtime: any):
|
||||
await runtime.loadPackage(package_name);
|
||||
}
|
||||
|
||||
export {loadInterpreter, pyodideReadyPromise, loadPackage}
|
||||
let loadFromFile = async function(s: string, runtime: any): Promise<any> {
|
||||
let filename = getLastPath(s);
|
||||
await runtime.runPythonAsync(`
|
||||
from pyodide.http import pyfetch
|
||||
from pyodide import eval_code
|
||||
response = await pyfetch("`+s+`")
|
||||
content = await response.bytes()
|
||||
|
||||
with open("`+filename+`", "wb") as f:
|
||||
f.write(content)
|
||||
`)
|
||||
|
||||
runtime.pyimport(filename.replace(".py", ""));
|
||||
}
|
||||
|
||||
export {loadInterpreter, pyodideReadyPromise, loadPackage, loadFromFile}
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
|
||||
export function addClasses(element: HTMLElement, classes: Array<string>){
|
||||
function addClasses(element: HTMLElement, classes: Array<string>){
|
||||
for (let entry of classes) {
|
||||
element.classList.add(entry);
|
||||
}
|
||||
}
|
||||
|
||||
const getLastPath = function (str) {
|
||||
return str.split('\\').pop().split('/').pop();
|
||||
}
|
||||
|
||||
export {addClasses, getLastPath}
|
||||
Reference in New Issue
Block a user