笔记本中的 Jupyter 小部件:重新定义 Interactive_output

时间:2021-07-20 02:25:42

标签: python jupyter-notebook jupyter-widget

我通常在 jupyter-notebook 中使用 interactive_output。 我遇到的一个问题是,在尝试重新定义由 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 / 对增量的调用。

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

0 个答案:

没有答案