-
Notifications
You must be signed in to change notification settings - Fork 5
/
multi_checkbox_widget.py
56 lines (50 loc) · 2.23 KB
/
multi_checkbox_widget.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# From https://gist.github.com/MattJBritton/9dc26109acb4dfe17820cf72d82f1e6f
import ipywidgets as wid
from IPython.display import display
import traitlets
def multi_checkbox_widget(options_dict, parent):
""" Widget with a search field and lots of checkboxes """
search_widget = wid.Text(width="auto", layout=wid.Layout(flex="1 1 auto", width="auto", overflow='hidden'))
output_widget = wid.Output()
options = [x for x in options_dict.values()]
options_layout = wid.Layout(
overflow='auto',
border='1px solid black',
width='170px',
height='440px',
flex_flow='column',
display='flex'
)
options_widget = wid.VBox(options, layout=options_layout)
multi_select = wid.VBox([search_widget, options_widget])
# Wire the search field to the checkboxes
@output_widget.capture()
def on_text_change(change):
search_input = change['new']
if search_input == '':
# Reset search field
new_options = sorted(options, key=lambda x: x.value, reverse=True)
else:
# Filter by search field using difflib.
# close_matches = difflib.get_close_matches(search_input, list(options_dict.keys()), cutoff=0.0)
close_matches = [x for x in list(options_dict.keys()) if str.lower(search_input.strip('')) in str.lower(x)]
new_options = sorted(
[x for x in options if x.description in close_matches],
key=lambda x: x.value, reverse=True
) # [options_dict[x] for x in close_matches]
options_widget.children = new_options
@output_widget.capture()
def on_selection_change(change):
country = change["owner"].description
val = change["new"]
if val:
parent.countries_to_plot = [x for x in parent.countries_to_plot] + [country]
else:
parent.countries_to_plot = [x for x in parent.countries_to_plot if x != country]
options_widget.children = sorted(options, key=lambda x: x.value, reverse=True)
on_text_change({"new": ""})
search_widget.observe(on_text_change, names='value')
for o in options:
o.observe(on_selection_change, names="value")
display(output_widget)
return multi_select