# Observer pattern does not work using a widget

**URL:** <https://discourse.jupyter.org/t/observer-pattern-does-not-work-using-a-widget/12840>\
**Category:** Widgets\
**Tags:** how-to\
**Created:** [February 1, 2022, 11:06am UTC](https://discourse.jupyter.org/t/observer-pattern-does-not-work-using-a-widget/12840 "2022-02-01T11:06:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgunstone](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/jgunstone/32/8661_2.png) [@jgunstone](https://discourse.jupyter.org/u/jgunstone)\
**Post date:** [February 1, 2022, 11:06am UTC](https://discourse.jupyter.org/t/observer-pattern-does-not-work-using-a-widget/12840/1 "2022-02-01T11:06:19Z")

</div>

this is a resolved issue on Github so i’m obviously just being slow…

> <https://github.com/jupyter-widgets/ipywidgets/issues/2078>
>
> Trying to reproduce the example in http://traitlets.readthedocs.io/en/stable/usi…ng\_traitlets.html#observe, but using a widget as a class property, the behavior changes and does not observe on changes
> 
> from ipywidgets import Label
> from traitlets import HasTraits, Unicode, observe
> class Foo(HasTraits):
> bar = Label()
> baz = Unicode()
>         
> @observe('bar')
> def \_observe\_bar(self, change):
> print(change\['old'\])
> print(change\['new'\])
> 
> f = Foo()
> f.bar = Label('hello') # No printing

but i’m trying to watch a widget using the @observe decorator and can’t seem to get it working…

```python
from traitlets import HasTraits, observe, Int, Unicode, Instance
import ipywidgets as widgets
class Foo(HasTraits):
    bar = Instance(klass=widgets.IntText, default_value=widgets.IntText())
    baz = Unicode()

    @observe('bar', "value")
    def _observe_bar(self, change):
        print('asdfasdf')
        print(change['old'])
        print(change['new'])
f = Foo()

```

then when i change the value of bar,

```python
f.bar.value = 4

```

the observer isn’t called…

what am i missing?

thanks in advance

---

<div class="post-metadata">

**Author:** ![bollwyvl](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/bollwyvl/32/904_2.png) [@bollwyvl](https://discourse.jupyter.org/u/bollwyvl)\
**Post date:** [February 1, 2022, 12:49pm UTC](https://discourse.jupyter.org/t/observer-pattern-does-not-work-using-a-widget/12840/2 "2022-02-01T12:49:47Z")

</div>

That `@observe` is looking for two different traits on `Foo`, `bar` (which exists) and `value`) which does not.

If you wish to observe `bar.value`, and `bar` can change, your `bar` observer needs to delegate the `value` observing to the new `bar` instance (and clean up any old handlers)

```auto
    @observe("bar")
    def _observe_bar(self, change):
         if change.old:
             change.old.unobserve(self._on_bar_value, ["value"])
         if change.new:
             change.new.observe(self._on_bar_value, ["value"])
    
    def _on_bar_value(self, change):
        print('asdfasdf')
        print(change['old'])
        print(change['new'])

```

---

<div class="post-metadata">

**Author:** ![jgunstone](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/jgunstone/32/8661_2.png) [@jgunstone](https://discourse.jupyter.org/u/jgunstone)\
**Post date:** [February 1, 2022, 1:53pm UTC](https://discourse.jupyter.org/t/observer-pattern-does-not-work-using-a-widget/12840/3 "2022-02-01T13:53:32Z")

</div>

thanks for the response -

i wish to watch `bar.value`, and `bar` cannot change. (i.e. use the decorator to watch the nested trait)

though it looks like this isn’t supported / isn’t the intended use case?

---

<div class="post-metadata">

**Author:** ![bollwyvl](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/bollwyvl/32/904_2.png) [@bollwyvl](https://discourse.jupyter.org/u/bollwyvl)\
**Post date:** [February 1, 2022, 2:01pm UTC](https://discourse.jupyter.org/t/observer-pattern-does-not-work-using-a-widget/12840/4 "2022-02-01T14:01:05Z")

</div>

> use the decorator to watch the nested trait

The API does not support that.

[https://traitlets.readthedocs.io/en/stable/api.html#traitlets.observe](https://traitlets.readthedocs.io/en/stable/api.html#traitlets.observe)

---

<div class="post-metadata">

**Author:** ![jgunstone](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/jgunstone/32/8661_2.png) [@jgunstone](https://discourse.jupyter.org/u/jgunstone)\
**Post date:** [February 1, 2022, 2:31pm UTC](https://discourse.jupyter.org/t/observer-pattern-does-not-work-using-a-widget/12840/5 "2022-02-01T14:31:02Z")

</div>

great - thanks very much for your help!
