Python โ WebAssembly
Compile Python functions to WebAssembly with ease. Built in Rust for performance and reliability.
$ cargo add waspy
Waspy is a Rust library. Add it to your project and compile Python to WebAssembly.
How Waspy Works
From Python source to optimized WebAssembly in four steps.
Python AST
Parse with RustPython
Custom IR
Intermediate Rep.
WASM Binary
wasm-encoder
Optimized
Binaryen
The Same Pipeline, In Code
Write Python, compile it from Rust, run it from JavaScript or TypeScript.
def fibonacci(n: int) -> int:
if n <= 1:
return n
a, b = 0, 1
for i in range(2, n + 1):
a, b = b, a + b
return b
def factorial(n: int) -> int:
result = 1
for i in range(1, n + 1):
result *= i
return result
// in your Rust project,
// after `cargo add waspy`
use waspy::compile_python_file;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// parse โ typed IR โ codegen โ Binaryen
let wasm =
compile_python_file("fibonacci.py", true)?;
// a standalone module,
// no Python interpreter inside
std::fs::write("fibonacci.wasm", &wasm)?;
Ok(())
}
// Node 18+
// (TypeScript works the same)
import { readFile } from "node:fs/promises";
const wasm = await readFile("./fibonacci.wasm");
const { instance } =
await WebAssembly.instantiate(wasm);
console.log(instance.exports.fibonacci(10));
// 55
console.log(instance.exports.factorial(5));
// 120
let wasm = waspy::compile_python_to_wasm(&source)?;
Compile Python you already hold in memory, no files involved.
let wasm = waspy::compile_python_project("./my_project", true)?;
Compile a project directory into one module, with __main__.py entry detection and pyproject.toml config.
Waspy vs Other Python Implementations
See how Waspy compares across performance, safety, and portability.
| Feature | Waspy | CPython | MicroPython | PyPy | HPy |
|---|---|---|---|---|---|
| Target | WebAssembly | Native/VM | Microcontrollers | JIT Compilation | C Extensions |
| Performance | Near-native WASM | Interpreted | Optimized for size | JIT optimized | Native speed |
| Type Safety | Compile-time | Runtime only | Runtime only | JIT inference | Runtime only |
| Universal Runtime | Browser/Server/Edge | OS dependent | Hardware specific | OS dependent | OS dependent |
| Startup Time | Instant | Module loading | Fast | JIT warmup | Import overhead |
| Memory Usage | Minimal | Standard | Ultra-low | Higher (JIT) | Standard |
| Standard Library | Basic support | Complete | Subset | Complete | Complete |
| Security | Sandboxed WASM | Full system access | Hardware limited | Full system access | Full system access |
| Use Case | Web apps, serverless | General purpose | IoT, embedded | CPU-intensive apps | C extension API |
What Waspy Does
What the compiler supports today, from parser to binary. Full per-feature status lives on the modules board.
Ahead-of-Time Compilation
Python compiles to a WASM binary before it runs, with no interpreter or VM in the output.
no runtime VMTyped Code Generation
Type annotations map to WASM value types (i32 for ints/bools, f64 for floats), checked at compile time.
annotationsBinaryen Optimization
The binary runs through Binaryen passes: dead-code elimination, inlining, constant folding.
optimizerSmall Rust API
compile_python_to_wasm() for one source, compile_python_file() for an entry file and its imports, compile_python_project() for a directory.
rust crateStandard WASM Output
Emits plain .wasm modules that load in browsers, Node.js, wasmtime, and edge runtimes.
.wasmLanguage Coverage
Classes, closures, lambdas, generators, comprehensions, exceptions, f-strings, decorators.
python 3 syntaxProject Compilation
Multi-file input with import resolution, __main__.py entry detection, pyproject.toml config.
one .wasm outImport Analysis
User-written .py modules resolve from disk and link statically, each compiled once, with namespace calls and aliases.
user modulesStandard Library
math, json, re, datetime, random, collections, itertools, functools, and logging implemented.
stdlib subsetArchitecture
Hover any node to see what happens at that stage.