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 .
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 app -o app.wasm
See the examples directories for more examples, including various ways to run the components you've created.
For an example of running a sandboxed Python guest within a Python host, see component-sandbox-demo.
Known Limitations
This project does not yet support WIT resources or interface versions. Both are coming soon.
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
Hashes for componentize_py-0.4.3-cp37-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 76927a2b8e3959adde4e832682dcc8486112663c84ce6461edc948689bf248ad |
|
MD5 | 6df23f734c093027369db739510ef46c |
|
BLAKE2b-256 | 7ab55ed673d40547746415c2603b2e30cb7d203206a7abcc01e736a7add4e7e4 |
Hashes for componentize_py-0.4.3-cp37-abi3-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 95371f0445f0d2eafc0ae48b878683f7cbd97ac3c5d642035cda24adcdf42481 |
|
MD5 | 9a95005c4f856f9774a0ae9a503a84eb |
|
BLAKE2b-256 | e574ced81ba98402f40a964fa9a2af857b8985dc93078a69165cf0ca18f43ba0 |
Hashes for componentize_py-0.4.3-cp37-abi3-manylinux_2_28_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a842d467e1597e84e7563dd4fc7cfc1aa922f5ae4d445eefd4d6b2c954e677db |
|
MD5 | 93afea94648e210d069d7ae63bf33645 |
|
BLAKE2b-256 | fbda226403c3324403892a1afe2c26c885fb4dcb05fc70edfbf124308d76646e |
Hashes for componentize_py-0.4.3-cp37-abi3-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f903f250c8a37c98088dec07595714740c7bc6349d9f5669538edd642bb243d6 |
|
MD5 | a23b3ec1583c34e917a7731aa3f0191e |
|
BLAKE2b-256 | f7f7c77ade8e6519e42c918839bc69da687bd51ca4ec27ce0af78ffe10ae1788 |
Hashes for componentize_py-0.4.3-cp37-abi3-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 13e453d597a9dad75ae2751a710dc1bfd934131a64a22a31f390de9be3a9ec55 |
|
MD5 | ede17f1ac679863ed06178cfd24d50e6 |
|
BLAKE2b-256 | 38910c268c855f4f6d93591d7db26a2c579b20c29cb670597c2dcf7d148a2750 |