Skip to main content

Tool to package Python applications as WebAssembly components

Project description

componentize-py

A Bytecode Alliance project

This is a tool to convert a Python application to a WebAssembly component. It takes the following as input:

  • a WIT file or directory
  • the name of a WIT world defined in the above file or directory
  • the name of a Python module which targets said world
  • a list of directories in which to find the Python module and its dependencies

The output is a component which may be run using e.g. wasmtime.

Getting Started

First, install Python 3.10 or later and pip if you don't already have them. Then, install componentize-py:

pip install componentize-py

Next, create or download the WIT world you'd like to target, e.g.:

cat >hello.wit <<EOF
package example:hello;
world hello {
  export hello: func() -> string;
}
EOF

If you're using an IDE or just want to examine the bindings produced for the WIT world, you can generate them using the bindings subcommand:

componentize-py -d hello.wit -w hello bindings hello_guest

Then, use the hello module produced by the command above to write your app:

cat >app.py <<EOF
import wit_world
class WitWorld(wit_world.WitWorld):
    def hello(self) -> str:
        return "Hello, World!"
EOF

And finally generate the component:

componentize-py -d hello.wit -w hello componentize --stub-wasi app -o app.wasm

To test it, you can install wasmtime-py and use it to generate host-side bindings for the component:

[!NOTE] Due to compatibility issues with wasmtime-py versions beyond 38.x, this example requires version 38.0.0 or earlier: pip install "wasmtime==38.0.0"

pip install wasmtime==38.0.0
python3 -m wasmtime.bindgen app.wasm --out-dir hello_host

Now we can write a simple host app using those bindings:

cat >host.py <<EOF
from hello_host import Root
from wasmtime import Config, Engine, Store

config = Config()
config.cache = True
engine = Engine(config)
store = Store(engine)
hello = Root(store)
print(f"component says: {hello.hello(store)}")
EOF

And finally run it:

 $ python3 host.py
component says: Hello, World!

See the examples directories for more examples, including various ways to run the components you've created.

Known Limitations

Currently, the application can only import dependencies during build time, which means any imports used at runtime must be resolved at the top level of the application module. For example, if x is a module with a submodule named y the following may not work:

import x

class Hello(hello.Hello):
    def hello(self) -> str:
        return x.y.foo()

That's because importing x does not necessarily resolve y. This can be addressed by modifying the code to import y at the top level of the file:

from x import y

class Hello(hello.Hello):
    def hello(self) -> str:
        return y.foo()

This limitation is being tracked as issue #23.

See the issue tracker for other known issues.

Contributing

See CONTRIBUTING.md for details on how to contribute to the project and build it from source.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

componentize_py-0.25.0.tar.gz (191.9 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

componentize_py-0.25.0-cp39-abi3-win_amd64.whl (15.3 MB view details)

Uploaded CPython 3.9+Windows x86-64

componentize_py-0.25.0-cp39-abi3-manylinux_2_28_x86_64.whl (17.3 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ x86-64

componentize_py-0.25.0-cp39-abi3-manylinux_2_28_aarch64.whl (16.9 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ ARM64

componentize_py-0.25.0-cp39-abi3-macosx_11_0_arm64.whl (15.8 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

componentize_py-0.25.0-cp39-abi3-macosx_10_12_x86_64.whl (16.6 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

Details for the file componentize_py-0.25.0.tar.gz.

File metadata

  • Download URL: componentize_py-0.25.0.tar.gz
  • Upload date:
  • Size: 191.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for componentize_py-0.25.0.tar.gz
Algorithm Hash digest
SHA256 c3efc804e16ee28aa16be75fb7da8c39729806e736ad626ea7f8db46619dab53
MD5 26fe7032d38927dd741247fe130dbff2
BLAKE2b-256 9ee9e6d79c0e41c95b512bd6bceaadd9b116a2f7a8f1061dc7c5f4d8fd61ec79

See more details on using hashes here.

File details

Details for the file componentize_py-0.25.0-cp39-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for componentize_py-0.25.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 a5f0c0ec5c451d3d9f238aa41b00ab91af1f340054b7f5e3e8b0c04d5c77e10c
MD5 253a2e3eb8b9c2ba945f6f8f8d2ba935
BLAKE2b-256 f6d6cb05fe58ee38343836e61e8d323a135a03e6f9e9729aa941f836396cdd45

See more details on using hashes here.

File details

Details for the file componentize_py-0.25.0-cp39-abi3-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for componentize_py-0.25.0-cp39-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d40832bc690ef244f219ae7e2d428c7b8754d23e011db99892205642a243ecda
MD5 474016cce956b4dc3377397afa28ed4e
BLAKE2b-256 e2e89d123bb8e156edb600c8aba3bdca91a44be510857ee5f1b66bc9f476967c

See more details on using hashes here.

File details

Details for the file componentize_py-0.25.0-cp39-abi3-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for componentize_py-0.25.0-cp39-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 7a3c378af2fe9026c07899e9b92a94725346355185c247d28570a359f05373cb
MD5 5750978f2f210f21cd5277b1a2e13be8
BLAKE2b-256 3ef6c8b89a74bc90869fb0ef62e50d140dc372353f2f0071f9815c6cf18aab65

See more details on using hashes here.

File details

Details for the file componentize_py-0.25.0-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for componentize_py-0.25.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cc085b1b92213aa2f0747e80c144a7e80922430c0d4caa787045bf6ae58935d1
MD5 e8ddf728ae15512a4acc286b2e76d4e9
BLAKE2b-256 3473e6452722dc1eaca59437ef6f40c0bdeb99a2917d14f98c346062811ceb2d

See more details on using hashes here.

File details

Details for the file componentize_py-0.25.0-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for componentize_py-0.25.0-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 9940df0d2408ec7e9c1a7d3645fd7c25926759867b28709b89fca007fc879d0e
MD5 749e4ddee4dec82c318d5b4799fee5a3
BLAKE2b-256 da2ad1b06f3411e59b38441b70826546d4efd6185f7132e6c3cfad2b7309e6fe

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page