You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: component-model/src/language-support/python.md
+26-15Lines changed: 26 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,41 +11,51 @@ First, install [Python 3.10 or later](https://www.python.org/) and [pip](https:/
11
11
pip install componentize-py
12
12
```
13
13
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`):
16
16
17
17
```wit
18
-
package example:component;
18
+
package docs:adder@0.1.0;
19
19
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;
22
26
}
23
27
```
24
28
25
29
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:
26
30
27
31
```sh
28
-
$ componentize-py --wit-path /path/to/examples/example-host/add.wit --world example bindings .
> 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.
32
37
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.
34
39
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
38
44
39
-
class Example(example.Example):
45
+
classAdd(adder.Adder):
40
46
defadd(self, x: int, y: int) -> int:
41
47
return x + y
42
-
EOT
43
48
```
44
49
45
50
We now can compile our application to a Wasm component using the `componentize` subcommand:
46
51
47
-
```sh
48
-
$ componentize-py --wit-path /path/to/examples/example-host/add.wit --world example componentize app -o add.wasm
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.
61
71
62
-
63
72
### Building a Component that Exports an Interface
64
73
65
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.
0 commit comments