如何使用 ipywidgets 创建一个切换按钮来显示和清除输出?
How to create a toogle button to show and clear output using ipywidgets?
我想创建一个切换按钮来显示一些输出并在使用 ipywidget
在 jupyter notebook 中切换时清除它。
我尝试使用 widgets.Output()
来做到这一点。但是,当我点击的次数越来越多时,这会显示空行。
请帮帮我。
import ipywidgets as w
toggle = w.ToggleButton(description='click me')
out = w.Output()
def fun(obj):
global out
with out:
display('asd')
if obj['new']:
display(out)
else:
out.clear_output()
out = w.Output()
toggle.observe(fun, 'value')
display(toggle)
我认为 global
关键字与 display(out)
的组合导致了这里的问题。我在下面做了一些简化:
在代码主体中显示 output
小部件,并使用 clear_output
命令(输出小部件仍为 'visible',但高度为零)。
import ipywidgets as w
toggle = w.ToggleButton(description='click me')
out = w.Output(layout=w.Layout(border = '1px solid black'))
def fun(obj):
with out:
if obj['new']:
display('asd')
else:
out.clear_output()
toggle.observe(fun, 'value')
display(toggle)
display(out)
我想创建一个切换按钮来显示一些输出并在使用 ipywidget
在 jupyter notebook 中切换时清除它。
我尝试使用 widgets.Output()
来做到这一点。但是,当我点击的次数越来越多时,这会显示空行。
请帮帮我。
import ipywidgets as w
toggle = w.ToggleButton(description='click me')
out = w.Output()
def fun(obj):
global out
with out:
display('asd')
if obj['new']:
display(out)
else:
out.clear_output()
out = w.Output()
toggle.observe(fun, 'value')
display(toggle)
我认为 global
关键字与 display(out)
的组合导致了这里的问题。我在下面做了一些简化:
在代码主体中显示 output
小部件,并使用 clear_output
命令(输出小部件仍为 'visible',但高度为零)。
import ipywidgets as w
toggle = w.ToggleButton(description='click me')
out = w.Output(layout=w.Layout(border = '1px solid black'))
def fun(obj):
with out:
if obj['new']:
display('asd')
else:
out.clear_output()
toggle.observe(fun, 'value')
display(toggle)
display(out)