CDN (no install, no bundler)#
The npm package can be imported straight from jsDelivr in a plain html page: no install, no build step. The runtime, Python and its standard library are fetched from the CDN too.
<!doctype html>
<html>
<body>
<script type="module">
const cdn = "https://cdn.jsdelivr.net/npm";
const { initializePyjsRuntime } = await import(
`${cdn}/@derthorsten/pyjs@6.1.0/dist/index.js`
);
// python + stdlib; conda packages (here numpy)
// are solved and installed at runtime
const pyjs = await initializePyjsRuntime({
packages: ["numpy"],
onProgress: ({ item, phase, done, total }) => console.log(item, phase, done, total),
});
pyjs.runPython("import numpy as np; print(np.arange(10).sum())");
</script>
</body>
</html>
Pin the version (
@6.1.0): the JavaScript and the WebAssembly runtime must match, and pinned urls are cached by the browser.Import
dist/index.js(wasm32) ordist/wasm64.jsdirectly, not jsDelivr’s/+esmvariant: the runtime files are loaded relative to the module (import.meta.url),/+esmre-bundles it.Faster: pin the packages at build time (
npx @derthorsten/pyjs lock numpy) and pass them asurls: no solver and no repodata download in the browser. Package files are cached (Cache Storage), a second visit takes them from there.It also works in module workers (
new Worker(url, { type: "module" })) and in the devtools console (await import(...)).
The options of initializePyjsRuntime and the returned API are described in
npm package and the JavaScript API.