Python
Python programs can be compiled into WASM components using the tool componentize-py
. For Golem you should use our forked version of this tool, as it pins all its dependencies to the one required by Golem Cloud.
Download a prebuilt 0.11.0
binary from the following address:
https://github.com/golemcloud/componentize-py/releases/tag/golem-feb-2024 (opens in a new tab)
The easiest way to get started once the tooling is installed is to use the golem new
command as described in the Quickstart.
If you prefer to do it manually, first create a WIT interface definition in your project's wit
directory:
package my:component
interface api {
add: func(value: u64)
get: func() -> u64
}
world component-name {
export api
}
The next step is to generate the Python bindings from this interface description using componentize-py
:
componentize-py bindings bindings
The second parameter here is the name of the Python module to be generated. This module will contain all the types described in the interface description, as well as base classes to implement the exported functions.
Let's implement the two exported function in a [main.py](http://main.py)
file:
from bindings.component_name import exports
state: int = 0
class Api(exports.Api):
def add(self, value: int):
global state
print("add " + str(value))
state = state + value
def get(self) -> int:
global state
print("get")
return state
The name of the class must be Api
! The base class is imported from the generated bindings
module's submodule which is named as the world in your WIT file. If you are not sure what name to use, check the generated bindings, in this case in bindings/exports/__init__.py
.
The last step is to compile the Python program to a WASM component using componentize-py
:
componentize-py componentize main -o my-component.wasm
The resulting my-component.wasm
is ready to be uploaded to Golem Cloud!