Skip to content

Commit d5a64e5

Browse files
refactor(lang/py): update python docs
Signed-off-by: Victor Adossi <vadossi@cosmonic.com>
1 parent 246a610 commit d5a64e5

1 file changed

Lines changed: 26 additions & 15 deletions

File tree

  • component-model/src/language-support

component-model/src/language-support/python.md

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,51 @@ First, install [Python 3.10 or later](https://www.python.org/) and [pip](https:/
1111
pip install componentize-py
1212
```
1313

14-
Next, create or download the WIT world you would like to target. For this example we will use an [`example`
15-
world](https://github.com/bytecodealliance/component-docs/tree/main/component-model/examples/example-host/add.wit) with an `add` function:
14+
Next, create or download the WIT world you would like to target. For this example we will use an [`adder`
15+
world][adder-wit] with an `add` function (e.g. `wit/component.wit`):
1616

1717
```wit
18-
package example:component;
18+
package docs:adder@0.1.0;
1919
20-
world example {
21-
export add: func(x: s32, y: s32) -> s32;
20+
interface add {
21+
add: func(x: u32, y: u32) -> u32;
22+
}
23+
24+
world adder {
25+
export add;
2226
}
2327
```
2428

2529
If you want to generate bindings produced for the WIT world (for an IDE or typechecker), you can generate them using the `bindings` subcommand. Specify the path to the WIT interface with the world you are targeting:
2630

2731
```sh
28-
$ componentize-py --wit-path /path/to/examples/example-host/add.wit --world example bindings .
32+
$ componentize-py --wit-path wit/component/wit --world adder bindings .
2933
```
3034

35+
> [!NOTE]
3136
> You do not need to generate the bindings in order to `componentize` in the next step. `componentize` will generate bindings on-the-fly and bundle them into the produced component.
3237
33-
You can see that bindings were created in an `example` package which contains an `Example` protocol with an `add` method that we can implement:
38+
Bindings were created in an `adder` package which contains an `Add` protocol with an `add` method that we can implement.
3439

35-
```sh
36-
$ cat<<EOT >> app.py
37-
import example
40+
To implement this interface, put the following code in a file called `app.py`:
41+
42+
```py
43+
import adder
3844

39-
class Example(example.Example):
45+
class Add(adder.Adder):
4046
def add(self, x: int, y: int) -> int:
4147
return x + y
42-
EOT
4348
```
4449

4550
We now can compile our application to a Wasm component using the `componentize` subcommand:
4651

47-
```sh
48-
$ componentize-py --wit-path /path/to/examples/example-host/add.wit --world example componentize app -o add.wasm
52+
```console
53+
componentize-py \
54+
--wit-path wit/component.wit \
55+
--world adder \
56+
componentize \
57+
app \
58+
-o add.wasm
4959
Component built successfully
5060
```
5161

@@ -59,7 +69,6 @@ $ cargo run --release -- 1 2 ../path/to/add.wasm
5969

6070
See [`componentize-py`'s examples](https://github.com/bytecodealliance/componentize-py/tree/main/examples) to try out build HTTP, CLI, and TCP components from Python applications.
6171

62-
6372
### Building a Component that Exports an Interface
6473

6574
The [sample `add.wit` file](https://github.com/bytecodealliance/component-docs/tree/main/component-model/examples/example-host/add.wit) exports a function. However, you'll often prefer to export an interface, either to comply with an existing specification or to capture a set of functions and types that tend to go together. Let's expand our example world to export an interface rather than directly export the function.
@@ -143,3 +152,5 @@ $ python3 host.py
143152
```
144153

145154
[add-wasm]: https://github.com/bytecodealliance/component-docs/blob/main/component-model/examples/example-host/add.wasm
155+
156+
[adder-wit]: https://github.com/bytecodealliance/component-docs/tree/main/component-model/examples/tutorial/wit/adder/world.wit

0 commit comments

Comments
 (0)