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
* 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>
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:
> 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.
37
31
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
+
```
39
45
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
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.
71
81
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
-
classAdd(example.Example):
96
-
defadd(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
-
107
82
## Running components from Python Applications
108
83
109
84
Wasm components can also be invoked from Python applications. This section walks through using tooling
0 commit comments