12
12
from .widget_core import CoreWidget
13
13
from traitlets import Unicode , Dict , CInt , TraitError , validate , observe
14
14
from .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_none (iterable , 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 (None )), length )
15
21
16
22
class _SelectionContainer (Box , CoreWidget ):
17
23
"""Base class used to display multiple child widgets."""
@@ -29,10 +35,39 @@ def _validated_index(self, proposal):
29
35
else :
30
36
raise TraitError ('Invalid selection: index out of bounds' )
31
37
38
+ @validate ('titles' )
39
+ def _validate_titles (self , proposal ):
40
+ return tuple (pad_none (proposal .value , len (self .children )))
41
+
32
42
@observe ('children' )
33
43
def _observe_children (self , change ):
34
44
if self .selected_index is not None and len (change .new ) < self .selected_index :
35
45
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
+ titles [index ]= title
61
+ self .titles = tuple (titles )
62
+
63
+ def get_title (self , index ):
64
+ """Gets the title of a container pages.
65
+ Parameters
66
+ ----------
67
+ index : int
68
+ Index of the container page
69
+ """
70
+ return self .titles [index ]
36
71
37
72
@register
38
73
class Accordion (_SelectionContainer ):
0 commit comments