ipywidgets unobserve functools partial 1

ipywidgets unobserve functools partial

import ipywidgets as widgets
w = widgets.Text()
w.unobserve(None,...)
            
# still need to pass `names` whenever needed. 
# But at least you don't need to know the exact name of the handler. 
# Specially if you have used a `partial` when setting the observer.

Here is what the above code is Doing:
1. `w.observe` returns a list of `observe_handlers`
2. `observe_handlers` is a list of `observe_handler`
3. `observe_handler` is a namedtuple with the following fields:
– `attr`: the name of the attribute being observed
– `old`: the old value of the attribute
– `new`: the new value of the attribute
– `owner`: the object being observed
– `name`: the name of the handler
– `type`: the type of the handler
– `func`: the function being called
– `args`: the arguments passed to the function
– `kwargs`: the keyword arguments passed to the function
4. `observe_handler.name` is the name of the handler
5. `w.unobserve(None, names=observe_handler.name)` removes the handler

Similar Posts