Trying to fix bug with ipywidgets added dynamicaly

Hi everyone!
I’ve been trying to fix a bug that I recently discovered (full description is in this issue Ipywidgets not working if added dynamically · Issue #6439 · holoviz/panel · GitHub). As a new user, i am not able to add more than two links into the topic, thus I’ll use path to the file instead.

Problem defenition
In a nutshell, when ipywidget is created by another widget (e.g. slider is created by clicking a button), new widgets won’t be interactive. The error Wrong widget protocol version: received protocol version '', but was expecting major version '2' appears in browser console.

What have I tried
Since the error only appears in the browser, I desided to start with js part and discovered the following:
Error appears here ipywidgets_bokeh/src/manager.ts, line 129, when registering a new widget in this.kernel.registerCommTarget, msg in callable doesn’t contain any metadata.

I’ve also found that this msg is constructed by BaseComm when calling publish_msg.

In our case, we publish msg when constructing a new widget (by calling _on_widget_constructed in panel/io/ipywidget.py, line 63). Here’s where it gets interesting: when Comm is created (same file, line 88), by default (if primary arg is true), [Comm calls publish_msg in comm lib /comm/base_comm.py, line 68) with info about widget being created. This is fine, as the message contains all the information about the widget. But at the end of _on_widget_constructed, kernel.register_widget also calls publish_msg (inside comm.open()). BUT this time we call it without any args! It’s this call that causes the metadata error, because metadata is not passed for this particular message. I’ve tried to experiment with arguments and was able to fix all errors in browser console, but unfortunately the connection between widgets is still broken. This is when my ideas run out :frowning:

Luck of experience prevents me from moving forward with bug investigation, so I am bagging for help in finding the right direction.
I’ve seen that Phillip has a lot of experience in this field. @philippjfr, could you help me here, please?

Also, why do we have to call comm.open() twice in _on_widget_constructed?

1 Like

Welcome to the community @anton-br

Issue reported in Ipywidgets not working if added dynamically · Issue #6439 · holoviz/panel (github.com). The issues have been solved and fixes will be released with Panel 1.4.0. very soon.