Reputation: 3433
I would like to observe an ipywidget text input
out4 = wd.Output()
wd_input_text4 = wd.Text(value="",
placeholder='placeholder',
description='Number:',
)
def method4(sender):
with out4:
out4.clear_output()
print(wd_input_text4.value)
wd_input_text4.observe(method4, names=['value'], type='change')
display(wd.VBox([wd_input_text4,out4]))
What I would like is to pass to the handler an extra variable, just call it A. pseudocode would be (it does not work):
def method4(sender, A):
with out4:
out4.clear_output()
print(wd_input_text4.value, A)
wd_input_text4.observe(method4, names=['value'], type='change', A)
Is that even possible? How can I do it?
Upvotes: 2
Views: 544
Reputation: 5565
Using functools.partial would avoid a class to hold the extra state.
import ipywidgets as widgets
import functools
a = widgets.IntText()
def observe_val(to_add, widg):
val = widg['new']
print(val + to_add)
a.observe(functools.partial(observe_val, 4), names=['value'])
display(a)
Upvotes: 3
Reputation: 54718
class Method4:
def __init__(self,ctx):
self.ctx = ctx
def method4(self, sender):
with out4:
out4.clear_output()
print(wd_input_text4.value, self.ctx)
wd_input_text4.observe(Method4(A).method4, names=['value'], type='change')
Upvotes: 2