Skip to content

Commit

Permalink
Update grouping flattening methods (#46)
Browse files Browse the repository at this point in the history
Ensure tuples of (str, value) are *always* created for the "flat" list
of groupings used for the `options` attribute and trait.

Remove superfluous static method.
  • Loading branch information
CasperWA authored Oct 12, 2021
1 parent 8f78fe4 commit 767d940
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
4 changes: 2 additions & 2 deletions examples/examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.6"
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
}
32 changes: 11 additions & 21 deletions ipywidgets_extended/dropdown.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Dropdown Widget Extension"""
from typing import Any, List, Tuple, Union
from typing import Any, List, Tuple

from ipywidgets.widgets.widget_selection import Dropdown, _make_options
from traitlets import traitlets
Expand Down Expand Up @@ -117,7 +117,7 @@ def __init__(self, *args, **kwargs):
kwargs["index"] = kwargs["label"] = kwargs["value"] = None

if "grouping" in kwargs:
kwargs["options"] = self._create_grouping_options(grouping)
kwargs["options"] = self._flat_groupings(grouping)
super().__init__(*args, **kwargs)
self._initializing_traits_ = True

Expand Down Expand Up @@ -192,7 +192,7 @@ def _validate_grouping(self, proposal) -> Tuple[Tuple[str, List[str]]]:
def _set_grouping(self, change) -> None:
"""Put options into desired grouping, updating `options`"""
grouping = self._grouping_full
self.options = self._create_grouping_options(grouping)
self.options = self._flat_groupings(grouping)
self.set_trait(
"_grouping_labels",
tuple(
Expand Down Expand Up @@ -222,15 +222,15 @@ def _group_headers(self) -> List[str]:
return [_[0] for _ in self._grouping_labels]

def _flat_groupings(
self, grouping: Tuple[Tuple[str, Tuple[Any]]] = None
) -> List[Union[str, Any]]:
"""Get grouping similar to dropdown - a flat list of entries"""
self, grouping: Tuple[Tuple[str, Tuple[Tuple[str, Any]]]] = None
) -> List[Tuple[str, Any]]:
"""Get grouping similar to dropdown - a flat list of entries."""
grouping = grouping if grouping is not None else self._grouping_labels

res = []
for header, options in grouping:
if header:
res.append(header)
res.append((header, None))
res.extend(options)
return res

Expand All @@ -246,18 +246,8 @@ def _get_grouping_label_value(
grouping = grouping if grouping is not None else self._grouping_full

res = self._flat_groupings(grouping)[index]
if len(res) == 1:
return res, None
return res

@staticmethod
def _create_grouping_options(
grouping: Tuple[Tuple[str, Tuple[Tuple[str, Any]]]]
) -> Tuple[Tuple[str, Any]]:
"""Create and return a standard list of options from a grouping."""
res = []
for header, options in grouping:
if header:
res.append((header, None))
res.extend(options)
if not isinstance(res, tuple) or len(res) != 2:
raise ValueError(
f"Found a grouped value that is not a tuple with length 2: {res!r}"
)
return res

0 comments on commit 767d940

Please sign in to comment.