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
refactor(lang/py): Revise Python language support section (#312)
* Revise Python language support section
* Add explicit steps to create needed directories/files
* Move code examples into separate files
* Make the "example host" section consistent with the other language support sections
* Clarify comment about `--stub-wasi` option (made it clearer that it's an option passed to `componentize-py`)
* fix(lang/py): wording of tooling
---------
Co-authored-by: Victor Adossi <[email protected]>
Copy file name to clipboardExpand all lines: component-model/src/language-support/python.md
+50-40Lines changed: 50 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,30 +2,38 @@
2
2
3
3
## Building a Component with `componentize-py`
4
4
5
-
[`componentize-py`](https://github.com/bytecodealliance/componentize-py) is a tool that converts a Python
5
+
[`componentize-py`](https://github.com/bytecodealliance/componentize-py) is a tool
6
+
that converts a Python
6
7
application to a WebAssembly component.
7
8
8
-
First, install [Python 3.10 or later](https://www.python.org/) and [pip](https://pypi.org/project/pip/) if you don't already have them. Then, install `componentize-py`:
9
+
First, install [Python 3.10 or later](https://www.python.org/) and [pip](https://pypi.org/project/pip/)
10
+
if you don't already have them. Then, install `componentize-py`:
9
11
10
12
```sh
11
13
pip install componentize-py
12
14
```
13
15
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
+
Next, create or download the WIT world you would like to target.
17
+
For this example we will use an [`adder`world][adder-wit] with an `add` function.
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:
23
+
Create a new directory for your project and create a subdirectory in it called `wit`.
24
+
Copy and paste this code into a file named `wit/component.wit`.
25
+
26
+
If you want to generate bindings for the WIT world (for an IDE or typechecker),
27
+
you can generate them using the `bindings` subcommand.
28
+
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.
35
+
> You do not need to generate the bindings in order to `componentize` in the next step.
36
+
>`componentize` will generate bindings on-the-fly and bundle them into the produced component.
29
37
>
30
38
> If you attempt to run bindings generation twice, it will fail if the `bindings` folder already exists.
31
39
@@ -46,18 +54,14 @@ Bindings are generated in a folder called `wit_world` by default:
46
54
The `wit_world/exports` folder contains an `Add` protocol which has an `add` method that we can implement,
47
55
which represents the export defined in the `adder` world WIT.
48
56
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`:
57
+
To implement the `adder` world (in particular the `add` interface that is exported),
We now can compile our application to a Wasm component using the `componentize` subcommand:
64
+
We now can compile our application to a WebAssembly component using the `componentize` subcommand:
61
65
62
66
```console
63
67
componentize-py \
@@ -69,54 +73,60 @@ componentize-py \
69
73
Component built successfully
70
74
```
71
75
72
-
To test the component, run it using the [Rust `add`host](./rust.md#creating-a-command-component-with-cargo-component):
76
+
## Running components from the example host
73
77
74
-
```sh
75
-
$ cd component-model/examples/add-host
76
-
$ cargo run --release -- 1 2 ../path/to/add.wasm
77
-
1 + 2 = 3
78
-
```
78
+
The following section requires you to have [a Rust toolchain][rust] installed.
79
+
80
+
{{#include example-host-part1.md}}
79
81
80
-
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.
82
+
A successful run should show the following output
83
+
(of course, the paths to your example host and adder component will vary):
See [`componentize-py`'s examples](https://github.com/bytecodealliance/componentize-py/tree/main/examples)
90
+
to try out building HTTP, CLI, andTCP components from Python applications.
81
91
82
92
## Running components from Python Applications
83
93
84
-
Wasm components can also be invoked from Python applications. This section walks through using tooling
85
-
to call the [pre-built `app.wasm` component][add-wasm] in the examples.
94
+
WebAssembly components can also be invoked from Python applications.
95
+
This section walks through using python native tooling to call the [pre-built `add.wasm` component][add-wasm] in the examples.
86
96
87
-
> `wasmtime-py` is only able to run components built with `componentize-py` when the `--stub-wasi` option is used at build time. This is because `wasmtime-py` does not yet support [resources](../design/wit.md#resources), and `componentize-py` by default generates components which use resources from the `wasi:cli` world. See [this example](https://github.com/bytecodealliance/componentize-py/tree/main/examples/sandbox) of using the `--stub-wasi` option to generate a `wasmtime-py`-compatible component.
97
+
> To use `wasmtime-py` to run a component built with`componentize-py`,
98
+
> the `--stub-wasi` option must have been passed to `componentize-py`
99
+
> when the component was built.
100
+
> This is because `wasmtime-py` does not yet support [resources](../design/wit.md#resources),
101
+
>and`componentize-py` by default generates components which use resources from the `wasi:cli` world.
102
+
> See [this example](https://github.com/bytecodealliance/componentize-py/tree/main/examples/sandbox)
103
+
> of using the `--stub-wasi` option to generate a `wasmtime-py`-compatible component.
88
104
89
-
First, install [Python 3.11 or later](https://www.python.org/) and [pip](https://pypi.org/project/pip/) if you don't already have them. Then, install [`wasmtime-py`](https://github.com/bytecodealliance/wasmtime-py):
105
+
First, install [Python 3.11or later](https://www.python.org/) and [pip](https://pypi.org/project/pip/) if you don't already have them.
0 commit comments