GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - bytecodealliance/componentize-py: Tool for targeting the WebAssembly Component Model using Python

BranchesTags
Open more actions menu

Latest commit

 

History

250 Commits
250 Commits

Folders and files

NameName
Last commit message
Last commit date
.cargo
.cargo
 
 
.github/workflows
.github/workflows
 
 
adapters
adapters
 
 
bundled
bundled
 
 
examples
examples
 
 
runtime
runtime
 
 
src
src
 
 
test-generator
test-generator
 
 
tests
tests
 
 
wit
wit
 
 
.gitignore
.gitignore
 
 
CONTRIBUTING.md
CONTRIBUTING.md
 
 
Cargo.lock
Cargo.lock
 
 
Cargo.toml
Cargo.toml
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
alt-build.rs
alt-build.rs
 
 
build.rs
build.rs
 
 
pyo3-config.txt
pyo3-config.txt
 
 
pyproject.toml
pyproject.toml
 
 
stage.sh
stage.sh
 
 

Repository files navigation

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 bindings produced by the command above (a wit package inside hello_guest) to write your app:

cat >app.py <<EOF
import wit

@wit.guest
class Hello(wit.WorldExports):
    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

An exact, versioned world can also be loaded from a WebAssembly package registry:

componentize-py -w wasi:cli/[email protected] bindings wasi_cli

Use --registry-config PATH to merge a standard wasm-pkg configuration file with the global configuration. Use --no-default-registries to start with an empty configuration before loading that file. Registry loading is lazy, and a matching local WIT definition takes precedence. Remote world references must use the exact namespace:package/world@version form.

To test it, you can install wasmtime-py and write a simple host app which uses it to load and run our component:

pip install wasmtime==48.0.0
cat >host.py <<EOF
from wasmtime import Config, Engine, Store
from wasmtime.component import Component, Linker

config = Config()
config.cache = True
engine = Engine(config)
store = Store(engine)
component = Component.from_file(engine, "app.wasm")
linker = Linker(engine)
instance = linker.instantiate(store, component)
hello = instance.get_func(store, "hello")
print(f"component says: {hello(store)}")
EOF

And then 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.

About

Tool for targeting the WebAssembly Component Model using Python

pypi.org/project/componentize-py/

Resources

Readme
Apache-2.0 license

Contributing

Contributing
Activity
Custom properties

Stars

282 stars

Watchers

10 watching

Forks

52 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.