Skip to content

Commit 097a484

Browse files
authored
Merge pull request #2 from jtpio/updates
Update to the new `empack` and `pyjs`
2 parents 91d736d + d337117 commit 097a484

File tree

8 files changed

+280
-199
lines changed

8 files changed

+280
-199
lines changed

.github/workflows/main.yml

+24-52
Original file line numberDiff line numberDiff line change
@@ -5,73 +5,45 @@ on:
55
push:
66
pull_request:
77

8-
98
defaults:
109
run:
1110
shell: bash -l {0}
1211

13-
1412
jobs:
15-
16-
build_page:
17-
13+
build:
1814
runs-on: ubuntu-latest
19-
20-
strategy:
21-
fail-fast: false
22-
matrix:
23-
emsdk_ver: ["3.1.2"]
24-
2515
steps:
26-
- uses: actions/checkout@v2
27-
16+
- uses: actions/checkout@v3
2817

2918
- name: Install mamba
3019
uses: mamba-org/provision-with-micromamba@main
3120
with:
32-
environment-file: build-env.yaml
21+
environment-file: build-environment.yaml
3322
environment-name: pyjs-build-env
34-
micromamba-version: "0.23.2"
23+
micromamba-version: "1.4.2"
3524

36-
37-
- name: build page
25+
- name: Build the page
3826
run: |
39-
micromamba activate pyjs-build-env
40-
41-
42-
mkdir build
43-
pushd build
44-
45-
micromamba create -n pyjs-web-env \
46-
--platform=emscripten-32 \
47-
-c https://repo.mamba.pm/emscripten-forge \
48-
-c https://repo.mamba.pm/conda-forge \
49-
--yes \
50-
-f $GITHUB_WORKSPACE/web-env.yaml
27+
python build.py --env-name pyjs-web-env
5128
29+
- name: Upload artifact
30+
uses: actions/upload-pages-artifact@v1
31+
with:
32+
path: ./build
5233

53-
# donload empack config
54-
EMPACK_CONFIG=empack_config.yaml
55-
echo "donwload empack config"
56-
wget -O $EMPACK_CONFIG https://raw.githubusercontent.com/emscripten-forge/recipes/main/empack_config.yaml
57-
58-
59-
# pack the environment in a js/data file
60-
empack pack env --env-prefix $MAMBA_ROOT_PREFIX/envs/pyjs-web-env --outname python_data --config empack_config.yaml
61-
62-
63-
# copy relevant files from build to page
64-
cp python_data.js ../page
65-
cp python_data.data ../page
66-
34+
deploy:
35+
needs: build
36+
if: github.ref == 'refs/heads/main'
37+
permissions:
38+
pages: write
39+
id-token: write
6740

68-
# copy relevant files from env to page
69-
cp $MAMBA_ROOT_PREFIX/envs/pyjs-web-env/lib_js/pyjs/pyjs_runtime_browser* ../page
41+
environment:
42+
name: github-pages
43+
url: ${{ steps.deployment.outputs.page_url }}
7044

71-
- name: Deploy
72-
uses: peaceiris/actions-gh-pages@v3
73-
if: github.ref == 'refs/heads/main'
74-
with:
75-
github_token: ${{ secrets.GITHUB_TOKEN }}
76-
publish_dir: ./page
77-
enable_jekyll: false
45+
runs-on: ubuntu-latest
46+
steps:
47+
- name: Deploy to GitHub Pages
48+
id: deployment
49+
uses: actions/deploy-pages@v1

README.md

+41-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,42 @@
11
# sample-python-repl
2-
sample-python-repl
2+
3+
A demo repository for create a Python REPL that runs in the browser.
4+
5+
## Requirements
6+
7+
You will need to install [micromamba](https://mamba.readthedocs.io/en/latest/installation.html#micromamba) to build the application.
8+
9+
## Usage
10+
11+
First clone the repository:
12+
13+
```bash
14+
git clone https://github.com/emscripten-forge/sample-python-repl
15+
cd sample-python-repl
16+
```
17+
18+
Then create a new environment that will be used for building the REPL:
19+
20+
```bash
21+
# create the environment
22+
micromamba create -n pyjs-build-env -f build-environment.yaml -y
23+
# activate the environment
24+
micromamba activate pyjs-build-env
25+
```
26+
27+
In the environment, run the build script:
28+
29+
```bash
30+
python build.py
31+
```
32+
33+
This create a new environment that will include the dependencies for running the REPL in the browser (runtime).
34+
The script also copies the relevant files to the `build` folder.
35+
36+
You can then start an HTTP server to serve the files in the `build` folder:
37+
38+
```bash
39+
python -m http.server --directory build
40+
```
41+
42+
And open the REPL in your browser at http://localhost:8000.

build-env.yaml renamed to build-environment.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ channels:
33
- conda-forge
44
dependencies:
55
- python
6-
- empack >= 1.1.0
7-
- emsdk >=3.1.11
6+
- empack >= 3.0.0,<4.0.0

build.py

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import argparse
2+
import os
3+
import shutil
4+
import subprocess
5+
6+
from pathlib import Path
7+
8+
9+
def create_env(env_name):
10+
subprocess.run(
11+
[
12+
"micromamba",
13+
"create",
14+
"-n",
15+
env_name,
16+
"--platform=emscripten-32",
17+
"--yes",
18+
"-f",
19+
"web-environment.yaml",
20+
],
21+
check=True,
22+
)
23+
24+
25+
def pack_env(env_prefix, output_dir):
26+
subprocess.run(
27+
[
28+
"empack",
29+
"pack",
30+
"env",
31+
"--env-prefix",
32+
str(env_prefix),
33+
"--relocate-prefix",
34+
"/",
35+
"--no-use-cache",
36+
"--outdir",
37+
output_dir,
38+
],
39+
check=True,
40+
)
41+
42+
43+
def copy_files(src_dir, dst_dir):
44+
for path in src_dir.glob("*"):
45+
shutil.copy(str(path), str(dst_dir))
46+
47+
48+
def main(env_name):
49+
create_env(env_name)
50+
51+
# create build/work dir
52+
build_dir = Path("build")
53+
build_dir.mkdir(exist_ok=True)
54+
55+
# pack the environment in a js/data file
56+
env_prefix = Path(
57+
os.environ.get("MAMBA_ROOT_PREFIX", f"/opt/conda/envs/{env_name}")
58+
)
59+
pack_env(env_prefix / "envs" / env_name, output_dir=str(build_dir))
60+
61+
# copy relevant files
62+
page_dir = Path("page")
63+
copy_files(page_dir, build_dir)
64+
65+
# copy relevant files from env to page
66+
lib_js_dir = env_prefix / "envs" / env_name / "lib_js" / "pyjs"
67+
copy_files(lib_js_dir, build_dir)
68+
69+
70+
if __name__ == "__main__":
71+
parser = argparse.ArgumentParser(
72+
description="Install wasm env and copy relevant files."
73+
)
74+
parser.add_argument(
75+
"--env-name",
76+
metavar="ENV_NAME",
77+
type=str,
78+
required=True,
79+
help="name of the mamba environment",
80+
)
81+
args = parser.parse_args()
82+
main(args.env_name)

build_page.sh

-36
This file was deleted.

page/index.html

+68-37
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,78 @@
1-
<!doctype html>
1+
<!DOCTYPE html>
22
<html lang="en-us">
3-
<head>
4-
<title>python3.11-wasm embeded via pybind11 build by boa</title>
5-
</head>
3+
<head>
4+
<title>Sample Python REPL running in the browser</title>
5+
</head>
66

7-
<!-- Required meta tags -->
8-
<meta charset="utf-8">
9-
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
7+
<!-- Required meta tags -->
8+
<meta charset="utf-8" />
9+
<meta
10+
name="viewport"
11+
content="width=device-width, initial-scale=1, shrink-to-fit=no"
12+
/>
1013

11-
<!-- Bootstrap CSS -->
12-
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
14+
<!-- Bootstrap CSS -->
15+
<link
16+
rel="stylesheet"
17+
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
18+
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
19+
crossorigin="anonymous"
20+
/>
1321

14-
<body>
22+
<body>
23+
<!-- Optional JavaScript -->
24+
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
25+
<script
26+
src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
27+
integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
28+
crossorigin="anonymous"
29+
></script>
30+
<script
31+
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"
32+
integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
33+
crossorigin="anonymous"
34+
></script>
35+
<script
36+
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
37+
integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
38+
crossorigin="anonymous"
39+
></script>
1540

41+
<!-- <h3>python3.11-wasm embeded via pybind11 build by boa</h3> -->
42+
<form name="myform">
43+
<td><textarea name="inputtext"></textarea></td>
1644

45+
<button
46+
type="button"
47+
class="btn btn-outline-primary"
48+
disabled
49+
id="run_button"
50+
name="run_button"
51+
>
52+
Run
53+
</button>
1754

18-
<!-- Optional JavaScript -->
19-
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
20-
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
21-
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
22-
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
55+
<p id="p1"></p>
2356

24-
<!-- <h3>python3.11-wasm embeded via pybind11 build by boa</h3> -->
25-
<form name="myform">
26-
<td><textarea name="inputtext"></textarea></td>
57+
<td><textarea name="outputtext"></textarea></td>
58+
</form>
2759

28-
<button type="button" class="btn btn-outline-primary" disabled id="run_button" name="run_button">Run</button>
29-
30-
<p id="p1"></p>
31-
32-
<td><textarea name="outputtext"></textarea></td>
33-
</form>
34-
35-
36-
<script type="application/javascript" src="pyjs_runtime_browser.js"></script>
37-
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.32.0/codemirror.min.js"></script>
38-
<script src='https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.13.4/mode/python/python.js'></script>
39-
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.32.0/codemirror.css" />
40-
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.13.4/theme/monokai.css'/>
41-
<script type="application/javascript" src="script.js"></script>
42-
<script type="application/javascript">
43-
44-
45-
</script>
46-
</body>
60+
<script
61+
type="application/javascript"
62+
src="pyjs_runtime_browser.js"
63+
></script>
64+
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.32.0/codemirror.min.js"></script>
65+
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.13.4/mode/python/python.js"></script>
66+
<link
67+
rel="stylesheet"
68+
href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.32.0/codemirror.css"
69+
/>
70+
<link
71+
rel="stylesheet"
72+
href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.13.4/theme/monokai.css"
73+
/>
74+
<script src="pyjs_runtime_browser.js"></script>
75+
<script type="application/javascript" src="script.js"></script>
76+
<script type="application/javascript"></script>
77+
</body>
4778
</html>

0 commit comments

Comments
 (0)