Instantly share code, notes, and snippets.
Last active
March 8, 2025 16:25
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save fomightez/fac1b5f6d61c1ceb873fe81409c109d7 to your computer and use it in GitHub Desktop.
In reference to https://stackoverflow.com/a/79494011/8508004 which may be deleted as violating current policy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "id": "de80422d-7e2c-48b1-8f12-7fdff18ec046", | |
| "metadata": {}, | |
| "source": [ | |
| "## Cleaning up AI generated code some \n", | |
| "\n", | |
| "In reference to https://stackoverflow.com/a/79494011/8508004 which may be deleted as violating current policy.\n", | |
| "\n", | |
| "Original version offered [there](https://stackoverflow.com/a/79494011/8508004):\n", | |
| "\n", | |
| "```python\n", | |
| "import ipywidgets as widgets\n", | |
| "import IPython.display\n", | |
| "import functools\n", | |
| "\n", | |
| "def _handler(button: widgets.Button, text: widgets.Text, output: widgets.Output) -> None:\n", | |
| " # Clear the output widget before displaying new content\n", | |
| " output.clear_output()\n", | |
| "\n", | |
| " # Display the label inside the output widget\n", | |
| " with output:\n", | |
| " label = widgets.Label(value=f\"Doing stuff with input '{text.value}'.\")\n", | |
| " IPython.display.display(label)\n", | |
| "\n", | |
| " # Do a bunch more stuff, including drawing new UI elements.\n", | |
| " # For example:\n", | |
| " progress = widgets.IntProgress(value=50, min=0, max=100, description=\"Progress:\")\n", | |
| " IPython.display.display(progress)\n", | |
| " \n", | |
| "# Create the input and button widgets\n", | |
| "prompt = widgets.Text(description=\"Input: \")\n", | |
| "button = widgets.Button(description=\"Run\")\n", | |
| "\n", | |
| "# Create an Output widget to which the output can be directed \n", | |
| "output = widgets.Output()\n", | |
| "\n", | |
| "# Bind the handler to the button click event\n", | |
| "\n", | |
| "# button.on_click(functools.partial(_handler, prompt, button, output))\n", | |
| "button.on_click(functools.partial(_handler, text=prompt, output=output))\n", | |
| "\n", | |
| "# Display the input, button, and output widgets\n", | |
| "prompt_box = widgets.HBox([prompt, button])\n", | |
| "IPython.display.display(prompt_box)\n", | |
| "IPython.display.display(output)\n", | |
| "```\n", | |
| "\n", | |
| "\n", | |
| "You can just use `display()` and not `import IPython.display` or use `IPython.display.display`. It's already there\n", | |
| "\n", | |
| "### Better version offered:" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "id": "95673eb5-809b-4154-8355-b28ff5db4e5b", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "application/vnd.jupyter.widget-view+json": { | |
| "model_id": "7dc7ff1ce466405d9cddfe556fe06249", | |
| "version_major": 2, | |
| "version_minor": 0 | |
| }, | |
| "text/plain": [ | |
| "HBox(children=(Text(value='', description='Input: '), Button(description='Run', style=ButtonStyle())))" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "output_type": "display_data" | |
| }, | |
| { | |
| "data": { | |
| "application/vnd.jupyter.widget-view+json": { | |
| "model_id": "75b617ffc32e4731bea1010dbddfeb22", | |
| "version_major": 2, | |
| "version_minor": 0 | |
| }, | |
| "text/plain": [ | |
| "Output()" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "output_type": "display_data" | |
| } | |
| ], | |
| "source": [ | |
| "import ipywidgets as widgets\n", | |
| "import functools\n", | |
| "\n", | |
| "# Create an Output widget \n", | |
| "output = widgets.Output()\n", | |
| "\n", | |
| "def _handler(button: widgets.Button, text: widgets.Text, output: widgets.Output) -> None:\n", | |
| " # Clear the output widget before displaying new content\n", | |
| " output.clear_output()\n", | |
| "\n", | |
| " # Display the label inside the output widget\n", | |
| " with output:\n", | |
| " label = widgets.Label(value=f\"Doing stuff with input '{text.value}'.\")\n", | |
| " display(label)\n", | |
| "\n", | |
| " # Do a bunch more stuff, including drawing new UI elements.\n", | |
| " # For example:\n", | |
| " progress = widgets.IntProgress(value=50, min=0, max=100, description=\"Progress:\")\n", | |
| " display(progress)\n", | |
| " \n", | |
| "# Create the input and button widgets\n", | |
| "prompt = widgets.Text(description=\"Input: \")\n", | |
| "button = widgets.Button(description=\"Run\")\n", | |
| "\n", | |
| "# Bind the handler to the button click event\n", | |
| "\n", | |
| "# button.on_click(functools.partial(_handler, prompt, button, output))\n", | |
| "button.on_click(functools.partial(_handler, text=prompt, output=output))\n", | |
| "\n", | |
| "# Display the input, button, and output widgets\n", | |
| "prompt_box = widgets.HBox([prompt, button])\n", | |
| "display(prompt_box, output)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "24fb94e7-1029-4dbc-9d30-ea4b713988f3", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "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.10.14" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 5 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment