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.19.3.tar.gz (172.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.19.3-cp39-abi3-win_amd64.whl (22.3 MB view details)

Uploaded CPython 3.9+Windows x86-64

componentize_py-0.19.3-cp39-abi3-manylinux_2_28_x86_64.whl (24.3 MB view details)

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

componentize_py-0.19.3-cp39-abi3-manylinux_2_28_aarch64.whl (23.9 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ ARM64

componentize_py-0.19.3-cp39-abi3-macosx_11_0_arm64.whl (22.8 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

componentize_py-0.19.3-cp39-abi3-macosx_10_12_x86_64.whl (23.6 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: componentize_py-0.19.3.tar.gz
  • Upload date:
  • Size: 172.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.19.3.tar.gz
Algorithm Hash digest
SHA256 09a3cf44dd4d85340e127467cf211817fb339e8673b2eb113527663635452ee2
MD5 831baa15b3bc3df4aae375f2694defa8
BLAKE2b-256 19fd8b04b29f7321a2cb9a290be015b45f6c776838b5b3abfa1dbc677910aee8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for componentize_py-0.19.3-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 f8ddd953c674460fb988cd5d8d2f72045c54df1e76880f7515451eb81ccd320f
MD5 e8413cfc472b66c3a59552d552d1ee9e
BLAKE2b-256 9b9d53c8548a6a8f903e63a70b113cf316520320f7cc9dd2ba102c1af0bb36e1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for componentize_py-0.19.3-cp39-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 38090c8ad642fbf754bcab9d9940ae73a9b4ca3f226eadfe48e7e9158cdf20d5
MD5 6b9a9afc366a408c20b75e2143cfa4cc
BLAKE2b-256 423494be4922fa52986138075f6ca98e4a775112b57145906a9dc34b3d8fdb06

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for componentize_py-0.19.3-cp39-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 3c739ac68f7308dfeda4a56b6abfbaa786ef5d1752365536869b5a02de3fcc42
MD5 4f0090f08f2d5195850123d5c223ae79
BLAKE2b-256 95cca3d94927fb510320ec07773eb3fb50e5ce4a6fe9bc08c4af9c8a34d849d6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for componentize_py-0.19.3-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c41e26785767ef5d2235216a61bae9263c85c6bfe94149484f84c2441c228d9e
MD5 6e3d2dd6d213c7b0327401814c57a226
BLAKE2b-256 3da6d4d4d6615b2bf312f169387e91414d5f24dbae8b8ffe1a1a38fd33b787b7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for componentize_py-0.19.3-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 0cd9ebe222947da39425c80293c9ee76e8657e6faf7a6dea8d7f98c8a4e5aa64
MD5 11e96b819f4ed8bef0ce8d53493e8969
BLAKE2b-256 b887cc02b37ceeacf4ecbf1325e01d45484bbc6b69ffb17f723f74cb57253eec

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