Created
March 18, 2025 13:09
-
-
Save minrk/78853abaeb76d808f5e5b92b38014492 to your computer and use it in GitHub Desktop.
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": "dca9b1c1-c115-497e-9e7a-11a95820d2ea", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Before Widget\n" | |
] | |
}, | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "cbdd500922054b13b5d4485d0955aae1", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"VBox(children=(Button(description='Test', style=ButtonStyle()),))" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "c8ceae9a7d804a2197be10789f187d56", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"Output(layout=Layout(border_bottom='1px solid black', border_left='1px solid black', border_right='1px solid b…" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"After Display\n" | |
] | |
} | |
], | |
"source": [ | |
"import ipywidgets as widgets \n", | |
"from IPython.display import display, clear_output\n", | |
"\n", | |
"print(\"Before Widget\")\n", | |
"\n", | |
"output_counter = 0\n", | |
"out = widgets.Output(layout={'border': '1px solid black'})\n", | |
"\n", | |
"def button_on_click(b):\n", | |
" out.clear_output()\n", | |
" with out:\n", | |
" print(\"Inside Widget\")\n", | |
" global output_counter\n", | |
" output_counter += 1\n", | |
" print(f'Outside Widget {output_counter}')\n", | |
" \n", | |
"btn = widgets.Button(description=\"Test\")\n", | |
"btn.on_click(button_on_click)\n", | |
"\n", | |
"Container = widgets.VBox(children=[btn])\n", | |
"\n", | |
"display(Container,out)\n", | |
"print('After Display')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "64c61282-b2b3-4064-a5c4-91fc8ed3d12e", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "36d00c63fbf04e82ae638c60263da6ea", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"Output()" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"# create an output area to display detached widget output\n", | |
"comm_out = widgets.Output()\n", | |
"display(comm_out)\n", | |
"\n", | |
"# send detatched output created during comm msgs to our output area\n", | |
"ip = get_ipython()\n", | |
"original_comm_msg_handler = ip.kernel.shell_handlers[\"comm_msg\"]\n", | |
"def captured_comm_msg(*args, **kwargs):\n", | |
" with comm_out:\n", | |
" return original_comm_msg_handler(*args, **kwargs)\n", | |
"ip.kernel.shell_handlers[\"comm_msg\"] = captured_comm_msg" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "893e8756-8f19-4764-be46-a3c6f129fe7c", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"output_counter=3\n" | |
] | |
}, | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "bd1445e8185d44fc93bf3c1411a4eaaa", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"Output()" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"print(f\"{output_counter=}\")\n", | |
"# comm_out references the global namespace, so redefining it changes the destination\n", | |
"# for future executions\n", | |
"comm_out = widgets.Output()\n", | |
"comm_out" | |
] | |
} | |
], | |
"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.10" | |
}, | |
"widgets": { | |
"application/vnd.jupyter.widget-state+json": { | |
"state": { | |
"0990662591054c1ebaea674e4ff70714": { | |
"model_module": "@jupyter-widgets/base", | |
"model_module_version": "2.0.0", | |
"model_name": "LayoutModel", | |
"state": {} | |
}, | |
"0abee62a478e4156a26cbd7180f9c213": { | |
"model_module": "@jupyter-widgets/base", | |
"model_module_version": "2.0.0", | |
"model_name": "LayoutModel", | |
"state": { | |
"border_bottom": "1px solid black", | |
"border_left": "1px solid black", | |
"border_right": "1px solid black", | |
"border_top": "1px solid black" | |
} | |
}, | |
"14c4828b2b384e5f9aac020bb7c6cd02": { | |
"model_module": "@jupyter-widgets/controls", | |
"model_module_version": "2.0.0", | |
"model_name": "ButtonModel", | |
"state": { | |
"description": "Test", | |
"layout": "IPY_MODEL_ed36b9856f964c7bbca5e81547ee4605", | |
"style": "IPY_MODEL_2f8c274afdcb4d2f8edbd17b2577da90", | |
"tooltip": null | |
} | |
}, | |
"2f8c274afdcb4d2f8edbd17b2577da90": { | |
"model_module": "@jupyter-widgets/controls", | |
"model_module_version": "2.0.0", | |
"model_name": "ButtonStyleModel", | |
"state": { | |
"font_family": null, | |
"font_size": null, | |
"font_style": null, | |
"font_variant": null, | |
"font_weight": null, | |
"text_color": null, | |
"text_decoration": null | |
} | |
}, | |
"36d00c63fbf04e82ae638c60263da6ea": { | |
"model_module": "@jupyter-widgets/output", | |
"model_module_version": "1.0.0", | |
"model_name": "OutputModel", | |
"state": { | |
"layout": "IPY_MODEL_0990662591054c1ebaea674e4ff70714", | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "Outside Widget 1\nOutside Widget 2\nOutside Widget 3\n" | |
} | |
] | |
} | |
}, | |
"3ddf98cff9f04dfdaae73a454dcc7d36": { | |
"model_module": "@jupyter-widgets/base", | |
"model_module_version": "2.0.0", | |
"model_name": "LayoutModel", | |
"state": {} | |
}, | |
"bd1445e8185d44fc93bf3c1411a4eaaa": { | |
"model_module": "@jupyter-widgets/output", | |
"model_module_version": "1.0.0", | |
"model_name": "OutputModel", | |
"state": { | |
"layout": "IPY_MODEL_3ddf98cff9f04dfdaae73a454dcc7d36", | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "Outside Widget 4\nOutside Widget 5\nOutside Widget 6\n" | |
} | |
] | |
} | |
}, | |
"c8ceae9a7d804a2197be10789f187d56": { | |
"model_module": "@jupyter-widgets/output", | |
"model_module_version": "1.0.0", | |
"model_name": "OutputModel", | |
"state": { | |
"layout": "IPY_MODEL_0abee62a478e4156a26cbd7180f9c213", | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "Inside Widget\n" | |
} | |
] | |
} | |
}, | |
"cbdd500922054b13b5d4485d0955aae1": { | |
"model_module": "@jupyter-widgets/controls", | |
"model_module_version": "2.0.0", | |
"model_name": "VBoxModel", | |
"state": { | |
"children": [ | |
"IPY_MODEL_14c4828b2b384e5f9aac020bb7c6cd02" | |
], | |
"layout": "IPY_MODEL_f271275e49574032a7595bf136f348c7" | |
} | |
}, | |
"ed36b9856f964c7bbca5e81547ee4605": { | |
"model_module": "@jupyter-widgets/base", | |
"model_module_version": "2.0.0", | |
"model_name": "LayoutModel", | |
"state": {} | |
}, | |
"f271275e49574032a7595bf136f348c7": { | |
"model_module": "@jupyter-widgets/base", | |
"model_module_version": "2.0.0", | |
"model_name": "LayoutModel", | |
"state": {} | |
} | |
}, | |
"version_major": 2, | |
"version_minor": 0 | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment