Skip to content

Commit a5e6641

Browse files
committed
Make selection container titles more transparent and easier to work with.
1 parent e643166 commit a5e6641

File tree

2 files changed

+15
-24
lines changed

2 files changed

+15
-24
lines changed

ipywidgets/widgets/widget_selectioncontainer.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
from .widget import register
1212
from .widget_core import CoreWidget
1313
from traitlets import Unicode, Dict, CInt, TraitError, validate
14+
from .trait_types import TypedTuple
1415

1516

1617
class _SelectionContainer(Box, CoreWidget):
1718
"""Base class used to display multiple child widgets."""
18-
_titles = Dict(help="Titles of the pages").tag(sync=True)
19+
titles = TypedTuple(trait=Unicode(), help="Titles of the pages").tag(sync=True)
1920
selected_index = CInt(
2021
help="""The index of the selected page. This is either an integer selecting a particular sub-widget, or None to have no widgets selected.""",
2122
allow_none=True
@@ -39,33 +40,23 @@ def set_title(self, index, title):
3940
title : unicode
4041
New title
4142
"""
42-
# JSON dictionaries have string keys, so we convert index to a string
43-
index = str(int(index))
44-
self._titles[index] = title
45-
self.send_state('_titles')
43+
titles = list(self.titles)
44+
titles[index] = title
45+
self.titles = titles
4646

4747
def get_title(self, index):
48-
"""Gets the title of a container pages.
48+
"""Gets the title of a container page.
4949
5050
Parameters
5151
----------
5252
index : int
5353
Index of the container page
5454
"""
55-
# JSON dictionaries have string keys, so we convert index to a string
56-
index = str(int(index))
57-
if index in self._titles:
58-
return self._titles[index]
55+
if 0<=index<len(self.titles):
56+
return self.titles[index]
5957
else:
6058
return None
6159

62-
def _repr_keys(self):
63-
# We also need to include _titles in repr for reproducibility
64-
yield from super()._repr_keys()
65-
if self._titles:
66-
yield '_titles'
67-
68-
6960
@register
7061
class Accordion(_SelectionContainer):
7162
"""Displays children each on a separate accordion page."""

packages/controls/src/widget_selectioncontainer.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class SelectionContainerModel extends BoxModel {
3232
...super.defaults(),
3333
_model_name: 'SelectionContainerModel',
3434
selected_index: 0,
35-
_titles: {}
35+
titles: []
3636
};
3737
}
3838
}
@@ -115,7 +115,7 @@ export class AccordionView extends DOMWidgetView {
115115
this.listenTo(this.model, 'change:selected_index', () =>
116116
this.update_selected_index()
117117
);
118-
this.listenTo(this.model, 'change:_titles', () => this.update_titles());
118+
this.listenTo(this.model, 'change:titles', () => this.update_titles());
119119
}
120120

121121
/**
@@ -158,7 +158,7 @@ export class AccordionView extends DOMWidgetView {
158158
*/
159159
update_titles(): void {
160160
const collapsed = this.pWidget.collapseWidgets;
161-
const titles = this.model.get('_titles');
161+
const titles = this.model.get('titles');
162162
for (let i = 0; i < collapsed.length; i++) {
163163
if (titles[i] !== void 0) {
164164
collapsed[i].widget.title.label = titles[i];
@@ -188,7 +188,7 @@ export class AccordionView extends DOMWidgetView {
188188
// Placeholder widget to keep our position in the tab panel while we create the view.
189189
const accordion = this.pWidget;
190190
const placeholder = new Widget();
191-
placeholder.title.label = this.model.get('_titles')[index] || '';
191+
placeholder.title.label = this.model.get('titles')[index] || '';
192192
accordion.addWidget(placeholder);
193193
return this.create_child_view(model)
194194
.then((view: DOMWidgetView) => {
@@ -293,7 +293,7 @@ export class TabView extends DOMWidgetView {
293293
this
294294
);
295295
this.listenTo(this.model, 'change:children', () => this.updateTabs());
296-
this.listenTo(this.model, 'change:_titles', () => this.updateTitles());
296+
this.listenTo(this.model, 'change:titles', () => this.updateTitles());
297297
}
298298

299299
/**
@@ -339,7 +339,7 @@ export class TabView extends DOMWidgetView {
339339
*/
340340
addChildView(model: WidgetModel, index: number): Promise<DOMWidgetView> {
341341
// Placeholder widget to keep our position in the tab panel while we create the view.
342-
const label = this.model.get('_titles')[index] || '';
342+
const label = this.model.get('titles')[index] || '';
343343
const tabs = this.pWidget;
344344
const placeholder = new Widget();
345345
placeholder.title.label = label;
@@ -379,7 +379,7 @@ export class TabView extends DOMWidgetView {
379379
* Updates the tab page titles.
380380
*/
381381
updateTitles(): void {
382-
const titles = this.model.get('_titles') || {};
382+
const titles = this.model.get('titles') || [];
383383
each(this.pWidget.widgets, (widget, i) => {
384384
widget.title.label = titles[i] || '';
385385
});

0 commit comments

Comments
 (0)