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:

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

componentize_py-0.17.2.tar.gz (161.9 kB view details)

Uploaded Source

Built Distributions

componentize_py-0.17.2-cp39-abi3-win_amd64.whl (18.3 MB view details)

Uploaded CPython 3.9+Windows x86-64

componentize_py-0.17.2-cp39-abi3-manylinux_2_28_x86_64.whl (20.3 MB view details)

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

componentize_py-0.17.2-cp39-abi3-manylinux_2_28_aarch64.whl (19.7 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ ARM64

componentize_py-0.17.2-cp39-abi3-macosx_11_0_arm64.whl (19.0 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

componentize_py-0.17.2-cp39-abi3-macosx_10_12_x86_64.whl (19.4 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: componentize_py-0.17.2.tar.gz
  • Upload date:
  • Size: 161.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.17.2.tar.gz
Algorithm Hash digest
SHA256 f3149d93388b8c4cd6807a0a5c8ecce83d647ee21a92b35043111df6746e76e8
MD5 a3d80250e3f642f3a370a0da2cf1ba30
BLAKE2b-256 eb45ba5b7abdbd966e465f02c25b3b07c5dcf776bfd837ea00b5f7db73a81b21

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for componentize_py-0.17.2-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 fc274995921254f039b65c23891ab161f92da5e0528e47586ced57ea07e97959
MD5 30576aa8b7c2e92a1ded16f0213fd60d
BLAKE2b-256 84485eb6802251e6998aec26988aa6b3ed6301526f8e289f9ade65932f424aef

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for componentize_py-0.17.2-cp39-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0105d497661a1a5bc985ae817cc251f5234556a83ac5712ab36b5f45a0a344be
MD5 8621c7c83749cc72f1aaff1c7651c334
BLAKE2b-256 53baf9b8e9aa55538ae6d5fe115edc1e5182ac727d35030008f003514af57a04

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for componentize_py-0.17.2-cp39-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 4b4a3e0c14fe9532feaf54c208e3be94619a8ffae102725542b8da979e598629
MD5 540552ee29d7df98f7652e453a3125aa
BLAKE2b-256 eb465a6a6e2296d2a5ee7f5624b4998d158ff1e2956ef75beb2412536f6b3981

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for componentize_py-0.17.2-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c2280294f552440969cc0aca701f1b9f69f702dab74b7bc58a0c9b08f18ed52f
MD5 b135939f96f7841731dfe6f77723f62a
BLAKE2b-256 ae5e523dcca0e2dfd70f22b3c2b0076fa3f6296fa027f1ea452224a258b57595

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for componentize_py-0.17.2-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 098841d6729ce9773733131f359fd996a8fa5b98bd70c279d7e303cf33ed9c0c
MD5 7a10b772e420d7500c842d8cdfdeb10c
BLAKE2b-256 abea7f26864d889d4050562ba77315f4ee7e4905775143b033c23c3b5d4d8430

See more details on using hashes here.

Supported by

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