Skip to content
This repository was archived by the owner on May 3, 2022. It is now read-only.

Commit aca290c

Browse files
mayankkumar2Nick Vidal
authored and
Nick Vidal
committed
docs(python-wasm): webassembly instructions for python
Signed-off-by: Mayank Kumar <mayank22oct@gmail.com>
1 parent b755023 commit aca290c

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

docs/WebAssembly/Python.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# WebAssembly with Python
2+
3+
## Environment Setup
4+
5+
### Docker
6+
Install docker using the instructions [here](https://docs.docker.com/engine/install/)
7+
### CPython's WASM build
8+
#### Steps to build a CPython WebAssembly Build
9+
10+
- Clone the repo https://github.com/singlestore-labs/python-wasi/
11+
```bash
12+
git clone https://github.com/singlestore-labs/python-wasi/
13+
```
14+
- Change directory to ```python-wasi``` using the command: ```cd python-wasi```
15+
- Build the docker image using the command: ```docker build -f docker/Dockerfile -t wasi-build:latest docker```
16+
- Now start the docker container and mount the current directory was working directory inside docker container:
17+
```bash
18+
docker run -it --rm -v $(pwd):$(pwd) -w $(pwd) wasi-build:latest bash
19+
```
20+
- To download the CPython source, dependencies and to build the CPython's WASM build, run the command:
21+
```bash
22+
./run.sh
23+
```
24+
- The build output is saved at ```opt/wasi-python/```.
25+
### Wasmtime
26+
Install wasmtime using the instructions at [wasmtime.dev](https://wasmtime.dev/)
27+
28+
## Python Program to Print the Fibonacci sequence
29+
30+
```python3
31+
nterms = 10
32+
n1, n2 = 0, 1
33+
count = 0
34+
if nterms == 1:
35+
print("Fibonacci sequence upto",nterms,":")
36+
print(n1)
37+
else:
38+
print("Fibonacci sequence:")
39+
while count < nterms:
40+
print(n1)
41+
nth = n1 + n2
42+
# update values
43+
n1 = n2
44+
n2 = nth
45+
count += 1
46+
```
47+
48+
## Running the python code on wasmtime
49+
- Change directory to the **root python-wasi source directory**.
50+
- Save the fibonacci code source at ```$HOME/fib.py```
51+
- Run the python fibonacci code in wasmtime using the command
52+
```bash
53+
wasmtime run --mapdir=$(pwd)/opt::opt \
54+
-- opt/wasi-python/bin/python3.wasm -c "$(cat $HOME/fib.py)"
55+
```
56+
output:
57+
```bash
58+
Fibonacci sequence:
59+
0
60+
1
61+
1
62+
2
63+
3
64+
5
65+
8
66+
13
67+
21
68+
34
69+
```
70+
71+
## References
72+
- https://www.programiz.com/python-programming/examples/fibonacci-sequence
73+
- https://github.com/singlestore-labs/python-wasi/blob/main/README.md
74+

0 commit comments

Comments
 (0)