# Trying to fix bug with ipywidgets added dynamicaly

**URL:** <https://discourse.holoviz.org/t/trying-to-fix-bug-with-ipywidgets-added-dynamicaly/7001>\
**Category:** Panel\
**Tags:** bokeh, ipywidgets\
**Created:** [March 21, 2024, 10:17pm UTC](https://discourse.holoviz.org/t/trying-to-fix-bug-with-ipywidgets-added-dynamicaly/7001 "2024-03-21T22:17:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![anton-br](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/anton-br/32/6428_2.png) [@anton-br](https://discourse.holoviz.org/u/anton-br)\
**Post date:** [March 21, 2024, 10:17pm UTC](https://discourse.holoviz.org/t/trying-to-fix-bug-with-ipywidgets-added-dynamicaly/7001/1 "2024-03-21T22:17:17Z")

</div>

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](https://github.com/holoviz/panel/issues/6439)). 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](https://github.com/ipython/ipykernel/blob/830829fd344849c69027a7a198f745e7b84bcb28/ipykernel/comm/comm.py#L19) 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 ☹

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`?

---

<div class="post-metadata">

**Author:** ![Marc](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/marc/32/24_2.png) [@Marc](https://discourse.holoviz.org/u/Marc)\
**Post date:** [March 22, 2024, 6:36pm UTC](https://discourse.holoviz.org/t/trying-to-fix-bug-with-ipywidgets-added-dynamicaly/7001/2 "2024-03-22T18:36:33Z")

</div>

Welcome to the community @anton-br

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