Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
itepifanio committed Feb 15, 2024
1 parent 1f344db commit cc39f2c
Showing 1 changed file with 47 additions and 29 deletions.
76 changes: 47 additions & 29 deletions nbs/00_comm.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,47 @@
"metadata": {},
"outputs": [],
"source": [
"#| hide\n",
"import solara\n",
"import ipywidgets as widgets\n",
"\n",
"import ipykernel\n",
"from ipykernel.comm import Comm"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Ipykernel](https://github.com/ipython/ipykernel/tree/main) employs the `Comm` class for facilitating communication between the front-end and back-end. It builds on a [base Comm](https://github.com/ipython/comm/tree/main) implementation, which includes a `send` function for messaging the front-end. Monkey-patching this `send` function presents a direct approach to intercept `Comm` communications."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"_original_send = Comm.send\n",
"\n",
"def _patched_send(self, data=None, metadata=None, buffers=None):\n",
" widget = widgets.Widget.widgets.get(self.comm_id)\n",
" widget_type = type(widget).__name__ if widget else \"Unknown\"\n",
" print(f\"Comm message sent by {widget_type} ({self.comm_id}): {data}\")\n",
"\n",
" _original_send(self, data, metadata, buffers)\n",
"\n",
"Comm.send = _patched_send"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Testing\n",
"\n",
"The following cells displays ipywidgets and solara example of monitoring state changes. Interact with the following widgets to intercept its state changes."
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -30,7 +64,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "958c6ca38a444c4ab33cc750577e6a51",
"model_id": "994c3642550c42c39481c1930cf8a038",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -42,29 +76,6 @@
"output_type": "display_data"
}
],
"source": [
"from ipykernel.comm import Comm\n",
"from IPython import get_ipython\n",
"\n",
"_original_send = Comm.send\n",
"\n",
"def _patched_send(self, data=None, metadata=None, buffers=None):\n",
" try:\n",
" widget = widgets.Widget.widgets.get(self.comm_id)\n",
" widget_type = type(widget).__name__ if widget else \"Unknown\"\n",
" print(f\"Comm message sent by {widget_type} ({self.comm_id}): {data}\")\n",
" except:\n",
" print(f\"Comm message sent by unknown widget ({self.comm_id}): {data}\")\n",
" _original_send(self, data, metadata, buffers)\n",
"\n",
"Comm.send = _patched_send"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"slider = widgets.IntSlider(value=7, min=0, max=10, step=1, description='Test Slider:')\n",
"display(slider)"
Expand All @@ -79,21 +90,21 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Comm message sent by VBox (a182262bcad6402fb12ad0cfa0620898): {'method': 'update', 'state': {'children': ['IPY_MODEL_353c8460ac98451390b1293dd03fb349']}, 'buffer_paths': []}\n"
"Comm message sent by VBox (9ba04b3acebb4e688f3395979a666d03): {'method': 'update', 'state': {'children': ['IPY_MODEL_9a3b850216e54b369dc4b482ab5a5104']}, 'buffer_paths': []}\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/tmp/ipykernel_15337/3691898007.py:8: DeprecationWarning: Widget.widgets is deprecated.\n",
"/tmp/ipykernel_14614/897649624.py:4: DeprecationWarning: Widget.widgets is deprecated.\n",
" widget = widgets.Widget.widgets.get(self.comm_id)\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "a182262bcad6402fb12ad0cfa0620898",
"model_id": "9ba04b3acebb4e688f3395979a666d03",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -110,8 +121,15 @@
],
"source": [
"int_value = solara.reactive(42)\n",
"solara.SliderInt(\"Some integer\", value=int_value, min=-10, max=120)"
"solara.SliderInt(\"Another Test Slider:\", value=int_value, min=-10, max=120)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down

0 comments on commit cc39f2c

Please sign in to comment.