ipywidgets
ipywidgets copied to clipboard
Cross widgets communication
We are creating a custom widget for spreadsheets inside the jupyter notebook. We have support for cell references in the spreadsheets. We want to implement a functionality where its possible to reference another sheet cell (cross-sheet referencing). For this, we need the ability to communicate to other custom widget instance using messages and ability to listen for the messages. We want some guidance around it. Which functionality of ipywidget can we use to achieve this? Is it possible to notify another widget instance about an event and send message?
There is not much documentation/guidance for cross widget communication (Link to documentation). I tried looking up and reading the source code of ipywidgets. The custom widget class extends from DOMWidget which extens from Widget class. There is send(self, content, buffers=None) method but this only sends the message to the front-end. There is no API to send the message to a different communication channel of different widget instance.
I think you will need to go through the backend to do this, not directly from a frontend view to another frontend view. You can for instance observe a model trait and do something in another model of your widget, which would be displayed in another cell.
I second David's comment here. It's better (and likely more performant) to have any cross-communication done via logic in the backend.
I think you will need to go through the backend to do this, not directly from a frontend view to another frontend view. You can for instance observe a model trait and do something in another model of your widget, which would be displayed in another cell.
@davidbrochart @ibdafna How can I observe other model traits in the current model? Do I need to use widgets.link to link a widget with every other widget? Also, how can I access other widget instances active in the kernel?
Something like:
def func(change):
model2.my_trait = change["new"]
model1.observe(func, names=["my_trait"])