= BaseComm.send
_original_send
def _patched_send(self, data=None, metadata=None, buffers=None):
= widget_module._instances.get(self.comm_id)
widget = type(widget).__name__ if widget else "Unknown"
widget_type print(f"Comm message sent by {widget_type} ({self.comm_id}): {data}")
self, data, metadata, buffers)
_original_send(
= _patched_send # type: ignore BaseComm.send
Comm
Exploring Comm API to intersept their messages
Ipykernel employs the Comm
class for facilitating communication between the front-end and back-end. It builds on a base Comm implementation, which includes a send
function for messaging the front-end. Monkey-patching this send
function presents a direct approach to intercept Comm
communications.
Testing
The following cells displays ipywidgets and solara example of monitoring state changes. Interact with the following widgets to intercept its state changes.
= widgets.IntSlider(value=7, min=0, max=10, step=1, description='Test Slider:')
slider display(slider)
= solara.reactive(42)
int_value "Another Test Slider:", value=int_value, min=-10, max=120) solara.SliderInt(