Skip to content

Commit 78cdfcb

Browse files
authored
Merge pull request #2746 from jasongrout/titles
Make selection container titles a tuple of strings
2 parents b3708e1 + e88f8dd commit 78cdfcb

File tree

4 files changed

+15
-57
lines changed

4 files changed

+15
-57
lines changed

docs/source/examples/Widget List.ipynb

+3-7
Original file line numberDiff line numberDiff line change
@@ -1223,9 +1223,7 @@
12231223
"metadata": {},
12241224
"outputs": [],
12251225
"source": [
1226-
"accordion = widgets.Accordion(children=[widgets.IntSlider(), widgets.Text()])\n",
1227-
"accordion.set_title(0, 'Slider')\n",
1228-
"accordion.set_title(1, 'Text')\n",
1226+
"accordion = widgets.Accordion(children=[widgets.IntSlider(), widgets.Text()], titles=('Slider', 'Text'))\n",
12291227
"accordion"
12301228
]
12311229
},
@@ -1248,8 +1246,7 @@
12481246
"children = [widgets.Text(description=name) for name in tab_contents]\n",
12491247
"tab = widgets.Tab()\n",
12501248
"tab.children = children\n",
1251-
"for i in range(len(children)):\n",
1252-
" tab.set_title(i, str(i))\n",
1249+
"tab.titles = [str(i) for i in range(len(children))]\n",
12531250
"tab"
12541251
]
12551252
},
@@ -1347,8 +1344,7 @@
13471344
"source": [
13481345
"tab_nest = widgets.Tab()\n",
13491346
"tab_nest.children = [accordion, accordion]\n",
1350-
"tab_nest.set_title(0, 'An accordion')\n",
1351-
"tab_nest.set_title(1, 'Copy of the accordion')\n",
1347+
"tab_nest.titles = ('An accordion', 'Copy of the accordion')\n",
13521348
"tab_nest"
13531349
]
13541350
},

ipywidgets/widgets/widget_selectioncontainer.py

+2-40
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
from .widget import register
1212
from .widget_core import CoreWidget
1313
from traitlets import Unicode, Dict, CInt, TraitError, validate, observe
14-
14+
from .trait_types import TypedTuple
1515

1616
class _SelectionContainer(Box, CoreWidget):
1717
"""Base class used to display multiple child widgets."""
18-
_titles = Dict(help="Titles of the pages").tag(sync=True)
18+
titles = TypedTuple(trait=Unicode(), help="Titles of the pages").tag(sync=True)
1919
selected_index = CInt(
2020
help="""The index of the selected page. This is either an integer selecting a particular sub-widget, or None to have no widgets selected.""",
2121
allow_none=True,
@@ -34,44 +34,6 @@ def _observe_children(self, change):
3434
if self.selected_index is not None and len(change.new) < self.selected_index:
3535
self.selected_index = None
3636

37-
# Public methods
38-
def set_title(self, index, title):
39-
"""Sets the title of a container page.
40-
41-
Parameters
42-
----------
43-
index : int
44-
Index of the container page
45-
title : unicode
46-
New title
47-
"""
48-
# JSON dictionaries have string keys, so we convert index to a string
49-
index = str(int(index))
50-
self._titles[index] = title
51-
self.send_state('_titles')
52-
53-
def get_title(self, index):
54-
"""Gets the title of a container pages.
55-
56-
Parameters
57-
----------
58-
index : int
59-
Index of the container page
60-
"""
61-
# JSON dictionaries have string keys, so we convert index to a string
62-
index = str(int(index))
63-
if index in self._titles:
64-
return self._titles[index]
65-
else:
66-
return None
67-
68-
def _repr_keys(self):
69-
# We also need to include _titles in repr for reproducibility
70-
yield from super()._repr_keys()
71-
if self._titles:
72-
yield '_titles'
73-
74-
7537
@register
7638
class Accordion(_SelectionContainer):
7739
"""Displays children each on a separate accordion page."""

packages/controls/src/widget_selectioncontainer.ts

+7-7
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: null,
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
});

packages/schema/jupyterwidgetmodels.latest.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ Attribute | Type | Default | Help
7171
`_model_module` | string | `'@jupyter-widgets/controls'` |
7272
`_model_module_version` | string | `'1.5.0'` |
7373
`_model_name` | string | `'AccordionModel'` |
74-
`_titles` | object | `{}` | Titles of the pages
7574
`_view_module` | string | `'@jupyter-widgets/controls'` |
7675
`_view_module_version` | string | `'1.5.0'` |
7776
`_view_name` | string | `'AccordionView'` |
@@ -80,6 +79,7 @@ Attribute | Type | Default | Help
8079
`layout` | reference to Layout widget | reference to new instance |
8180
`selected_index` | `null` or number (integer) | `null` | The index of the selected page. This is either an integer selecting a particular sub-widget, or None to have no widgets selected.
8281
`tabbable` | `null` or boolean | `null` | Is widget tabbable?
82+
`titles` | array of string | `[]` | Titles of the pages
8383
`tooltip` | `null` or string | `null` | A tooltip caption.
8484

8585
### AudioModel (@jupyter-widgets/controls, 1.5.0); AudioView (@jupyter-widgets/controls, 1.5.0)
@@ -949,7 +949,6 @@ Attribute | Type | Default | Help
949949
`_model_module` | string | `'@jupyter-widgets/controls'` |
950950
`_model_module_version` | string | `'1.5.0'` |
951951
`_model_name` | string | `'StackedModel'` |
952-
`_titles` | object | `{}` | Titles of the pages
953952
`_view_module` | string | `'@jupyter-widgets/controls'` |
954953
`_view_module_version` | string | `'1.5.0'` |
955954
`_view_name` | string | `'StackedView'` |
@@ -958,6 +957,7 @@ Attribute | Type | Default | Help
958957
`layout` | reference to Layout widget | reference to new instance |
959958
`selected_index` | `null` or number (integer) | `null` | The index of the selected page. This is either an integer selecting a particular sub-widget, or None to have no widgets selected.
960959
`tabbable` | `null` or boolean | `null` | Is widget tabbable?
960+
`titles` | array of string | `[]` | Titles of the pages
961961
`tooltip` | `null` or string | `null` | A tooltip caption.
962962

963963
### TabModel (@jupyter-widgets/controls, 1.5.0); TabView (@jupyter-widgets/controls, 1.5.0)
@@ -968,7 +968,6 @@ Attribute | Type | Default | Help
968968
`_model_module` | string | `'@jupyter-widgets/controls'` |
969969
`_model_module_version` | string | `'1.5.0'` |
970970
`_model_name` | string | `'TabModel'` |
971-
`_titles` | object | `{}` | Titles of the pages
972971
`_view_module` | string | `'@jupyter-widgets/controls'` |
973972
`_view_module_version` | string | `'1.5.0'` |
974973
`_view_name` | string | `'TabView'` |
@@ -977,6 +976,7 @@ Attribute | Type | Default | Help
977976
`layout` | reference to Layout widget | reference to new instance |
978977
`selected_index` | `null` or number (integer) | `null` | The index of the selected page. This is either an integer selecting a particular sub-widget, or None to have no widgets selected.
979978
`tabbable` | `null` or boolean | `null` | Is widget tabbable?
979+
`titles` | array of string | `[]` | Titles of the pages
980980
`tooltip` | `null` or string | `null` | A tooltip caption.
981981

982982
### TextModel (@jupyter-widgets/controls, 1.5.0); TextView (@jupyter-widgets/controls, 1.5.0)

0 commit comments

Comments
 (0)