Skip to content

Commit 40d6193

Browse files
committed
allow value interpolation
1 parent 4429f90 commit 40d6193

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/py.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@ import {pyodide as Pyodide} from "./dependencies.js";
22

33
export default async function py(require) {
44
const pyodide = await (await require(Pyodide.resolve())).loadPyodide();
5-
return async function py() {
6-
const code = String.raw.apply(String, arguments);
5+
return async function py(strings, ...values) {
6+
let globals = {};
7+
const code = strings.reduce((code, string, i) => {
8+
if (!(i in values)) return code + string;
9+
const name = `_${i}`;
10+
globals[name] = values[i];
11+
return code + string + name;
12+
}, "");
13+
globals = pyodide.toPy(globals);
714
await pyodide.loadPackagesFromImports(code);
8-
const value = await pyodide.runPython(code);
15+
const value = await pyodide.runPythonAsync(code, {globals});
916
return pyodide.isPyProxy(value) ? value.toJs() : value;
1017
};
1118
}

0 commit comments

Comments
 (0)