Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/web/cowasm.org/README.md
1067 views

Using python-wasm with webpack

You can use python-wasm with webpack5. There are two things you may have to modify in your webpack configuration. See webpack.config.js, in particular:

  • The NodePolyfillPlugin is needed because python-wasm uses memfs, which requires several polyfilled libraries.

  • The wasm and zip asset/resource rules are needed so python-wasm can import the python wasm binary and zip filesystem.

Thus your webpack.config.js has to include this:

const NodePolyfillPlugin = require("node-polyfill-webpack-plugin"); module.exports = { plugins: [ new NodePolyfillPlugin(), // ... ], module: { rules: [ { test: /\.wasm$|\.zip$/, type: "asset/resource", }, //... ], }, };

Once you do that, you can just put

const python = require("python-wasm");

or (for Typescript)

import python from "python-wasm";

in your code and use the python object, as illustrated here in src/index.ts.

Trying the demo in your browser

git clone https://github.com/sagemathinc/cowasm cd cowasm/packages/website npm install npm run serve

Then visit the URL that it outputs, which is probably http://localhost:8080