Skip to content

Commit 42d27ba

Browse files
fix(lang/py): componentize py usage (#296)
* fix(lang/py): componentize-py update This commit fixes the discrepancy due to changes in how `componentize-py` works that have been merged recently. Both bindings generation and the generated code have changed such that the guide is no longer correct as to what to import and implement -- the new docs prefer a `wit_world` import which is generated. * fix(lang/py): remove section regarding exporting interface This commit removes the section regarding exporting interfaces (instead of functions) as the code was long ago changed to use interfaces by default. * refactor(lang/py): use markdown include for WIT file * fix(lang/py): exports interface in python docs Co-authored-by: Wolfgang Meier <womeier@posteo.de> --------- Co-authored-by: Wolfgang Meier <womeier@posteo.de>
1 parent e4e0c4b commit 42d27ba

File tree

1 file changed

+25
-50
lines changed
  • component-model/src/language-support

1 file changed

+25
-50
lines changed

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

Lines changed: 25 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,44 @@ Next, create or download the WIT world you would like to target. For this exampl
1515
world][adder-wit] with an `add` function (e.g. `wit/component.wit`):
1616

1717
```wit
18-
package docs:adder@0.1.0;
19-
20-
interface add {
21-
add: func(x: u32, y: u32) -> u32;
22-
}
23-
24-
world adder {
25-
export add;
26-
}
18+
{{#include ../../examples/tutorial/wit/adder/world.wit}}
2719
```
2820

2921
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:
3022

31-
```sh
32-
$ componentize-py --wit-path wit/component/wit --world adder bindings .
23+
```console
24+
componentize-py --wit-path wit --world adder bindings .
3325
```
3426

3527
> [!NOTE]
3628
> 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.
29+
>
30+
> If you attempt to run bindings generation twice, it will fail if the `bindings` folder already exists.
3731
38-
Bindings were created in an `adder` package which contains an `Add` protocol with an `add` method that we can implement.
32+
Bindings are generated in a folder called `wit_world` by default:
33+
34+
```
35+
<project folder>
36+
├── wit
37+
│   └── component.wit
38+
└── wit_world
39+
├── exports
40+
│   ├── add.py
41+
│   └── __init__.py
42+
├── __init__.py
43+
└── types.py
44+
```
3945

40-
To implement this interface, put the following code in a file called `app.py`:
46+
The `wit_world/exports` folder contains an `Add` protocol which has an `add` method that we can implement,
47+
which represents the export defined in the `adder` world WIT.
48+
49+
To implement the `adder` world (in particular the `add` interface that is exported), put the following code
50+
in a file called `app.py`:
4151

4252
```py
43-
import adder
53+
from wit_world import exports
4454

45-
class Add(adder.Adder):
55+
class Add(exports.Add):
4656
def add(self, x: int, y: int) -> int:
4757
return x + y
4858
```
@@ -69,41 +79,6 @@ $ cargo run --release -- 1 2 ../path/to/add.wasm
6979

7080
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.
7181

72-
### Building a Component that Exports an Interface
73-
74-
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.
75-
76-
```wit
77-
// add-interface.wit
78-
package example:component;
79-
80-
interface add {
81-
add: func(x: u32, y: u32) -> u32;
82-
}
83-
84-
world example {
85-
export add;
86-
}
87-
```
88-
89-
If you peek at the bindings, you'll notice that we now implement a class for the `add` interface rather than for the `example` world. This is a consistent pattern. As you export more interfaces from your world, you implement more classes. Our add example gets the slight update of:
90-
91-
```py
92-
# app.py
93-
import example
94-
95-
class Add(example.Example):
96-
def add(self, a: int, b: int) -> int:
97-
return a + b
98-
```
99-
100-
Once again, compile an application to a Wasm component using the `componentize` subcommand:
101-
102-
```sh
103-
$ componentize-py --wit-path add-interface.wit --world example componentize app -o add.wasm
104-
Component built successfully
105-
```
106-
10782
## Running components from Python Applications
10883

10984
Wasm components can also be invoked from Python applications. This section walks through using tooling

0 commit comments

Comments
 (0)