Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shadiakiki1986/4a35fd7b067bdf543d4f840bea208971 to your computer and use it in GitHub Desktop.
Save shadiakiki1986/4a35fd7b067bdf543d4f840bea208971 to your computer and use it in GitHub Desktop.
t211109_ipywidgets_interact_on_dropdown_updated_from_radio.ipynb
{
"nbformat": 4,
"nbformat_minor": 5,
"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"
},
"colab": {
"name": "t211109_ipywidgets_interact_on_dropdown_updated_from_radio.ipynb",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"93335f2142e4494a852013e7ee52e30f": {
"model_module": "@jupyter-widgets/controls",
"model_name": "RadioButtonsModel",
"model_module_version": "1.5.0",
"state": {
"_options_labels": [
"[1, 2, 3]",
"[3, 2, 1]"
],
"_view_name": "RadioButtonsView",
"style": "IPY_MODEL_708a8f13dc7f4f25bbd3234bb929fb51",
"_dom_classes": [],
"description": "",
"_model_name": "RadioButtonsModel",
"index": 0,
"_view_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_view_count": null,
"disabled": false,
"_view_module_version": "1.5.0",
"description_tooltip": null,
"_model_module": "@jupyter-widgets/controls",
"layout": "IPY_MODEL_be6785a2214d4a11b88cafd6e782cf95"
}
},
"708a8f13dc7f4f25bbd3234bb929fb51": {
"model_module": "@jupyter-widgets/controls",
"model_name": "DescriptionStyleModel",
"model_module_version": "1.5.0",
"state": {
"_view_name": "StyleView",
"_model_name": "DescriptionStyleModel",
"description_width": "",
"_view_module": "@jupyter-widgets/base",
"_model_module_version": "1.5.0",
"_view_count": null,
"_view_module_version": "1.2.0",
"_model_module": "@jupyter-widgets/controls"
}
},
"be6785a2214d4a11b88cafd6e782cf95": {
"model_module": "@jupyter-widgets/base",
"model_name": "LayoutModel",
"model_module_version": "1.2.0",
"state": {
"_view_name": "LayoutView",
"grid_template_rows": null,
"right": null,
"justify_content": null,
"_view_module": "@jupyter-widgets/base",
"overflow": null,
"_model_module_version": "1.2.0",
"_view_count": null,
"flex_flow": null,
"width": null,
"min_width": null,
"border": null,
"align_items": null,
"bottom": null,
"_model_module": "@jupyter-widgets/base",
"top": null,
"grid_column": null,
"overflow_y": null,
"overflow_x": null,
"grid_auto_flow": null,
"grid_area": null,
"grid_template_columns": null,
"flex": null,
"_model_name": "LayoutModel",
"justify_items": null,
"grid_row": null,
"max_height": null,
"align_content": null,
"visibility": null,
"align_self": null,
"height": null,
"min_height": null,
"padding": null,
"grid_auto_rows": null,
"grid_gap": null,
"max_width": null,
"order": null,
"_view_module_version": "1.2.0",
"grid_template_areas": null,
"object_position": null,
"object_fit": null,
"grid_auto_columns": null,
"margin": null,
"display": null,
"left": null
}
},
"e59be0f9ef91405bb1f22eb04c682662": {
"model_module": "@jupyter-widgets/controls",
"model_name": "DropdownModel",
"model_module_version": "1.5.0",
"state": {
"_options_labels": [
"3",
"2",
"1"
],
"_view_name": "DropdownView",
"style": "IPY_MODEL_0e6c3e0f03504e5bba55cadc0924a7c9",
"_dom_classes": [],
"description": "",
"_model_name": "DropdownModel",
"index": 0,
"_view_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_view_count": null,
"disabled": false,
"_view_module_version": "1.5.0",
"description_tooltip": null,
"_model_module": "@jupyter-widgets/controls",
"layout": "IPY_MODEL_7943a24cdc9444d8b084a8002d5c7ddb"
}
},
"0e6c3e0f03504e5bba55cadc0924a7c9": {
"model_module": "@jupyter-widgets/controls",
"model_name": "DescriptionStyleModel",
"model_module_version": "1.5.0",
"state": {
"_view_name": "StyleView",
"_model_name": "DescriptionStyleModel",
"description_width": "",
"_view_module": "@jupyter-widgets/base",
"_model_module_version": "1.5.0",
"_view_count": null,
"_view_module_version": "1.2.0",
"_model_module": "@jupyter-widgets/controls"
}
},
"7943a24cdc9444d8b084a8002d5c7ddb": {
"model_module": "@jupyter-widgets/base",
"model_name": "LayoutModel",
"model_module_version": "1.2.0",
"state": {
"_view_name": "LayoutView",
"grid_template_rows": null,
"right": null,
"justify_content": null,
"_view_module": "@jupyter-widgets/base",
"overflow": null,
"_model_module_version": "1.2.0",
"_view_count": null,
"flex_flow": null,
"width": null,
"min_width": null,
"border": null,
"align_items": null,
"bottom": null,
"_model_module": "@jupyter-widgets/base",
"top": null,
"grid_column": null,
"overflow_y": null,
"overflow_x": null,
"grid_auto_flow": null,
"grid_area": null,
"grid_template_columns": null,
"flex": null,
"_model_name": "LayoutModel",
"justify_items": null,
"grid_row": null,
"max_height": null,
"align_content": null,
"visibility": null,
"align_self": null,
"height": null,
"min_height": null,
"padding": null,
"grid_auto_rows": null,
"grid_gap": null,
"max_width": null,
"order": null,
"_view_module_version": "1.2.0",
"grid_template_areas": null,
"object_position": null,
"object_fit": null,
"grid_auto_columns": null,
"margin": null,
"display": null,
"left": null
}
},
"90e77e91da6043ada75cb67d1bffd60f": {
"model_module": "@jupyter-widgets/controls",
"model_name": "DropdownModel",
"model_module_version": "1.5.0",
"state": {
"_options_labels": [
"3",
"2",
"1"
],
"_view_name": "DropdownView",
"style": "IPY_MODEL_0af214d5fc4447c2af847e10dc0e5ac7",
"_dom_classes": [],
"description": "",
"_model_name": "DropdownModel",
"index": 1,
"_view_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_view_count": null,
"disabled": false,
"_view_module_version": "1.5.0",
"description_tooltip": null,
"_model_module": "@jupyter-widgets/controls",
"layout": "IPY_MODEL_56d5e6c4780d49f3bb9eab98b8e72d2a"
}
},
"0af214d5fc4447c2af847e10dc0e5ac7": {
"model_module": "@jupyter-widgets/controls",
"model_name": "DescriptionStyleModel",
"model_module_version": "1.5.0",
"state": {
"_view_name": "StyleView",
"_model_name": "DescriptionStyleModel",
"description_width": "",
"_view_module": "@jupyter-widgets/base",
"_model_module_version": "1.5.0",
"_view_count": null,
"_view_module_version": "1.2.0",
"_model_module": "@jupyter-widgets/controls"
}
},
"56d5e6c4780d49f3bb9eab98b8e72d2a": {
"model_module": "@jupyter-widgets/base",
"model_name": "LayoutModel",
"model_module_version": "1.2.0",
"state": {
"_view_name": "LayoutView",
"grid_template_rows": null,
"right": null,
"justify_content": null,
"_view_module": "@jupyter-widgets/base",
"overflow": null,
"_model_module_version": "1.2.0",
"_view_count": null,
"flex_flow": null,
"width": null,
"min_width": null,
"border": null,
"align_items": null,
"bottom": null,
"_model_module": "@jupyter-widgets/base",
"top": null,
"grid_column": null,
"overflow_y": null,
"overflow_x": null,
"grid_auto_flow": null,
"grid_area": null,
"grid_template_columns": null,
"flex": null,
"_model_name": "LayoutModel",
"justify_items": null,
"grid_row": null,
"max_height": null,
"align_content": null,
"visibility": null,
"align_self": null,
"height": null,
"min_height": null,
"padding": null,
"grid_auto_rows": null,
"grid_gap": null,
"max_width": null,
"order": null,
"_view_module_version": "1.2.0",
"grid_template_areas": null,
"object_position": null,
"object_fit": null,
"grid_auto_columns": null,
"margin": null,
"display": null,
"left": null
}
},
"f558231bbb644dc5a3835a83888ea14c": {
"model_module": "@jupyter-widgets/controls",
"model_name": "VBoxModel",
"model_module_version": "1.5.0",
"state": {
"_view_name": "VBoxView",
"_dom_classes": [
"widget-interact"
],
"_model_name": "VBoxModel",
"_view_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_view_count": null,
"_view_module_version": "1.5.0",
"box_style": "",
"layout": "IPY_MODEL_d21541431dd14d45a34e1e593425b108",
"_model_module": "@jupyter-widgets/controls",
"children": [
"IPY_MODEL_6a920b6fc89f47b0aea9b34f6d0f1ba2",
"IPY_MODEL_588c60b16acc4c0a9b09db6babb05591",
"IPY_MODEL_a54b39227bb24355a615c75ace4fcfff"
]
}
},
"d21541431dd14d45a34e1e593425b108": {
"model_module": "@jupyter-widgets/base",
"model_name": "LayoutModel",
"model_module_version": "1.2.0",
"state": {
"_view_name": "LayoutView",
"grid_template_rows": null,
"right": null,
"justify_content": null,
"_view_module": "@jupyter-widgets/base",
"overflow": null,
"_model_module_version": "1.2.0",
"_view_count": null,
"flex_flow": null,
"width": null,
"min_width": null,
"border": null,
"align_items": null,
"bottom": null,
"_model_module": "@jupyter-widgets/base",
"top": null,
"grid_column": null,
"overflow_y": null,
"overflow_x": null,
"grid_auto_flow": null,
"grid_area": null,
"grid_template_columns": null,
"flex": null,
"_model_name": "LayoutModel",
"justify_items": null,
"grid_row": null,
"max_height": null,
"align_content": null,
"visibility": null,
"align_self": null,
"height": null,
"min_height": null,
"padding": null,
"grid_auto_rows": null,
"grid_gap": null,
"max_width": null,
"order": null,
"_view_module_version": "1.2.0",
"grid_template_areas": null,
"object_position": null,
"object_fit": null,
"grid_auto_columns": null,
"margin": null,
"display": null,
"left": null
}
},
"6a920b6fc89f47b0aea9b34f6d0f1ba2": {
"model_module": "@jupyter-widgets/controls",
"model_name": "RadioButtonsModel",
"model_module_version": "1.5.0",
"state": {
"_options_labels": [
"[1, 2, 3]",
"[3, 2, 1]"
],
"_view_name": "RadioButtonsView",
"style": "IPY_MODEL_28024c580ea14367a0b71b6a324f8961",
"_dom_classes": [],
"description": "v_radio",
"_model_name": "RadioButtonsModel",
"index": 1,
"_view_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_view_count": null,
"disabled": false,
"_view_module_version": "1.5.0",
"description_tooltip": null,
"_model_module": "@jupyter-widgets/controls",
"layout": "IPY_MODEL_695771b53ba64473aa720914bec6f214"
}
},
"588c60b16acc4c0a9b09db6babb05591": {
"model_module": "@jupyter-widgets/controls",
"model_name": "DropdownModel",
"model_module_version": "1.5.0",
"state": {
"_options_labels": [
"3",
"2",
"1"
],
"_view_name": "DropdownView",
"style": "IPY_MODEL_ba62d9849481411c8bafaf7c1f7e4539",
"_dom_classes": [],
"description": "v_dropdown",
"_model_name": "DropdownModel",
"index": 2,
"_view_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_view_count": null,
"disabled": false,
"_view_module_version": "1.5.0",
"description_tooltip": null,
"_model_module": "@jupyter-widgets/controls",
"layout": "IPY_MODEL_592de1a80b444e2b84608853927f0176"
}
},
"a54b39227bb24355a615c75ace4fcfff": {
"model_module": "@jupyter-widgets/output",
"model_name": "OutputModel",
"model_module_version": "1.0.0",
"state": {
"_view_name": "OutputView",
"msg_id": "",
"_dom_classes": [],
"_model_name": "OutputModel",
"outputs": [],
"_view_module": "@jupyter-widgets/output",
"_model_module_version": "1.0.0",
"_view_count": null,
"_view_module_version": "1.0.0",
"layout": "IPY_MODEL_9aa592dd41be48af8d09336c7eed592a",
"_model_module": "@jupyter-widgets/output"
}
},
"28024c580ea14367a0b71b6a324f8961": {
"model_module": "@jupyter-widgets/controls",
"model_name": "DescriptionStyleModel",
"model_module_version": "1.5.0",
"state": {
"_view_name": "StyleView",
"_model_name": "DescriptionStyleModel",
"description_width": "",
"_view_module": "@jupyter-widgets/base",
"_model_module_version": "1.5.0",
"_view_count": null,
"_view_module_version": "1.2.0",
"_model_module": "@jupyter-widgets/controls"
}
},
"695771b53ba64473aa720914bec6f214": {
"model_module": "@jupyter-widgets/base",
"model_name": "LayoutModel",
"model_module_version": "1.2.0",
"state": {
"_view_name": "LayoutView",
"grid_template_rows": null,
"right": null,
"justify_content": null,
"_view_module": "@jupyter-widgets/base",
"overflow": null,
"_model_module_version": "1.2.0",
"_view_count": null,
"flex_flow": null,
"width": null,
"min_width": null,
"border": null,
"align_items": null,
"bottom": null,
"_model_module": "@jupyter-widgets/base",
"top": null,
"grid_column": null,
"overflow_y": null,
"overflow_x": null,
"grid_auto_flow": null,
"grid_area": null,
"grid_template_columns": null,
"flex": null,
"_model_name": "LayoutModel",
"justify_items": null,
"grid_row": null,
"max_height": null,
"align_content": null,
"visibility": null,
"align_self": null,
"height": null,
"min_height": null,
"padding": null,
"grid_auto_rows": null,
"grid_gap": null,
"max_width": null,
"order": null,
"_view_module_version": "1.2.0",
"grid_template_areas": null,
"object_position": null,
"object_fit": null,
"grid_auto_columns": null,
"margin": null,
"display": null,
"left": null
}
},
"ba62d9849481411c8bafaf7c1f7e4539": {
"model_module": "@jupyter-widgets/controls",
"model_name": "DescriptionStyleModel",
"model_module_version": "1.5.0",
"state": {
"_view_name": "StyleView",
"_model_name": "DescriptionStyleModel",
"description_width": "",
"_view_module": "@jupyter-widgets/base",
"_model_module_version": "1.5.0",
"_view_count": null,
"_view_module_version": "1.2.0",
"_model_module": "@jupyter-widgets/controls"
}
},
"592de1a80b444e2b84608853927f0176": {
"model_module": "@jupyter-widgets/base",
"model_name": "LayoutModel",
"model_module_version": "1.2.0",
"state": {
"_view_name": "LayoutView",
"grid_template_rows": null,
"right": null,
"justify_content": null,
"_view_module": "@jupyter-widgets/base",
"overflow": null,
"_model_module_version": "1.2.0",
"_view_count": null,
"flex_flow": null,
"width": null,
"min_width": null,
"border": null,
"align_items": null,
"bottom": null,
"_model_module": "@jupyter-widgets/base",
"top": null,
"grid_column": null,
"overflow_y": null,
"overflow_x": null,
"grid_auto_flow": null,
"grid_area": null,
"grid_template_columns": null,
"flex": null,
"_model_name": "LayoutModel",
"justify_items": null,
"grid_row": null,
"max_height": null,
"align_content": null,
"visibility": null,
"align_self": null,
"height": null,
"min_height": null,
"padding": null,
"grid_auto_rows": null,
"grid_gap": null,
"max_width": null,
"order": null,
"_view_module_version": "1.2.0",
"grid_template_areas": null,
"object_position": null,
"object_fit": null,
"grid_auto_columns": null,
"margin": null,
"display": null,
"left": null
}
},
"9aa592dd41be48af8d09336c7eed592a": {
"model_module": "@jupyter-widgets/base",
"model_name": "LayoutModel",
"model_module_version": "1.2.0",
"state": {
"_view_name": "LayoutView",
"grid_template_rows": null,
"right": null,
"justify_content": null,
"_view_module": "@jupyter-widgets/base",
"overflow": null,
"_model_module_version": "1.2.0",
"_view_count": null,
"flex_flow": null,
"width": null,
"min_width": null,
"border": null,
"align_items": null,
"bottom": null,
"_model_module": "@jupyter-widgets/base",
"top": null,
"grid_column": null,
"overflow_y": null,
"overflow_x": null,
"grid_auto_flow": null,
"grid_area": null,
"grid_template_columns": null,
"flex": null,
"_model_name": "LayoutModel",
"justify_items": null,
"grid_row": null,
"max_height": null,
"align_content": null,
"visibility": null,
"align_self": null,
"height": null,
"min_height": null,
"padding": null,
"grid_auto_rows": null,
"grid_gap": null,
"max_width": null,
"order": null,
"_view_module_version": "1.2.0",
"grid_template_areas": null,
"object_position": null,
"object_fit": null,
"grid_auto_columns": null,
"margin": null,
"display": null,
"left": null
}
},
"04ec1825b3f44a4894c9fc71722bc3ff": {
"model_module": "@jupyter-widgets/output",
"model_name": "OutputModel",
"model_module_version": "1.0.0",
"state": {
"_view_name": "OutputView",
"msg_id": "",
"_dom_classes": [],
"_model_name": "OutputModel",
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"1\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"1\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"1\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"1\n"
]
}
],
"_view_module": "@jupyter-widgets/output",
"_model_module_version": "1.0.0",
"_view_count": null,
"_view_module_version": "1.0.0",
"layout": "IPY_MODEL_c5e01d388e8d4874982d9c6019f211e0",
"_model_module": "@jupyter-widgets/output"
}
},
"c5e01d388e8d4874982d9c6019f211e0": {
"model_module": "@jupyter-widgets/base",
"model_name": "LayoutModel",
"model_module_version": "1.2.0",
"state": {
"_view_name": "LayoutView",
"grid_template_rows": null,
"right": null,
"justify_content": null,
"_view_module": "@jupyter-widgets/base",
"overflow": null,
"_model_module_version": "1.2.0",
"_view_count": null,
"flex_flow": null,
"width": null,
"min_width": null,
"border": null,
"align_items": null,
"bottom": null,
"_model_module": "@jupyter-widgets/base",
"top": null,
"grid_column": null,
"overflow_y": null,
"overflow_x": null,
"grid_auto_flow": null,
"grid_area": null,
"grid_template_columns": null,
"flex": null,
"_model_name": "LayoutModel",
"justify_items": null,
"grid_row": null,
"max_height": null,
"align_content": null,
"visibility": null,
"align_self": null,
"height": null,
"min_height": null,
"padding": null,
"grid_auto_rows": null,
"grid_gap": null,
"max_width": null,
"order": null,
"_view_module_version": "1.2.0",
"grid_template_areas": null,
"object_position": null,
"object_fit": null,
"grid_auto_columns": null,
"margin": null,
"display": null,
"left": null
}
}
}
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/shadiakiki1986/4a35fd7b067bdf543d4f840bea208971/t211109_ipywidgets_interact_on_dropdown_updated_from_radio.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "L_cu2Fj3Egb1"
},
"source": [
"https://github.com/jupyter-widgets/ipywidgets/issues/3309"
],
"id": "L_cu2Fj3Egb1"
},
{
"cell_type": "code",
"metadata": {
"id": "F0kqJ7qu0tts"
},
"source": [
"# Instead of sed patch, override the _propagate_options function\n",
"#!cp \\\n",
"# /usr/local/lib/python3.7/dist-packages/ipywidgets/widgets/widget_selection.py \\\n",
"# /usr/local/lib/python3.7/dist-packages/ipywidgets/widgets/widget_selection.py.bkp"
],
"id": "F0kqJ7qu0tts",
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "uLNfZqAWzm_U",
"outputId": "7b2f6b62-51fe-4b9a-f8ff-b2aa7bd642bc"
},
"source": [
"#FNSED=\"/usr/local/lib/python3.7/dist-packages/ipywidgets/widgets/widget_selection.py\"\n",
"#!cp {FNSED}.bkp $FNSED\n",
"#!grep \"self.index = 0\" $FNSED\n",
"#!sed -i \"s/self.index = 0/self.index = self.options.index(self.value) if (self.value in self.options) else 0/g\" $FNSED\n",
"#!grep \"self.index = \" $FNSED\n",
"#\n",
"## RESTART"
],
"id": "uLNfZqAWzm_U",
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
" self.index = 0\n",
" print(self.value); self.index = self.options.index(self.value) if (self.value in self.options) else 0\n",
" self.index = None\n",
" self.index = index\n",
" self.index = index\n",
" self.index = ()\n",
" self.index = index\n",
" self.index = index\n",
" self.index = (0, 0)\n"
]
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "5f54iDww0bwn"
},
"source": [
""
],
"id": "5f54iDww0bwn",
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "PSNH3QrcxQIP"
},
"source": [
"import ipywidgets as widgets"
],
"id": "PSNH3QrcxQIP",
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 91,
"referenced_widgets": [
"93335f2142e4494a852013e7ee52e30f",
"708a8f13dc7f4f25bbd3234bb929fb51",
"be6785a2214d4a11b88cafd6e782cf95",
"e59be0f9ef91405bb1f22eb04c682662",
"0e6c3e0f03504e5bba55cadc0924a7c9",
"7943a24cdc9444d8b084a8002d5c7ddb"
]
},
"id": "7736c6fa",
"outputId": "7117d241-fb79-4af9-f4b4-65e4d9d8eb8c"
},
"source": [
"# Example 1: a very fast flicker is observed in the dropdown, from 1 to 3 and back to 1\n",
"w_radio = widgets.RadioButtons(options=[[1,2,3], [3,2,1]], index=0)\n",
"w_dropdown = widgets.Dropdown(options=[1,2,3], index=0)\n",
"\n",
"\"\"\"\n",
"def update_dropdown(e):\n",
" options_new = w_radio.value\n",
" with w_dropdown.hold_trait_notifications():\n",
" w_dropdown._options_full = options_new\n",
" w_dropdown._propagate_options(None)\n",
" \n",
"w_radio.observe(update_dropdown, 'value') \n",
"\"\"\"\n",
"\n",
"display(w_radio, w_dropdown)"
],
"id": "7736c6fa",
"execution_count": null,
"outputs": [
{
"output_type": "display_data",
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "93335f2142e4494a852013e7ee52e30f",
"version_minor": 0,
"version_major": 2
},
"text/plain": [
"RadioButtons(options=([1, 2, 3], [3, 2, 1]), value=[1, 2, 3])"
]
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "e59be0f9ef91405bb1f22eb04c682662",
"version_minor": 0,
"version_major": 2
},
"text/plain": [
"Dropdown(options=(1, 2, 3), value=1)"
]
},
"metadata": {}
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1ucP-7Il6w0O"
},
"source": [
"SImplify by just having dropdown"
],
"id": "1ucP-7Il6w0O"
},
{
"cell_type": "code",
"metadata": {
"id": "M_s48ZxK594v"
},
"source": [
"w_dropdown = widgets.Dropdown(options=[1,2,3], index=0)\n",
"assert w_dropdown.value==1\n",
"#w_dropdown.options = widgets.widget_selection._make_options([3,2,1])\n",
"w_dropdown.options = [3,2,1]\n",
"assert w_dropdown.value==3 # want this to stay 1"
],
"id": "M_s48ZxK594v",
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 67,
"referenced_widgets": [
"90e77e91da6043ada75cb67d1bffd60f",
"0af214d5fc4447c2af847e10dc0e5ac7",
"56d5e6c4780d49f3bb9eab98b8e72d2a"
]
},
"id": "74qlsZEd8zKk",
"outputId": "c18ab45d-0632-4445-e501-6c8371795a16"
},
"source": [
"# Again, with my own patch\n",
"w_dropdown = widgets.Dropdown(options=[1,2,3], index=0)\n",
"\n",
"assert w_dropdown.value==1\n",
"\n",
"display(w_dropdown)\n",
"\n",
"print(\"sleep 5 to observe any value change\")\n",
"import time\n",
"time.sleep(5)\n",
"\n",
"def ipywidgets__propagate_options(self, change):\n",
" # To override the built-in function at\n",
" # https://github.com/jupyter-widgets/ipywidgets/blob/9326d28c474cb6d2b58876d469489a73e98f903a/python/ipywidgets/ipywidgets/widgets/widget_selection.py#L214\n",
" # Check issue https://github.com/jupyter-widgets/ipywidgets/issues/3309\n",
" \"Set the values and labels, and select the first option if we aren't initializing\"\n",
" options = self._options_full\n",
" self.set_trait('_options_labels', tuple(i[0] for i in options))\n",
" self._options_values = tuple(i[1] for i in options)\n",
" #print(f\"Options changed: {self.options} -> {self._options_values}\")\n",
"\n",
" if self.index is None:\n",
" # Do nothing, we don't want to force a selection if\n",
" # the options list changed\n",
" return\n",
"\n",
" if self._initializing_traits_ is not True:\n",
" if len(options) > 0:\n",
" # >>>>>>>>>>>>>>>>>>>>>>>>\n",
" # try to keep the same value\n",
" if self.value in self._options_values:\n",
" index_new = self._options_values.index(self.value)\n",
" #print(f\"try to keep same value {self.value} by changing index from {self.index} to {index_new}\")\n",
" # Either update index directly, or use the _propagate_index function\n",
" self.index = index_new\n",
" return\n",
" # >>>>>>>>>>>>>>>>>>>>>>>>\n",
" \n",
" if self.index == 0:\n",
" # Explicitly trigger the observers to pick up the new value and\n",
" # label. Just setting the value would not trigger the observers\n",
" # since traitlets thinks the value hasn't changed.\n",
" self._notify_trait('index', 0, 0)\n",
" else:\n",
" self.index = 0\n",
" else:\n",
" self.index = None\n",
"\n",
"\n",
"def ipywidgets__set_options(w, l):\n",
" # avoid \"w_dropdown.options=...\" so as to use my own propagate function\n",
" # self.options = l\n",
" # \"hold_sync\" necessary to avoid flicker of selected value from 1 to 3 to 1\n",
" with self.hold_sync():\n",
" self._options_full = widgets.widget_selection._make_options(l)\n",
" ipywidgets__propagate_options(w, None)\n",
" \n",
"\n",
"ipywidgets__set_options(w_dropdown, [3,2,1])\n",
"assert w_dropdown.value==1 # indeed this stays 1\n",
"\n",
"# Visually confirm that the displayed value is still 1"
],
"id": "74qlsZEd8zKk",
"execution_count": null,
"outputs": [
{
"output_type": "display_data",
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "90e77e91da6043ada75cb67d1bffd60f",
"version_minor": 0,
"version_major": 2
},
"text/plain": [
"Dropdown(options=(1, 2, 3), value=1)"
]
},
"metadata": {}
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"sleep 5 to observe any value change\n"
]
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "jn-ikwvv7f_H"
},
"source": [
""
],
"id": "jn-ikwvv7f_H",
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "0ef63172"
},
"source": [
"Below will display in the output: 1,3,3,1,1,etc\n",
"whereas I would have just wanted it to display: 1\n",
"\n",
"Check issue from notebook 03-portfolio_building (xai app) where the top-level themes dropdown is causing too much calculations frmo the PortfolioStats... class"
],
"id": "0ef63172"
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 185,
"referenced_widgets": [
"f558231bbb644dc5a3835a83888ea14c",
"d21541431dd14d45a34e1e593425b108",
"6a920b6fc89f47b0aea9b34f6d0f1ba2",
"588c60b16acc4c0a9b09db6babb05591",
"a54b39227bb24355a615c75ace4fcfff",
"28024c580ea14367a0b71b6a324f8961",
"695771b53ba64473aa720914bec6f214",
"ba62d9849481411c8bafaf7c1f7e4539",
"592de1a80b444e2b84608853927f0176",
"9aa592dd41be48af8d09336c7eed592a",
"04ec1825b3f44a4894c9fc71722bc3ff",
"c5e01d388e8d4874982d9c6019f211e0"
]
},
"id": "a4a041ea",
"outputId": "ccb4a86f-21e7-4833-cfc5-7e6fc7aea9c8"
},
"source": [
"# Example 2: works with manual, but not with throttling or debouncing or hold_sync or hold_...\n",
"# Update: now works without manual given the ipywidgets__set_options function above that uses hold_sync\n",
"w_radio = widgets.RadioButtons(options=[[1,2,3], [3,2,1]])\n",
"w_dropdown = widgets.Dropdown(options=[1,2,3], index=0)\n",
"w_output = widgets.Output()\n",
"\n",
"def do_interact(v_radio, v_dropdown):\n",
" with w_output:\n",
" print(v_dropdown)\n",
" \n",
"w_interactive = widgets.interactive(do_interact, {'manual': False}, v_radio=w_radio, v_dropdown=w_dropdown)\n",
"\n",
"def update_dropdown(e):\n",
" ipywidgets__set_options(w_dropdown, w_radio.value)\n",
"\n",
" \n",
"w_radio.observe(update_dropdown, 'value') \n",
"\n",
"display(w_interactive, w_output)"
],
"id": "a4a041ea",
"execution_count": null,
"outputs": [
{
"output_type": "display_data",
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "f558231bbb644dc5a3835a83888ea14c",
"version_minor": 0,
"version_major": 2
},
"text/plain": [
"interactive(children=(RadioButtons(description='v_radio', options=([1, 2, 3], [3, 2, 1]), value=[1, 2, 3]), Dr…"
]
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "04ec1825b3f44a4894c9fc71722bc3ff",
"version_minor": 0,
"version_major": 2
},
"text/plain": [
"Output()"
]
},
"metadata": {}
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "n6aTzpfJFnvx"
},
"source": [
""
],
"id": "n6aTzpfJFnvx",
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "YjEBH_3XFn3Z"
},
"source": [
""
],
"id": "YjEBH_3XFn3Z",
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "9cbpsgLIFn_R"
},
"source": [
"\n",
"\n",
"# debounce decorator\n",
"# from https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Events.html#Debouncing\n",
"import asyncio\n",
"from time import time\n",
"\n",
"def throttle(wait):\n",
" \"\"\" Decorator that prevents a function from being called\n",
" more than once every wait period. \"\"\"\n",
" def decorator(fn):\n",
" time_of_last_call = 0\n",
" scheduled, timer = False, None\n",
" new_args, new_kwargs = None, None\n",
" def throttled(*args, **kwargs):\n",
" nonlocal new_args, new_kwargs, time_of_last_call, scheduled, timer\n",
" def call_it():\n",
" nonlocal new_args, new_kwargs, time_of_last_call, scheduled, timer\n",
" time_of_last_call = time()\n",
" fn(*new_args, **new_kwargs)\n",
" scheduled = False\n",
" time_since_last_call = time() - time_of_last_call\n",
" new_args, new_kwargs = args, kwargs\n",
" if not scheduled:\n",
" scheduled = True\n",
" new_wait = max(0, wait - time_since_last_call)\n",
" timer = Timer(new_wait, call_it)\n",
" timer.start()\n",
" return throttled\n",
" return decorator\n"
],
"id": "9cbpsgLIFn_R",
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment