# How to link custom widgets

**URL:** https://discourse.jupyter.org/t/how-to-link-custom-widgets/8438
**Category:** Widgets
**Created:** [March 20, 2021, 7:57pm UTC](https://discourse.jupyter.org/t/how-to-link-custom-widgets/8438 "2021-03-20T19:57:19Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![zaphod](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/zaphod/32/4991_2.png) [@zaphod](https://discourse.jupyter.org/u/zaphod)
#### Post date: [March 20, 2021, 7:57pm UTC](https://discourse.jupyter.org/t/how-to-link-custom-widgets/8438/1 "2021-03-20T19:57:19Z")

</div>

Hi everyone,

I want to create a custom widget that can access the front-end model of another widget so that they can share functions and data. For example, given:

```
c1 = MainWidget()
c2 = SubWidget(c1)

```

How can I handle the c1 argument so that the SubWidget’s front-end model has an attribute like `this.mainwidget` that points to the MainWidget’s `this.model`?

Something like this is done in the [link widget](https://github.com/jupyter-widgets/ipywidgets/blob/51322341d4f6d88449f9dbf6d538c60de6d23543/packages/controls/src/widget_link.ts) when they assign two passed models to `this.sourceModel` and `this.targetModel`, but I can’t figure out how it is done.

Thanks in advance!

---

<div class="post-metadata">

### Author: ![jtp](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/jtp/32/19100_2.png) [@jtp](https://discourse.jupyter.org/u/jtp)
#### Post date: [March 22, 2021, 8:59am UTC](https://discourse.jupyter.org/t/how-to-link-custom-widgets/8438/2 "2021-03-22T08:59:10Z")

</div>

Thanks @zaphod.

This example by @ianhi might provide some pointers: [custom-ipywidget-howto/jslink-examples.ipynb at 3721f28c943de769538766cdaf41af02cba244d9 · ianhi/custom-ipywidget-howto · GitHub](https://github.com/ianhi/custom-ipywidget-howto/blob/3721f28c943de769538766cdaf41af02cba244d9/jslink-custom-widgets/examples/jslink-examples.ipynb)

---

<div class="post-metadata">

### Author: ![zaphod](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/zaphod/32/4991_2.png) [@zaphod](https://discourse.jupyter.org/u/zaphod)
#### Post date: [March 22, 2021, 11:12am UTC](https://discourse.jupyter.org/t/how-to-link-custom-widgets/8438/3 "2021-03-22T11:12:38Z")

</div>

Thanks @jtp

The example shows how to connect widget attributes after creation. However, I would like to connect them at initialization. Here is one example that I found to work:

On the Python side, I store the main widget id as a traitlet:

```
@ipywidgets.register
class SubWidget(ipywidgets.DOMWidget):
    
    # ... _view_name etc.
    mainwidget_id = traitlets.Unicode().tag(sync=True)
    
    def __init__ (self, mainwidget, **kwargs):
        self.mainwidget_id = mainwidget._model_id
        super(). __init__ (**kwargs)

```

On the Javascript side, I use the `widget_manager` to get the model from the id:

```
var SubWidget = widgets.DOMWidgetModel.extend({

    // ... defaults: _view_name etc. 

	initialize: function (attributes, options) {
        widgets.DOMWidgetModel.prototype.initialize.call(this, attributes, options);
		let promise = this.widget_manager.get_model(this.get('mainwidget_id'))
		promise.then(this.connect_mainwidget.bind(this));
    },

    connect_mainwidget: function(mainwidget) {
		this.mainwidget = mainwidget
	},

});

```

If there is a better way, please let me know!

---

<div class="post-metadata">

### Author: ![ianhi](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/ianhi/32/2498_2.png) [@ianhi](https://discourse.jupyter.org/u/ianhi)
#### Post date: [March 22, 2021, 11:33pm UTC](https://discourse.jupyter.org/t/how-to-link-custom-widgets/8438/4 "2021-03-22T23:33:46Z")

</div>

Hi @zaphod theres another related example for what you want: [custom-ipywidget-howto/connecting-widgets at main · ianhi/custom-ipywidget-howto · GitHub](https://github.com/ianhi/custom-ipywidget-howto/tree/main/connecting-widgets#connecting-widgets)

Though it looks as though you’ve found a very similar solution. The only difference is that I send the `model_id` as a message rather than as a trait, but I think that both ways are valid. In fact there are likely advantages to using a trait.

If you can think of a way that that example could be improved to make this clearer please feel free to open a PR 🙂

---

<div class="post-metadata">

### Author: ![ianhi](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/ianhi/32/2498_2.png) [@ianhi](https://discourse.jupyter.org/u/ianhi)
#### Post date: [March 22, 2021, 11:35pm UTC](https://discourse.jupyter.org/t/how-to-link-custom-widgets/8438/5 "2021-03-22T23:35:39Z")

</div>

Oh actually there is a seemingly substantive difference. You’ve accessed the private attribute `_model_id` while the version in the linked example uses the public `widget.comm.comm_id` which may be better future proofed.

---

<div class="post-metadata">

### Author: ![zaphod](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/zaphod/32/4991_2.png) [@zaphod](https://discourse.jupyter.org/u/zaphod)
#### Post date: [March 23, 2021, 8:10am UTC](https://discourse.jupyter.org/t/how-to-link-custom-widgets/8438/6 "2021-03-23T08:10:30Z")

</div>

Thanks @ianhi, that’s a great example repository!

What do you think could be the (dis-)advantages of using a traitlet?

I think with sending a message the queue of events is a bit more straightforward
