0

我通常interactive_output在 jupyter-notebook 中使用。我遇到的一个问题是,在尝试重新定义由 调用的函数时interactive_output,我无法删除以前的注册,因此我通常会多次回调该函数。

我的设置相当于这个:

#cell 1
from ipywidgets import interactive_output, Text
topLevelCounter = 0
textW = Text(value="some text", continuous_update=False)
textW

#cell 2
def increment(text):
    global topLevelCounter
    topLevelCounter += 1
    print('call', topLevelCounter, 'with text', text)

#cell 3
ioW = interactive_output(increment, {'text': textW})

#cell 4
ioW

我尝试重新定义我的增量函数。只是重新定义函数增量(即编辑后再次运行单元格 2)不会这样做,我猜 ioW 持有对该函数的引用,很好。

重新定义 ioW 不会这样做,即使重新显示也不会这样做:我现在确实看到在输入文本小部件时调用了新版本的增量,但我的计数器每次都会跳 2。如果我第三次运行单元格 2、3、4,则 textW 中的每个输入都会将计数器增加 3 次。

我不想重新定义 textW 或使用unobserve_all(): 它可以工作,但对我来说不是解决方案,因为我试图不必重置小部件上的其他依赖项。我试图专门使用unobserve,但我无法让它真正删除我的 interactive_output / 调用来增加。

我错过了什么?如何重新定义由交互式输出调用的函数以实际替换前一个函数?

4

0 回答 0