Last active
December 1, 2021 12:50
-
-
Save keckelt/3df73c8b8a3f7c31c43f5079a79bc7ca to your computer and use it in GitHub Desktop.
Update slider handle color based on value.
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": "code", | |
"execution_count": 1, | |
"id": "0dedc90e-2a24-409a-9e78-d7de914f02b9", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from ipywidgets import IntSlider, interact\n", | |
"# Compare https://github.com/jupyter-widgets/ipywidgets/blob/master/docs/source/examples/Widget%20Styling.ipynb " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "e25d8e9b-acc8-4d48-8653-0fdd23163f1d", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"s1 = IntSlider(description='Drag Me')\n", | |
"s1.style.handle_color = 'silver'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "723b040c-186f-4bf4-98e2-022ca7f3a53c", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "3484398951cf40cebeab620e52b8b492", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"interactive(children=(IntSlider(value=0, description='Drag Me', style=SliderStyle(handle_color='silver')), Out…" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"@interact(slider=s1)\n", | |
"def react(slider):\n", | |
" s1.style.handle_color = 'lightgreen' if slider < 50 else 'tomato'\n", | |
" print(slider)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "4ab86834-577d-4d95-a18c-9a8430d3121b", | |
"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.9.7" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interactive Demo:
Compare Widget Styling Notebook: https://github.com/jupyter-widgets/ipywidgets/blob/master/docs/source/examples/Widget%20Styling.ipynb