如何清除两个小部件及其被其他小部件清除
How to clear both widgets and their out by other widget
我加载了 checkbox
的文件列表,使用 ipywidgets
:
进行过滤
from ipywidgets import Checkbox, interact, Layout, Button
import ipywidgets as widgets
import glob
from traitlets import traitlets
from IPython.display import display, clear_output
class LoadedButton(widgets.Button):
def __init__(self, value=None, *args, **kwargs):
super(LoadedButton, self).__init__(*args, **kwargs)
# Create the value attribute.
self.add_traits(value=traitlets.Any(value))
def file_list(**all_kwargs):
cols = [k for k, v in all_kwargs.items() if v==True]
return cols
def reload_files(rr):
for c in all_files:
c.close()
clear_output()
print('Unselect the csv files above *to EXCLUDE*')
rr.value =interact(file_list, **all_kwargs)
return rr
extension = 'csv' # extention of file you want to read, csv, dat, etc.
all_file_list = [i for i in glob.glob('*.{}'.format(extension))]
all_files = [Checkbox(description=a, value=True) for a in all_file_list ]
all_kwargs = {c.description: c.value for c in all_files}
lb = LoadedButton(description="Load/reload file list", value=None)
lb.on_click(reload_files)
display(lb)
我希望每次单击使用 Button 小部件创建的按钮时覆盖以前的输出(小部件和小部件输出),但不是覆盖它而是创建另一个输出实例。我尝试了 clear_output
和 widget.close()
选项,但没有任何帮助。我知道 clear_output
不会清除小部件,但我希望使用 close()
可以做到这一点。有谁知道通过按钮重新加载时如何 clear/overwrite ipywidgets 以及 ipywidget 输出?
如果我正确理解了这个问题,我认为这可能对你有用;
from ipywidgets import Checkbox, interact, Layout, Button
import ipywidgets as widgets
import glob
from traitlets import traitlets
from IPython.display import display, clear_output
class LoadedButton(widgets.Button):
def __init__(self, value=None, *args, **kwargs):
super(LoadedButton, self).__init__(*args, **kwargs)
# Create the value attribute.
self.add_traits(value=traitlets.Any(value))
def file_list(**all_kwargs):
cols = [k for k, v in all_kwargs.items() if v==True]
return cols
def reload_files(rr):
if rr.value is not None:
# print("Reloading...")
# Reset the panel to just the button.
panel.children = [panel.children[0]]
rr.value = None
if rr.value is None:
extension = 'csv' # extention of file you want to read, csv, dat, etc.
all_file_list = [i for i in glob.glob('*.{}'.format(extension))]
all_files = [Checkbox(description=a, value=True) for a in all_file_list ]
all_kwargs = {c.description: c.value for c in all_files}
rr.value=widgets.interactive(file_list, **all_kwargs)
panel.children += (rr.value,)
# display(rr.value)
lb = LoadedButton(description="Load/reload file list", value=None)
lb.on_click(reload_files)
# A VBox for the Button and checkboxes.
panel = widgets.VBox([lb])
panel
我认为问题出在依赖 display
函数来表示小部件。当您让一个小部件修改了另一个小部件的可见性时,您可能做的最好的事情就是在 VBox
或 HBox
等布局小部件中添加和删除它们。不管怎样,让我知道结果如何。
更新:
glob
和所有其他文件名操作也需要在单击按钮时更新。所以它现在这样做了。
这里还有如何访问 interactive
小部件上的复选框';
panel.children[1].children
或
lb.value.children
两个语句引用同一个对象。
我加载了 checkbox
的文件列表,使用 ipywidgets
:
from ipywidgets import Checkbox, interact, Layout, Button
import ipywidgets as widgets
import glob
from traitlets import traitlets
from IPython.display import display, clear_output
class LoadedButton(widgets.Button):
def __init__(self, value=None, *args, **kwargs):
super(LoadedButton, self).__init__(*args, **kwargs)
# Create the value attribute.
self.add_traits(value=traitlets.Any(value))
def file_list(**all_kwargs):
cols = [k for k, v in all_kwargs.items() if v==True]
return cols
def reload_files(rr):
for c in all_files:
c.close()
clear_output()
print('Unselect the csv files above *to EXCLUDE*')
rr.value =interact(file_list, **all_kwargs)
return rr
extension = 'csv' # extention of file you want to read, csv, dat, etc.
all_file_list = [i for i in glob.glob('*.{}'.format(extension))]
all_files = [Checkbox(description=a, value=True) for a in all_file_list ]
all_kwargs = {c.description: c.value for c in all_files}
lb = LoadedButton(description="Load/reload file list", value=None)
lb.on_click(reload_files)
display(lb)
我希望每次单击使用 Button 小部件创建的按钮时覆盖以前的输出(小部件和小部件输出),但不是覆盖它而是创建另一个输出实例。我尝试了 clear_output
和 widget.close()
选项,但没有任何帮助。我知道 clear_output
不会清除小部件,但我希望使用 close()
可以做到这一点。有谁知道通过按钮重新加载时如何 clear/overwrite ipywidgets 以及 ipywidget 输出?
如果我正确理解了这个问题,我认为这可能对你有用;
from ipywidgets import Checkbox, interact, Layout, Button
import ipywidgets as widgets
import glob
from traitlets import traitlets
from IPython.display import display, clear_output
class LoadedButton(widgets.Button):
def __init__(self, value=None, *args, **kwargs):
super(LoadedButton, self).__init__(*args, **kwargs)
# Create the value attribute.
self.add_traits(value=traitlets.Any(value))
def file_list(**all_kwargs):
cols = [k for k, v in all_kwargs.items() if v==True]
return cols
def reload_files(rr):
if rr.value is not None:
# print("Reloading...")
# Reset the panel to just the button.
panel.children = [panel.children[0]]
rr.value = None
if rr.value is None:
extension = 'csv' # extention of file you want to read, csv, dat, etc.
all_file_list = [i for i in glob.glob('*.{}'.format(extension))]
all_files = [Checkbox(description=a, value=True) for a in all_file_list ]
all_kwargs = {c.description: c.value for c in all_files}
rr.value=widgets.interactive(file_list, **all_kwargs)
panel.children += (rr.value,)
# display(rr.value)
lb = LoadedButton(description="Load/reload file list", value=None)
lb.on_click(reload_files)
# A VBox for the Button and checkboxes.
panel = widgets.VBox([lb])
panel
我认为问题出在依赖 display
函数来表示小部件。当您让一个小部件修改了另一个小部件的可见性时,您可能做的最好的事情就是在 VBox
或 HBox
等布局小部件中添加和删除它们。不管怎样,让我知道结果如何。
更新:
glob
和所有其他文件名操作也需要在单击按钮时更新。所以它现在这样做了。
这里还有如何访问 interactive
小部件上的复选框';
panel.children[1].children
或
lb.value.children
两个语句引用同一个对象。