Skip to content

Commit

Permalink
Merge pull request #3477 from jasongrout/settitle
Browse files Browse the repository at this point in the history
Add back set_title and get_title to selection containers
  • Loading branch information
ibdafna authored Jun 13, 2022
2 parents 981f93e + e45b748 commit 1657447
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,29 @@ def test_selected_index(self):
def test_selected_index_out_of_bounds(self):
with self.assertRaises(TraitError):
Accordion(self.children, selected_index=-1)


def test_titles(self):
accordion = Accordion(self.children, selected_index=None)
assert accordion.get_state()['titles'] == ('', '')
assert accordion.titles == ('', '')

accordion.set_title(1, 'Title 1')
assert accordion.get_state()['titles'] == ('', 'Title 1')
assert accordion.titles[1] == 'Title 1'
assert accordion.get_title(1) == 'Title 1'

# Backwards compatible with 7.x api
accordion.set_title(1, None)
assert accordion.get_state()['titles'] == ('', '')
assert accordion.titles[1] == ''
assert accordion.get_title(1) == ''

with self.assertRaises(IndexError):
accordion.set_title(2, 'out of bounds')
with self.assertRaises(IndexError):
accordion.get_title(2)

accordion.children = tuple(accordion.children[:1])
assert len(accordion.children) == 1
assert accordion.titles == ('',)
38 changes: 38 additions & 0 deletions python/ipywidgets/ipywidgets/widgets/widget_selectioncontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
from .widget_core import CoreWidget
from traitlets import Unicode, Dict, CInt, TraitError, validate, observe
from .trait_types import TypedTuple
from itertools import chain, repeat, islice

# Inspired by an itertools recipe: https://docs.python.org/3/library/itertools.html#itertools-recipes
def pad(iterable, padding=None, length=None):
"""Returns the sequence elements and then returns None up to the given size (or indefinitely if size is None)."""
return islice(chain(iterable, repeat(padding)), length)

class _SelectionContainer(Box, CoreWidget):
"""Base class used to display multiple child widgets."""
Expand All @@ -29,10 +35,42 @@ def _validated_index(self, proposal):
else:
raise TraitError('Invalid selection: index out of bounds')

@validate('titles')
def _validate_titles(self, proposal):
return tuple(pad(proposal.value, '', len(self.children)))

@observe('children')
def _observe_children(self, change):
if self.selected_index is not None and len(change.new) < self.selected_index:
self.selected_index = None
if len(self.titles) != len(change.new):
# Run validation function
self.titles = tuple(self.titles)

def set_title(self, index, title):
"""Sets the title of a container page.
Parameters
----------
index : int
Index of the container page
title : unicode
New title
"""
titles = list(self.titles)
# for backwards compatibility with ipywidgets 7.x
if title is None:
title = ''
titles[index]=title
self.titles = tuple(titles)

def get_title(self, index):
"""Gets the title of a container page.
Parameters
----------
index : int
Index of the container page
"""
return self.titles[index]

@register
class Accordion(_SelectionContainer):
Expand Down

0 comments on commit 1657447

Please sign in to comment.