Skip to content

Commit 8295552

Browse files
Add example on ipywidgets and user interaction
1 parent f3c1c26 commit 8295552

File tree

1 file changed

+172
-0
lines changed

1 file changed

+172
-0
lines changed

examples/interaction.ipynb

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "0a839229-63b1-4d62-8336-dfec92e72eda",
6+
"metadata": {},
7+
"source": [
8+
"# Using ipywidgets for user interaction"
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"id": "7b3b4b32-0867-4454-952d-0199ad972957",
14+
"metadata": {},
15+
"source": [
16+
"## Installation"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": null,
22+
"id": "091f57f2-34dd-4ca7-bdc8-ed5ef19005aa",
23+
"metadata": {},
24+
"outputs": [],
25+
"source": [
26+
"!pip install ipywidgets"
27+
]
28+
},
29+
{
30+
"cell_type": "markdown",
31+
"id": "79396b89-219f-4cf0-a719-c4a46af2bc61",
32+
"metadata": {},
33+
"source": [
34+
"## Using ipywidgets"
35+
]
36+
},
37+
{
38+
"cell_type": "code",
39+
"execution_count": null,
40+
"id": "868f5fae-1e30-404d-a70c-ced026efa105",
41+
"metadata": {},
42+
"outputs": [],
43+
"source": [
44+
"import ipywidgets as widgets"
45+
]
46+
},
47+
{
48+
"cell_type": "markdown",
49+
"id": "ddda3c77-2744-4ef0-8b0d-6f7592e556df",
50+
"metadata": {},
51+
"source": [
52+
"## Get all the images"
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": null,
58+
"id": "250b2c53-77b7-4fee-bc55-649546afff34",
59+
"metadata": {},
60+
"outputs": [],
61+
"source": [
62+
"from os import listdir"
63+
]
64+
},
65+
{
66+
"cell_type": "code",
67+
"execution_count": null,
68+
"id": "561debf0-5bf6-4ad2-88d0-26d7be585aa8",
69+
"metadata": {},
70+
"outputs": [],
71+
"source": [
72+
"images = []\n",
73+
"\n",
74+
"for file in listdir(\"../images\"):\n",
75+
" if \".png\" in file:\n",
76+
" images.append(\"../images/\" + file)"
77+
]
78+
},
79+
{
80+
"cell_type": "markdown",
81+
"id": "1a159184-31a1-4628-8a4d-ffcae3d4a5e5",
82+
"metadata": {},
83+
"source": [
84+
"## Using GridSpectLayout"
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": null,
90+
"id": "e5954bd2-5f13-41b7-ad7f-1b21d0e4634e",
91+
"metadata": {},
92+
"outputs": [],
93+
"source": [
94+
"from ipywidgets import GridspecLayout, Image, interact\n",
95+
"paths = []\n",
96+
"\n",
97+
"checkboxes = [widgets.Checkbox(value=False, description='Favorite') for _ in range(len(images))]\n",
98+
"\n",
99+
"# Create the GridspecLayout widget\n",
100+
"layout = GridspecLayout(n_columns=2, n_rows=len(images), width='400px')\n",
101+
"for i, (img, checkbox) in enumerate(zip(images, checkboxes)):\n",
102+
" file = open(img, \"rb\")\n",
103+
" image = file.read()\n",
104+
" image_widget = widgets.Image(\n",
105+
" value=image,\n",
106+
" format='png',\n",
107+
" width=100,\n",
108+
" height=100,\n",
109+
" )\n",
110+
" layout[i,0] = image_widget\n",
111+
" layout[i, 1] = checkbox\n",
112+
"\n",
113+
"# Button to get selected images\n",
114+
"button = widgets.Button(description=\"Select\")\n",
115+
"\n",
116+
"# Output widget to display selected images\n",
117+
"output = widgets.Output()\n",
118+
"\n",
119+
"# Function to get selected images\n",
120+
"def get_selected_images(btn):\n",
121+
" global paths\n",
122+
" paths = []\n",
123+
" selected_paths = [images[i] for i, checkbox in enumerate(checkboxes) if checkbox.value]\n",
124+
" with output:\n",
125+
" output.clear_output()\n",
126+
" print(\"Selected Images:\")\n",
127+
" for path in selected_paths:\n",
128+
" print(path)\n",
129+
" paths.append(path)\n",
130+
" print(paths)\n",
131+
" \n",
132+
"\n",
133+
"# Link button click event to function\n",
134+
"button.on_click(get_selected_images)\n",
135+
"\n",
136+
"# Display the layout and button\n",
137+
"display(layout, button, output)"
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": null,
143+
"id": "8dbf7a77-41e0-430c-98b8-133cffaf2681",
144+
"metadata": {},
145+
"outputs": [],
146+
"source": [
147+
"paths"
148+
]
149+
}
150+
],
151+
"metadata": {
152+
"kernelspec": {
153+
"display_name": "Python 3 (ipykernel)",
154+
"language": "python",
155+
"name": "python3"
156+
},
157+
"language_info": {
158+
"codemirror_mode": {
159+
"name": "ipython",
160+
"version": 3
161+
},
162+
"file_extension": ".py",
163+
"mimetype": "text/x-python",
164+
"name": "python",
165+
"nbconvert_exporter": "python",
166+
"pygments_lexer": "ipython3",
167+
"version": "3.11.4"
168+
}
169+
},
170+
"nbformat": 4,
171+
"nbformat_minor": 5
172+
}

0 commit comments

Comments
 (0)