11# pyjs-code-runner
2+
23[ ![ CI] ( https://github.com/emscripten-forge/pyjs-code-runner/actions/workflows/main.yaml/badge.svg )] ( https://github.com/emscripten-forge/pyjs-code-runner/actions/workflows/main.yaml )
34
45A driver to run python code in a wasm environment, almost like running vanilla python code.
56
67## Motivation
78
8- Debugging, experimenting and testing python code from a dedicated conda environment
9+ Debugging, experimenting and testing python code from a dedicated conda environment
910in browser environment is a complex process with a lot of (complicated) steps.
1011
11- * create the environment for emscripten
12+ * create the environment for emscripten
1213* pack the environemtn
1314
15+ ## Installation
16+
17+ Currently ` pyjs-code-runner ` is not available on PyPI. To install it, clone the repository and install it with ` pip ` :
18+
19+ We first recommend to create a new conda environment for ` pyjs-code-runner ` :
20+
21+ ``` bash
22+ mamba create -n pyjs-code-runner -c conda-forge python
23+ mamba activate pyjs-code-runner
24+ ```
25+
26+ Then install ` pyjs-code-runner ` :
27+
28+ ``` bash
29+ git clone https://github.com/emscripten-forge/pyjs-code-runner
30+ cd pyjs-code-runner
31+ python -m pip install -e .
32+ ```
33+
34+ Then install the browser for use with Playwright:
35+
36+ ``` bash
37+ playwright install
38+ ```
39+
40+ You will then need another conda environment for the code you want to run in the browser. Here we specify the ` emscripten-forge ` channel and the ` emscripten-32 ` platform:
1441
42+ ``` bash
43+ mamba create -n my_env -c https://repo.mamba.pm/emscripten-forge -c https://repo.mamba.pm/conda-forge --platform=emscripten-32 python numpy pyjs
44+ ```
45+
46+ You might want to add more dependencies to this environment, depending on the code you would like to run.
47+
48+ ## Usage
1549
16- ## Examples
50+ Here we assume a file ` main.py ` located at ` ~/foo/bar/main.py ` with the following content:
1751
52+ ``` py
53+ import numpy as np
1854
55+ print (" Hello from pyjs-code-runner" )
56+ print (" numpy version:" , np.__version__ )
57+ ```
1958
20- Here we assume a file ` main.py ` located at ` ~/foo/bar/main.py ` .
59+ You can then run this code in the browser with the following command:
2160
2261``` bash
23- # run in browser-main-thread backend
62+ # run in browser-main-thread backend
2463pyjs_code_runner run script \
2564 browser-main \
2665 --conda-env ~ /micromamba/envs/my_env ` # the emscripten-forge env` \
@@ -37,12 +76,40 @@ pyjs_code_runner run script
3776 \
3877 --async-main ` # should a top-level async` \
3978 ` # function named main be called` \
40- --headless
79+ --headless
4180
4281```
4382
83+ The ` --headless ` flag will run the browser in headless mode. If you want to see the browser open on your machine, you can remove this flag.
84+
85+ When you run this command you swill be able to see the output of the code in the terminal that looks like the following:
86+
4487``` bash
45- # run in browser-worker-thread backend
88+ Failed to load resource: the server responded with a status of 404 (File not found)
89+ fetching python package from ./python-3.10.2-h_hash_26_cpython.tar.gz
90+ fetching pkg numpy from ./numpy-1.24.2-py310h6d2fff6_0.tar.gz
91+ fetching pkg pip from ./pip-23.1-pyhd8ed1ab_0.tar.gz
92+ fetching pkg setuptools from ./setuptools-63.4.2-py310h8bed8af_0.tar.gz
93+ fetching pkg wheel from ./wheel-0.40.0-pyhd8ed1ab_0.tar.gz
94+ fetching pkg pyparsing from ./pyparsing-3.0.9-pyhd8ed1ab_0.tar.gz
95+ fetching pkg pyjs from ./pyjs-1.0.0-hc96583f_0.tar.gz
96+ fetching pkg emscripten-abi from ./emscripten-abi-3.1.27-hb0f4dca_5.tar.gz
97+ extract /package_tarballs/setuptools-63.4.2-py310h8bed8af_0.tar.gz (304 bytes)
98+ extract /package_tarballs/wheel-0.40.0-pyhd8ed1ab_0.tar.gz (51524 bytes)
99+ extract /package_tarballs/pyparsing-3.0.9-pyhd8ed1ab_0.tar.gz (90256 bytes)
100+ extract /package_tarballs/pyjs-1.0.0-hc96583f_0.tar.gz (283 bytes)
101+ extract /package_tarballs/emscripten-abi-3.1.27-hb0f4dca_5.tar.gz (302 bytes)
102+ extract /package_tarballs/pip-23.1-pyhd8ed1ab_0.tar.gz (1319563 bytes)
103+ extract /package_tarballs/python-3.10.2-h_hash_26_cpython.tar.gz (2279530 bytes)
104+ extract /package_tarballs/numpy-1.24.2-py310h6d2fff6_0.tar.gz (3594626 bytes)
105+ Hello from pyjs-code-runner
106+ numpy version: 1.24.2
107+ ```
108+
109+ There are other ways to run the code, for example in a worker thread:
110+
111+ ``` bash
112+ # run in browser-worker-thread backend
46113# in a headless fashion
47114pyjs_code_runner run script \
48115 browser-worker \
@@ -51,8 +118,5 @@ pyjs_code_runner run script \
51118 --script main.py \
52119 --work-dir /tests \
53120 --async-main \
54- --headless
55-
56- ```
57-
58-
121+ --headless
122+ ```
0 commit comments