【发布时间】:2019-10-04 12:57:48
【问题描述】:
我已经构建了一个 jupyter notebook 来分析和对比历史数据。因为数据库非常大,我使用了搜索+观察小部件来允许用户选择他们想要的实验,而不是滚动浏览所有数据。现在所有实验的所有复选框都已生成,但这既太大而无法放入盒子中,并且占用了大量时间。
有没有办法默认只显示最后 10 个实验,并且只在搜索后生成所需的复选框?
我基于此gist 编写代码。我已经尝试将复选框小部件移动到 else 循环中,但这不起作用。此外,我的代码中的全局/局部变量情况似乎很混乱。
import ipywidgets as widgets
#Generate a dummy list
Allfileslist = ["Experiment {}".format(x) for x in range(600)]
#Search box + generate ALL the checkboxes, this seems to be the bottleneck
search_widget = widgets.Text(placeholder='Type for older experiments', description= 'Search:')
experiments = {eachfilename: widgets.Checkbox(description=eachfilename, value=False) for eachfilename in Allfileslist}
options = [experiments[eachfilename] for eachfilename in Allfileslist]
options_widget = widgets.VBox(options, layout={'overflow': 'auto'})
def whentextischanged (change):
"""Dynamically update the widget experiments"""
search_input = change['new']
if search_input == '':
# Reset search field, default to last 9 experiments
new_options = [experiments[eachfilename] for eachfilename in Allfileslist[-10:]]
else:
# Filter by search
close_matches = [x for x in Allfileslist if search_input.upper() in x]
### magic code that generates only the match boxes ###
new_options = [experiments[eachfilename] for eachfilename in close_matches]
options_widget.children = new_options
#Generate the vbox, search
multi_select = widgets.VBox([search_widget, options_widget])
search_widget.observe(whentextischanged, names='value')
#serve it up
multi_select
有没有办法只生成加快速度所需的复选框?
更新 我将 ac24 的答案与一些修改结合起来,它几乎可以满足它的需要。唯一没有的是在搜索框一开始为空时显示默认值。之后,清除搜索框会显示正确的行为。
import ipywidgets as widgets
#Generate a dummy list
Allfileslist = ["{}".format(x) for x in range(600)]
#Search box + generate some checboxes
search_widget = widgets.Text(placeholder='Type for older experiments', description= 'Search:',value='')
experiments = {}
options_widget = widgets.VBox(layout={'overflow': 'auto'})
default_options = [widgets.Checkbox(description=eachfilename, value=False) for eachfilename in Allfileslist[-10:]]
def whentextischanged (change):
"""Dynamically update the widget experiments"""
search_input = change['new']
if search_input == '':
# Reset search field, default to last 9 experiments
new_options = default_options
else:
# Filter by search
close_matches = [x for x in Allfileslist if search_input.lower() in x.lower()][:10]
for name in close_matches:
if name not in experiments:
experiments[name] = widgets.Checkbox(description=name, value=False)
new_options = [experiments[eachfilename] for eachfilename in close_matches]
options_widget.children = new_options
#Generate the vbox, search
multi_select = widgets.VBox([search_widget, options_widget])
search_widget.observe(whentextischanged, names='value')
multi_select
【问题讨论】:
标签: jupyter-notebook ipywidgets