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
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

factored_componentize_py-0.20.1.tar.gz (182.8 kB view details)

Uploaded Source

Built Distribution

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

factored_componentize_py-0.20.1-cp39-abi3-manylinux_2_39_x86_64.whl (24.4 MB view details)

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

File details

Details for the file factored_componentize_py-0.20.1.tar.gz.

File metadata

File hashes

Hashes for factored_componentize_py-0.20.1.tar.gz
Algorithm Hash digest
SHA256 e00a6eb7e5b645919b11c9512e4e363cd4806d87056d07ed2cf850635a4dd6a1
MD5 8faed7c644c71f7403156f33ad12e19d
BLAKE2b-256 c384d2a15c61ad57c6ce9db0c2e9e6fbf03a5fc9154598c0dd98938591bc8481

See more details on using hashes here.

File details

Details for the file factored_componentize_py-0.20.1-cp39-abi3-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for factored_componentize_py-0.20.1-cp39-abi3-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 dbc96314a177ed8bb111edb6bcc16602c064657b073b938ec2d217c5c35ad45e
MD5 cbe31e8ee448076a9b6774486a506f39
BLAKE2b-256 a4c8c86d16c787db9cdfb3c02d986bebc2d994df30918205295df4696f0a9c8e

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