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.24.0.tar.gz (208.8 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.24.0-cp39-abi3-win_amd64.whl (14.8 MB view details)

Uploaded CPython 3.9+Windows x86-64

componentize_py-0.24.0-cp39-abi3-manylinux_2_28_x86_64.whl (16.8 MB view details)

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

componentize_py-0.24.0-cp39-abi3-manylinux_2_28_aarch64.whl (16.3 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ ARM64

componentize_py-0.24.0-cp39-abi3-macosx_11_0_arm64.whl (15.3 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

componentize_py-0.24.0-cp39-abi3-macosx_10_12_x86_64.whl (16.1 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: componentize_py-0.24.0.tar.gz
  • Upload date:
  • Size: 208.8 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.24.0.tar.gz
Algorithm Hash digest
SHA256 c9a8569af27c966fb1734d40130a744d5ca602dee98d1b7c57622db237a3f0bd
MD5 36593288a5ca766b837ebaa9905472c7
BLAKE2b-256 85f5f8b4318628bc9e373ee1dc59c577426cae0aa0d7163fb96dede46e3375e0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for componentize_py-0.24.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 111a1630d3bcdc651e1ee0aa25c5e0079b1f20ca975e43b20a9cbbea77b26a86
MD5 2b3b202bd393806b7ec9396bd421ce63
BLAKE2b-256 4855287785fcdd147764a5cc5f1e49eaa2695c53b5f8de7ba17bdc1696617871

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for componentize_py-0.24.0-cp39-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8ce842fce13960a23e8515cadb6914178692a185efce139d6cb3517a47cf3ad4
MD5 cecd3a6030b63d24bff702c053e8e056
BLAKE2b-256 6d7c550b39b777cc6ce6ee705391841c8b6fdc07995de5fe3c37a7d0b7424a55

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for componentize_py-0.24.0-cp39-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 039d049cdd39854629d975b65a8e658100d341ea2ce6509cf8af243089b510f5
MD5 709d42dcc54263b3dd5e4049643474df
BLAKE2b-256 05d8da01980126a040c24a7f5ff8c13bac065c1226be5538626f3d58a3097079

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for componentize_py-0.24.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c89bddc6de53324df0d1db6c9112cad2af71648b48afb32d30a893c0313349fe
MD5 53818bb38b8d4c73d511da4185f8cf2a
BLAKE2b-256 2b625748935ebbffcd511ebd6431277c19cf00e1c7e5d3028aaedda5769b617a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for componentize_py-0.24.0-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 b2297213b23d647405d710f3d79aca82f0b792983aab2b9c9c47d4772cf5be8b
MD5 79e196d626f3497e9adbebd9120b7926
BLAKE2b-256 a3b84fc9d230cf37ff1a773eb1ecf5b0552071ffe79e832f5a09ed12d1be7e47

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