Skip to content

Commit

Permalink
Merge pull request #2746 from jasongrout/titles
Browse files Browse the repository at this point in the history
Make selection container titles a tuple of strings
  • Loading branch information
vidartf authored Jan 31, 2020
2 parents b3708e1 + e88f8dd commit 78cdfcb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 57 deletions.
10 changes: 3 additions & 7 deletions docs/source/examples/Widget List.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1223,9 +1223,7 @@
"metadata": {},
"outputs": [],
"source": [
"accordion = widgets.Accordion(children=[widgets.IntSlider(), widgets.Text()])\n",
"accordion.set_title(0, 'Slider')\n",
"accordion.set_title(1, 'Text')\n",
"accordion = widgets.Accordion(children=[widgets.IntSlider(), widgets.Text()], titles=('Slider', 'Text'))\n",
"accordion"
]
},
Expand All @@ -1248,8 +1246,7 @@
"children = [widgets.Text(description=name) for name in tab_contents]\n",
"tab = widgets.Tab()\n",
"tab.children = children\n",
"for i in range(len(children)):\n",
" tab.set_title(i, str(i))\n",
"tab.titles = [str(i) for i in range(len(children))]\n",
"tab"
]
},
Expand Down Expand Up @@ -1347,8 +1344,7 @@
"source": [
"tab_nest = widgets.Tab()\n",
"tab_nest.children = [accordion, accordion]\n",
"tab_nest.set_title(0, 'An accordion')\n",
"tab_nest.set_title(1, 'Copy of the accordion')\n",
"tab_nest.titles = ('An accordion', 'Copy of the accordion')\n",
"tab_nest"
]
},
Expand Down
42 changes: 2 additions & 40 deletions ipywidgets/widgets/widget_selectioncontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
from .widget import register
from .widget_core import CoreWidget
from traitlets import Unicode, Dict, CInt, TraitError, validate, observe

from .trait_types import TypedTuple

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

# Public methods
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
"""
# JSON dictionaries have string keys, so we convert index to a string
index = str(int(index))
self._titles[index] = title
self.send_state('_titles')

def get_title(self, index):
"""Gets the title of a container pages.
Parameters
----------
index : int
Index of the container page
"""
# JSON dictionaries have string keys, so we convert index to a string
index = str(int(index))
if index in self._titles:
return self._titles[index]
else:
return None

def _repr_keys(self):
# We also need to include _titles in repr for reproducibility
yield from super()._repr_keys()
if self._titles:
yield '_titles'


@register
class Accordion(_SelectionContainer):
"""Displays children each on a separate accordion page."""
Expand Down
14 changes: 7 additions & 7 deletions packages/controls/src/widget_selectioncontainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class SelectionContainerModel extends BoxModel {
...super.defaults(),
_model_name: 'SelectionContainerModel',
selected_index: null,
_titles: {}
_titles: []
};
}
}
Expand Down Expand Up @@ -115,7 +115,7 @@ export class AccordionView extends DOMWidgetView {
this.listenTo(this.model, 'change:selected_index', () =>
this.update_selected_index()
);
this.listenTo(this.model, 'change:_titles', () => this.update_titles());
this.listenTo(this.model, 'change:titles', () => this.update_titles());
}

/**
Expand Down Expand Up @@ -158,7 +158,7 @@ export class AccordionView extends DOMWidgetView {
*/
update_titles(): void {
const collapsed = this.pWidget.collapseWidgets;
const titles = this.model.get('_titles');
const titles = this.model.get('titles');
for (let i = 0; i < collapsed.length; i++) {
if (titles[i] !== void 0) {
collapsed[i].widget.title.label = titles[i];
Expand Down Expand Up @@ -188,7 +188,7 @@ export class AccordionView extends DOMWidgetView {
// Placeholder widget to keep our position in the tab panel while we create the view.
const accordion = this.pWidget;
const placeholder = new Widget();
placeholder.title.label = this.model.get('_titles')[index] || '';
placeholder.title.label = this.model.get('titles')[index] || '';
accordion.addWidget(placeholder);
return this.create_child_view(model)
.then((view: DOMWidgetView) => {
Expand Down Expand Up @@ -293,7 +293,7 @@ export class TabView extends DOMWidgetView {
this
);
this.listenTo(this.model, 'change:children', () => this.updateTabs());
this.listenTo(this.model, 'change:_titles', () => this.updateTitles());
this.listenTo(this.model, 'change:titles', () => this.updateTitles());
}

/**
Expand Down Expand Up @@ -339,7 +339,7 @@ export class TabView extends DOMWidgetView {
*/
addChildView(model: WidgetModel, index: number): Promise<DOMWidgetView> {
// Placeholder widget to keep our position in the tab panel while we create the view.
const label = this.model.get('_titles')[index] || '';
const label = this.model.get('titles')[index] || '';
const tabs = this.pWidget;
const placeholder = new Widget();
placeholder.title.label = label;
Expand Down Expand Up @@ -379,7 +379,7 @@ export class TabView extends DOMWidgetView {
* Updates the tab page titles.
*/
updateTitles(): void {
const titles = this.model.get('_titles') || {};
const titles = this.model.get('titles') || [];
each(this.pWidget.widgets, (widget, i) => {
widget.title.label = titles[i] || '';
});
Expand Down
6 changes: 3 additions & 3 deletions packages/schema/jupyterwidgetmodels.latest.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ Attribute | Type | Default | Help
`_model_module` | string | `'@jupyter-widgets/controls'` |
`_model_module_version` | string | `'1.5.0'` |
`_model_name` | string | `'AccordionModel'` |
`_titles` | object | `{}` | Titles of the pages
`_view_module` | string | `'@jupyter-widgets/controls'` |
`_view_module_version` | string | `'1.5.0'` |
`_view_name` | string | `'AccordionView'` |
Expand All @@ -80,6 +79,7 @@ Attribute | Type | Default | Help
`layout` | reference to Layout widget | reference to new instance |
`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.
`tabbable` | `null` or boolean | `null` | Is widget tabbable?
`titles` | array of string | `[]` | Titles of the pages
`tooltip` | `null` or string | `null` | A tooltip caption.

### AudioModel (@jupyter-widgets/controls, 1.5.0); AudioView (@jupyter-widgets/controls, 1.5.0)
Expand Down Expand Up @@ -949,7 +949,6 @@ Attribute | Type | Default | Help
`_model_module` | string | `'@jupyter-widgets/controls'` |
`_model_module_version` | string | `'1.5.0'` |
`_model_name` | string | `'StackedModel'` |
`_titles` | object | `{}` | Titles of the pages
`_view_module` | string | `'@jupyter-widgets/controls'` |
`_view_module_version` | string | `'1.5.0'` |
`_view_name` | string | `'StackedView'` |
Expand All @@ -958,6 +957,7 @@ Attribute | Type | Default | Help
`layout` | reference to Layout widget | reference to new instance |
`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.
`tabbable` | `null` or boolean | `null` | Is widget tabbable?
`titles` | array of string | `[]` | Titles of the pages
`tooltip` | `null` or string | `null` | A tooltip caption.

### TabModel (@jupyter-widgets/controls, 1.5.0); TabView (@jupyter-widgets/controls, 1.5.0)
Expand All @@ -968,7 +968,6 @@ Attribute | Type | Default | Help
`_model_module` | string | `'@jupyter-widgets/controls'` |
`_model_module_version` | string | `'1.5.0'` |
`_model_name` | string | `'TabModel'` |
`_titles` | object | `{}` | Titles of the pages
`_view_module` | string | `'@jupyter-widgets/controls'` |
`_view_module_version` | string | `'1.5.0'` |
`_view_name` | string | `'TabView'` |
Expand All @@ -977,6 +976,7 @@ Attribute | Type | Default | Help
`layout` | reference to Layout widget | reference to new instance |
`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.
`tabbable` | `null` or boolean | `null` | Is widget tabbable?
`titles` | array of string | `[]` | Titles of the pages
`tooltip` | `null` or string | `null` | A tooltip caption.

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

0 comments on commit 78cdfcb

Please sign in to comment.