Skip to content

Commit 9cfd541

Browse files
committed
Add demo notebooks and environment
1 parent dc2c664 commit 9cfd541

File tree

8 files changed

+552
-1
lines changed

8 files changed

+552
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ node_modules/
22
output/
33
package-lock.json
44
subshells.pdf
5+
__pycache__/
6+
.ipynb_checkpoints/

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,15 @@ npm run pdf
2525
```
2626

2727
which generates `subshells.pdf`.
28+
29+
## Serve demos locally
30+
31+
Create and activate a `micromamba` (or `pixi` or `conda` etc) environment and run `jupyter lab`:
32+
33+
34+
```bash
35+
micromamba create -f environment.yml
36+
micromamba activate subshells
37+
cd notebooks
38+
jupyter lab
39+
```

environment.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: subshells
2+
channels:
3+
- conda-forge
4+
dependencies:
5+
- ipykernel >= 7.1.0
6+
- ipywidgets
7+
- jupyterlab >= 4.4.0
8+
- numpy
9+
- pillow
10+
- python

index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ mermaid diagram similar to that in https://github.com/ipython/ipykernel/pull/124
158158
- `ipykernel` is the reference implementation.
159159
- Later releases fix problems, mostly caused by new shell channel thread rather than subshells themselves
160160
- `jupyterlab >= 4.4.0`
161-
- For `ipywidgets` support need `jupyterlab >= 4.4.0`
161+
- For `ipywidgets` support need `jupyterlab >= 4.4.4`
162162
- For `%matplotlib ipympl` support need `ipympl >= 0.9.8`
163163

164164
---

notebooks/demo_1_simple.ipynb

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "090c6acc-e56f-4c1f-8024-c413639a1724",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"a = [2, 3, 4, 5]\n",
11+
"a"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": null,
17+
"id": "a62bcaad-0ef5-4c9e-9e8c-ce9a239d2db1",
18+
"metadata": {
19+
"editable": true,
20+
"slideshow": {
21+
"slide_type": ""
22+
},
23+
"tags": []
24+
},
25+
"outputs": [],
26+
"source": [
27+
"import os, threading as t\n",
28+
"print({\"pid\": os.getpid(), \"thread_count\": t.active_count(), \"thread_id\": t.current_thread()})"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": null,
34+
"id": "c72811d3-ebf7-44ee-a787-c3c501d61745",
35+
"metadata": {
36+
"editable": true,
37+
"slideshow": {
38+
"slide_type": ""
39+
},
40+
"tags": []
41+
},
42+
"outputs": [],
43+
"source": [
44+
"%subshell"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": null,
50+
"id": "34e331d0-1772-45ce-83ed-5b388225bcc4",
51+
"metadata": {},
52+
"outputs": [],
53+
"source": [
54+
"# Simulate a long-running task.\n",
55+
"import time\n",
56+
"for b in range(10):\n",
57+
" print('b =', b)\n",
58+
" time.sleep(1)"
59+
]
60+
},
61+
{
62+
"cell_type": "code",
63+
"execution_count": null,
64+
"id": "a94a83c7-e120-4c34-b465-3c7e633b375d",
65+
"metadata": {},
66+
"outputs": [],
67+
"source": []
68+
}
69+
],
70+
"metadata": {
71+
"kernelspec": {
72+
"display_name": "Python 3 (ipykernel)",
73+
"language": "python",
74+
"name": "python3"
75+
},
76+
"language_info": {
77+
"codemirror_mode": {
78+
"name": "ipython",
79+
"version": 3
80+
},
81+
"file_extension": ".py",
82+
"mimetype": "text/x-python",
83+
"name": "python",
84+
"nbconvert_exporter": "python",
85+
"pygments_lexer": "ipython3",
86+
"version": "3.14.0"
87+
}
88+
},
89+
"nbformat": 4,
90+
"nbformat_minor": 5
91+
}

notebooks/demo_2_raytracer.ipynb

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "1f697483-ce2b-49cc-9e3d-feec5c1de98c",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from IPython.display import display\n",
11+
"import time\n",
12+
"from raytracer import RayTracer\n",
13+
"\n",
14+
"ray_tracer = RayTracer(700, 500)"
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"id": "bf4a9a3a-5ac7-4d96-9c42-35314729655a",
20+
"metadata": {},
21+
"source": [
22+
"## Run ray tracer\n",
23+
"\n",
24+
"### With just a single thread in this notebook we cannot plot the results until the calculation has finished"
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": null,
30+
"id": "49866843-8013-4110-8a40-44be22722a76",
31+
"metadata": {},
32+
"outputs": [],
33+
"source": [
34+
"ray_tracer.run_random()"
35+
]
36+
},
37+
{
38+
"cell_type": "markdown",
39+
"id": "9c312380-f822-4fd3-9ec4-fed5e407b80a",
40+
"metadata": {},
41+
"source": [
42+
"## Plot ray traced image"
43+
]
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": null,
48+
"id": "e6219511-ac58-414e-80e8-00b9aa126531",
49+
"metadata": {},
50+
"outputs": [],
51+
"source": [
52+
"handle = display(ray_tracer.as_image(), display_id=True)"
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": null,
58+
"id": "5eb89836-3147-4778-b1da-915a8dfeff5e",
59+
"metadata": {},
60+
"outputs": [],
61+
"source": [
62+
"ray_tracer.reset()"
63+
]
64+
},
65+
{
66+
"cell_type": "raw",
67+
"id": "24068e26-4c1b-49e3-acd5-f514010968cc",
68+
"metadata": {},
69+
"source": [
70+
"# code to run in subshell\n",
71+
"while not ray_tracer.completed:\n",
72+
" handle.update(ray_tracer.as_image())\n",
73+
" time.sleep(0.5)\n",
74+
"handle.update(ray_tracer.as_image())"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": null,
80+
"id": "f8b1bbd4-fc98-49eb-aafd-bb1899bb5536",
81+
"metadata": {},
82+
"outputs": [],
83+
"source": []
84+
}
85+
],
86+
"metadata": {
87+
"kernelspec": {
88+
"display_name": "Python 3 (ipykernel)",
89+
"language": "python",
90+
"name": "python3"
91+
},
92+
"language_info": {
93+
"codemirror_mode": {
94+
"name": "ipython",
95+
"version": 3
96+
},
97+
"file_extension": ".py",
98+
"mimetype": "text/x-python",
99+
"name": "python",
100+
"nbconvert_exporter": "python",
101+
"pygments_lexer": "ipython3",
102+
"version": "3.14.0"
103+
}
104+
},
105+
"nbformat": 4,
106+
"nbformat_minor": 5
107+
}

notebooks/demo_3_ipywidgets.ipynb

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "a3600d23-63b9-4a8f-86be-7c2a453ad462",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from ipywidgets import HBox, IntSlider\n",
11+
"import time\n",
12+
"from traitlets import link"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": 2,
18+
"id": "cf42830b-bdc4-48ac-977d-9abaf0e19aef",
19+
"metadata": {},
20+
"outputs": [
21+
{
22+
"data": {
23+
"application/vnd.jupyter.widget-view+json": {
24+
"model_id": "84129ec96ab44ec6afcfd4d3774271bc",
25+
"version_major": 2,
26+
"version_minor": 0
27+
},
28+
"text/plain": [
29+
"HBox(children=(IntSlider(value=0, orientation='vertical'), IntSlider(value=0, orientation='vertical'), IntSlid…"
30+
]
31+
},
32+
"execution_count": 2,
33+
"metadata": {},
34+
"output_type": "execute_result"
35+
}
36+
],
37+
"source": [
38+
"n = 10\n",
39+
"sliders = [IntSlider(orientation='vertical') for i in range(n)]\n",
40+
"HBox(sliders)"
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": 3,
46+
"id": "131fad3f-40ca-47b1-9089-58925686dd4a",
47+
"metadata": {},
48+
"outputs": [],
49+
"source": [
50+
"# Link all the sliders in python\n",
51+
"links = [link((sliders[i], 'value'), (sliders[i+1], 'value')) for i in range(n-1)]"
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": 4,
57+
"id": "eb2d735d-5ecc-49eb-b154-7a5d34601699",
58+
"metadata": {},
59+
"outputs": [
60+
{
61+
"name": "stdout",
62+
"output_type": "stream",
63+
"text": [
64+
"0 0\n",
65+
"0 0\n",
66+
"0 0\n",
67+
"0 0\n",
68+
"0 0\n",
69+
"0 0\n",
70+
"0 0\n",
71+
"0 0\n",
72+
"0 0\n",
73+
"0 0\n"
74+
]
75+
}
76+
],
77+
"source": [
78+
"for i in range(10):\n",
79+
" time.sleep(1)\n",
80+
" print(sliders[0].value, sliders[-1].value)"
81+
]
82+
},
83+
{
84+
"cell_type": "code",
85+
"execution_count": null,
86+
"id": "45ccce73-ec13-4a11-84b1-2d74b42a89dc",
87+
"metadata": {},
88+
"outputs": [],
89+
"source": []
90+
}
91+
],
92+
"metadata": {
93+
"kernelspec": {
94+
"display_name": "Python 3 (ipykernel)",
95+
"language": "python",
96+
"name": "python3"
97+
},
98+
"language_info": {
99+
"codemirror_mode": {
100+
"name": "ipython",
101+
"version": 3
102+
},
103+
"file_extension": ".py",
104+
"mimetype": "text/x-python",
105+
"name": "python",
106+
"nbconvert_exporter": "python",
107+
"pygments_lexer": "ipython3",
108+
"version": "3.14.0"
109+
}
110+
},
111+
"nbformat": 4,
112+
"nbformat_minor": 5
113+
}

0 commit comments

Comments
 (0)