Skip to content

Commit

Permalink
Add example on ipywidgets and user interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsamuelwrites committed Mar 6, 2024
1 parent f3c1c26 commit 8295552
Showing 1 changed file with 172 additions and 0 deletions.
172 changes: 172 additions & 0 deletions examples/interaction.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "0a839229-63b1-4d62-8336-dfec92e72eda",
"metadata": {},
"source": [
"# Using ipywidgets for user interaction"
]
},
{
"cell_type": "markdown",
"id": "7b3b4b32-0867-4454-952d-0199ad972957",
"metadata": {},
"source": [
"## Installation"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "091f57f2-34dd-4ca7-bdc8-ed5ef19005aa",
"metadata": {},
"outputs": [],
"source": [
"!pip install ipywidgets"
]
},
{
"cell_type": "markdown",
"id": "79396b89-219f-4cf0-a719-c4a46af2bc61",
"metadata": {},
"source": [
"## Using ipywidgets"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "868f5fae-1e30-404d-a70c-ced026efa105",
"metadata": {},
"outputs": [],
"source": [
"import ipywidgets as widgets"
]
},
{
"cell_type": "markdown",
"id": "ddda3c77-2744-4ef0-8b0d-6f7592e556df",
"metadata": {},
"source": [
"## Get all the images"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "250b2c53-77b7-4fee-bc55-649546afff34",
"metadata": {},
"outputs": [],
"source": [
"from os import listdir"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "561debf0-5bf6-4ad2-88d0-26d7be585aa8",
"metadata": {},
"outputs": [],
"source": [
"images = []\n",
"\n",
"for file in listdir(\"../images\"):\n",
" if \".png\" in file:\n",
" images.append(\"../images/\" + file)"
]
},
{
"cell_type": "markdown",
"id": "1a159184-31a1-4628-8a4d-ffcae3d4a5e5",
"metadata": {},
"source": [
"## Using GridSpectLayout"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e5954bd2-5f13-41b7-ad7f-1b21d0e4634e",
"metadata": {},
"outputs": [],
"source": [
"from ipywidgets import GridspecLayout, Image, interact\n",
"paths = []\n",
"\n",
"checkboxes = [widgets.Checkbox(value=False, description='Favorite') for _ in range(len(images))]\n",
"\n",
"# Create the GridspecLayout widget\n",
"layout = GridspecLayout(n_columns=2, n_rows=len(images), width='400px')\n",
"for i, (img, checkbox) in enumerate(zip(images, checkboxes)):\n",
" file = open(img, \"rb\")\n",
" image = file.read()\n",
" image_widget = widgets.Image(\n",
" value=image,\n",
" format='png',\n",
" width=100,\n",
" height=100,\n",
" )\n",
" layout[i,0] = image_widget\n",
" layout[i, 1] = checkbox\n",
"\n",
"# Button to get selected images\n",
"button = widgets.Button(description=\"Select\")\n",
"\n",
"# Output widget to display selected images\n",
"output = widgets.Output()\n",
"\n",
"# Function to get selected images\n",
"def get_selected_images(btn):\n",
" global paths\n",
" paths = []\n",
" selected_paths = [images[i] for i, checkbox in enumerate(checkboxes) if checkbox.value]\n",
" with output:\n",
" output.clear_output()\n",
" print(\"Selected Images:\")\n",
" for path in selected_paths:\n",
" print(path)\n",
" paths.append(path)\n",
" print(paths)\n",
" \n",
"\n",
"# Link button click event to function\n",
"button.on_click(get_selected_images)\n",
"\n",
"# Display the layout and button\n",
"display(layout, button, output)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8dbf7a77-41e0-430c-98b8-133cffaf2681",
"metadata": {},
"outputs": [],
"source": [
"paths"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit 8295552

Please sign in to comment.