Skip to content

Commit 1657447

Browse files
authored
Merge pull request jupyter-widgets#3477 from jasongrout/settitle
Add back set_title and get_title to selection containers
2 parents 981f93e + e45b748 commit 1657447

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

python/ipywidgets/ipywidgets/widgets/tests/test_selectioncontainer.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,29 @@ def test_selected_index(self):
2626
def test_selected_index_out_of_bounds(self):
2727
with self.assertRaises(TraitError):
2828
Accordion(self.children, selected_index=-1)
29+
30+
31+
def test_titles(self):
32+
accordion = Accordion(self.children, selected_index=None)
33+
assert accordion.get_state()['titles'] == ('', '')
34+
assert accordion.titles == ('', '')
35+
36+
accordion.set_title(1, 'Title 1')
37+
assert accordion.get_state()['titles'] == ('', 'Title 1')
38+
assert accordion.titles[1] == 'Title 1'
39+
assert accordion.get_title(1) == 'Title 1'
40+
41+
# Backwards compatible with 7.x api
42+
accordion.set_title(1, None)
43+
assert accordion.get_state()['titles'] == ('', '')
44+
assert accordion.titles[1] == ''
45+
assert accordion.get_title(1) == ''
46+
47+
with self.assertRaises(IndexError):
48+
accordion.set_title(2, 'out of bounds')
49+
with self.assertRaises(IndexError):
50+
accordion.get_title(2)
51+
52+
accordion.children = tuple(accordion.children[:1])
53+
assert len(accordion.children) == 1
54+
assert accordion.titles == ('',)

python/ipywidgets/ipywidgets/widgets/widget_selectioncontainer.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
from .widget_core import CoreWidget
1313
from traitlets import Unicode, Dict, CInt, TraitError, validate, observe
1414
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(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

1622
class _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
3876
class Accordion(_SelectionContainer):

0 commit comments

Comments
 (0)