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 hello
class Hello(hello.Hello):
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:
pip install wasmtime
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file componentize_py-0.15.2.tar.gz
.
File metadata
- Download URL: componentize_py-0.15.2.tar.gz
- Upload date:
- Size: 152.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b73f2aafd011b1d8c16a6a7ed0525657fb7923e7dedc5dada609e2eb2eb4cfd6 |
|
MD5 | f249a969a8fece828315dbc0bca28ff7 |
|
BLAKE2b-256 | 42d940b1631c6cdd404c669d673ed1618f6394776983707aa48770311667f63c |
File details
Details for the file componentize_py-0.15.2-cp39-abi3-win_amd64.whl
.
File metadata
- Download URL: componentize_py-0.15.2-cp39-abi3-win_amd64.whl
- Upload date:
- Size: 17.3 MB
- Tags: CPython 3.9+, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 232768f075a4836eeb9118838d0acd028371e319bedd7de75e9009af4fc5d72e |
|
MD5 | 8895508cdd2e05ebac16ac70a9e49960 |
|
BLAKE2b-256 | 6e1407bc1b90541329cdbfd3bb6bf35cd9de72d19f34f26a58fd9649703a2255 |
File details
Details for the file componentize_py-0.15.2-cp39-abi3-manylinux_2_28_x86_64.whl
.
File metadata
- Download URL: componentize_py-0.15.2-cp39-abi3-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 19.0 MB
- Tags: CPython 3.9+, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 80240117bc877e106e379e422f18d9e5e8080742ef0dfcaf78ec10e930593b10 |
|
MD5 | e0d1a2ef87be64e7787ff27ce694e82a |
|
BLAKE2b-256 | 973cc7de1842baff7971d4554a617dbcd950cbb6cae45824748c08eea25d736e |
File details
Details for the file componentize_py-0.15.2-cp39-abi3-manylinux_2_28_aarch64.whl
.
File metadata
- Download URL: componentize_py-0.15.2-cp39-abi3-manylinux_2_28_aarch64.whl
- Upload date:
- Size: 18.6 MB
- Tags: CPython 3.9+, manylinux: glibc 2.28+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7d53b56e2ef0c4dc5d8cc3e702fcf67d5acc6c099bac481485706839e77a82f1 |
|
MD5 | 36909c47654da9af692bddae7d149449 |
|
BLAKE2b-256 | 233a487b56363aca009d0b0504b8099c35c0a19ddb502cc5534cc005e29f87a3 |
File details
Details for the file componentize_py-0.15.2-cp39-abi3-macosx_11_0_arm64.whl
.
File metadata
- Download URL: componentize_py-0.15.2-cp39-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 17.7 MB
- Tags: CPython 3.9+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b4ff0aeaf0f0f6ecb94ef38ce3aa5da5b6bdc063e542d21ed2ec702a98ebe990 |
|
MD5 | 1f5f0e79ff7eb4e30b4b1873112c01fc |
|
BLAKE2b-256 | 678befe83efad1c66e6f3fa0e567ceaed1a89ddf13da2e2245d5896112a53ba1 |
File details
Details for the file componentize_py-0.15.2-cp39-abi3-macosx_10_12_x86_64.whl
.
File metadata
- Download URL: componentize_py-0.15.2-cp39-abi3-macosx_10_12_x86_64.whl
- Upload date:
- Size: 18.1 MB
- Tags: CPython 3.9+, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 530d9eb14cb3313347e2198e0d245898c187a405fe418c495265a19802816700 |
|
MD5 | 9f1c862b813acd36fab9ff28e93f2e1f |
|
BLAKE2b-256 | e481de7fa87c2029aa5ada673d0d7a2be9e1368e27d5ecdc38932f50b7980efe |