This example demonstrates compiling a Rust library to WebAssembly, creating a JavaScript package api for it, and calling it from Python.
This is accomplished using PythonMonkey, a Python library for executing JavaScript and WebAssembly from Python. Check it out on GitHub!
Install Rust + Cargo: https://doc.rust-lang.org/cargo/getting-started/installation.html
Install wasm-pack
using Cargo: $ cargo install wasm-pack
Install Python with a minimum version of 3.8.
Install PythonMonkey using pip: $ pip install pythonmonkey
Compile:
$ ./build.sh
Run:
$ python3 main.py
Support for Node.js libraries with PythonMonkey is currently a work in
progress so the build.sh
script is required to convert Node.js specific
code to PythonMonkey friendly code. This is performed by replacing
readFileSync
with Python's open
and writing a simple pure JS version
of join
from the path
library.