class Py {
constructor() {
this.globals = {}
}
async py(strings, ...expressions) {
const lastLine=strings.raw[strings.raw.length-1].trim().split("\n")
let outputVar=lastLine.slice(-1)
let code = strings.reduce((result, string, index) => {
if (expressions[index]) {
const name = `x${index}`;
this.globals.set(name,expressions[index])
return result + string + name;
}else{
return result+string
}
}, '');
code+=`\n[${outputVar},globals()]`
await pyodide.loadPackagesFromImports(code);
const result = await pyodide.pyodide_py.eval_code_async(
code,
pyodide.toPy(this.globals)
);
const jsResult = result.toJs()
this.globals = jsResult[1]
return jsResult[0]
}
}