1212from .widget_core import CoreWidget
1313from traitlets import Unicode , Dict , CInt , TraitError , validate , observe
1414from .trait_types import TypedTuple
15+ from itertools import chain , repeat , islice
16+
17+ # Inspired by an itertools recipe: https://docs.python.org/3/library/itertools.html#itertools-recipes
18+ def pad (iterable , padding = None , length = None ):
19+ """Returns the sequence elements and then returns None up to the given size (or indefinitely if size is None)."""
20+ return islice (chain (iterable , repeat (padding )), length )
1521
1622class _SelectionContainer (Box , CoreWidget ):
1723 """Base class used to display multiple child widgets."""
@@ -29,10 +35,42 @@ def _validated_index(self, proposal):
2935 else :
3036 raise TraitError ('Invalid selection: index out of bounds' )
3137
38+ @validate ('titles' )
39+ def _validate_titles (self , proposal ):
40+ return tuple (pad (proposal .value , '' , len (self .children )))
41+
3242 @observe ('children' )
3343 def _observe_children (self , change ):
3444 if self .selected_index is not None and len (change .new ) < self .selected_index :
3545 self .selected_index = None
46+ if len (self .titles ) != len (change .new ):
47+ # Run validation function
48+ self .titles = tuple (self .titles )
49+
50+ def set_title (self , index , title ):
51+ """Sets the title of a container page.
52+ Parameters
53+ ----------
54+ index : int
55+ Index of the container page
56+ title : unicode
57+ New title
58+ """
59+ titles = list (self .titles )
60+ # for backwards compatibility with ipywidgets 7.x
61+ if title is None :
62+ title = ''
63+ titles [index ]= title
64+ self .titles = tuple (titles )
65+
66+ def get_title (self , index ):
67+ """Gets the title of a container page.
68+ Parameters
69+ ----------
70+ index : int
71+ Index of the container page
72+ """
73+ return self .titles [index ]
3674
3775@register
3876class Accordion (_SelectionContainer ):
0 commit comments