notebook/IPython/html/widgets/widget_container.py

74 lines
2.5 KiB
Python
Raw Normal View History

"""Container class.
2013-10-26 02:31:48 +08:00
Represents a container that can be used to group other widgets.
"""
# Copyright (c) IPython Development Team.
2013-10-26 02:31:48 +08:00
# Distributed under the terms of the Modified BSD License.
2014-01-07 20:04:24 +08:00
from .widget import DOMWidget
2014-07-12 21:32:49 +08:00
from IPython.utils.traitlets import Unicode, Tuple, TraitError, Int, CaselessStrEnum
from IPython.utils.warn import DeprecatedClass
2013-10-17 14:15:24 +08:00
class Container(DOMWidget):
"""Displays multiple widgets in a group."""
2014-01-23 08:21:00 +08:00
_view_name = Unicode('ContainerView', sync=True)
# Child widgets in the container.
# Using a tuple here to force reassignment to update the list.
# When a proper notifying-list trait exists, that is what should be used here.
children = Tuple(sync=True, allow_none=False)
def __init__(self, children = (), **kwargs):
kwargs['children'] = children
super(Container, self).__init__(**kwargs)
self.on_displayed(Container._fire_children_displayed)
def _fire_children_displayed(self):
2014-06-11 00:57:50 +08:00
for child in self.children:
child._handle_displayed()
2014-01-16 22:20:04 +08:00
class Popup(Container):
"""Displays multiple widgets in an in page popup div."""
2014-01-23 08:21:00 +08:00
_view_name = Unicode('PopupView', sync=True)
description = Unicode(sync=True)
button_text = Unicode(sync=True)
2014-07-12 21:32:49 +08:00
class FlexContainer(Container):
"""Displays multiple widgets using the flexible box model."""
2014-07-12 21:32:49 +08:00
_view_name = Unicode('FlexContainerView', sync=True)
2014-07-19 05:34:39 +08:00
orientation = CaselessStrEnum(values=['vertical', 'horizontal'], default_value='vertical', sync=True)
2014-07-12 21:32:49 +08:00
flex = Int(0, sync=True, help="""Specify the flexible-ness of the model.""")
def _flex_changed(self, name, old, new):
new = min(max(0, new), 2)
if self.flex != new:
self.flex = new
2014-07-16 05:46:11 +08:00
_locations = ['start', 'center', 'end', 'baseline', 'stretch']
pack = CaselessStrEnum(
values=_locations,
default_value='start', allow_none=False, sync=True)
align = CaselessStrEnum(
values=_locations,
default_value='start', allow_none=False, sync=True)
2014-07-12 21:32:49 +08:00
2014-07-19 05:34:39 +08:00
def VBox(*pargs, **kwargs):
"""Displays multiple widgets vertically using the flexible box model."""
2014-07-19 05:34:39 +08:00
kwargs['orientation'] = 'vertical'
return FlexContainer(*pargs, **kwargs)
2014-07-12 21:32:49 +08:00
2014-07-19 05:34:39 +08:00
def HBox(*pargs, **kwargs):
"""Displays multiple widgets horizontally using the flexible box model."""
2014-07-19 05:34:39 +08:00
kwargs['orientation'] = 'horizontal'
return FlexContainer(*pargs, **kwargs)
2014-07-12 21:32:49 +08:00
# Remove in IPython 4.0
ContainerWidget = DeprecatedClass(Container, 'ContainerWidget')
PopupWidget = DeprecatedClass(Popup, 'PopupWidget')