-
Notifications
You must be signed in to change notification settings - Fork 942
/
widget_box.py
139 lines (106 loc) · 3.89 KB
/
widget_box.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
"""Box widgets.
These widgets are containers that can be used to
group other widgets together and control their
relative layouts.
"""
from .widget import register, widget_serialization, Widget
from .domwidget import DOMWidget
from .widget_core import CoreWidget
from .docutils import doc_subst
from .trait_types import TypedTuple
from traitlets import Unicode, CaselessStrEnum, Instance
_doc_snippets = {}
_doc_snippets['box_params'] = """
children: iterable of Widget instances
list of widgets to display
box_style: str
one of 'success', 'info', 'warning' or 'danger', or ''.
Applies a predefined style to the box. Defaults to '',
which applies no pre-defined style.
"""
@register
@doc_subst(_doc_snippets)
class Box(DOMWidget, CoreWidget):
""" Displays multiple widgets in a group.
The widgets are laid out horizontally.
Parameters
----------
{box_params}
Examples
--------
>>> import ipywidgets as widgets
>>> title_widget = widgets.HTML('<em>Box Example</em>')
>>> slider = widgets.IntSlider()
>>> widgets.Box([title_widget, slider])
"""
_model_name = Unicode('BoxModel').tag(sync=True)
_view_name = Unicode('BoxView').tag(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, use that instead.
children = TypedTuple(trait=Instance(Widget), help="List of widget children").tag(
sync=True, **widget_serialization)
box_style = CaselessStrEnum(
values=['success', 'info', 'warning', 'danger', ''], default_value='',
help="""Use a predefined styling for the box.""").tag(sync=True)
def __init__(self, children=(), **kwargs):
kwargs['children'] = children
super(Box, self).__init__(**kwargs)
self.on_displayed(Box._fire_children_displayed)
def _fire_children_displayed(self):
for child in self.children:
child._handle_displayed()
def __copy__(self):
cls = self.__class__
result = cls.__new__(cls)
result.__init__(children=tuple([*self.children]))
# Deep Copy
# from copy import copy
# result.__init__(children=tuple(copy(child) for child in self.children))
new_state = {key: value for key, value in self.get_state().items() if
not key.startswith('_') and
not key == 'layout' and
not key == 'style' and
not key == 'children'}
for key, value in new_state.items():
setattr(result, key, value)
result.layout = self.layout
return result
@register
@doc_subst(_doc_snippets)
class VBox(Box):
""" Displays multiple widgets vertically using the flexible box model.
Parameters
----------
{box_params}
Examples
--------
>>> import ipywidgets as widgets
>>> title_widget = widgets.HTML('<em>Vertical Box Example</em>')
>>> slider = widgets.IntSlider()
>>> widgets.VBox([title_widget, slider])
"""
_model_name = Unicode('VBoxModel').tag(sync=True)
_view_name = Unicode('VBoxView').tag(sync=True)
@register
@doc_subst(_doc_snippets)
class HBox(Box):
""" Displays multiple widgets horizontally using the flexible box model.
Parameters
----------
{box_params}
Examples
--------
>>> import ipywidgets as widgets
>>> title_widget = widgets.HTML('<em>Horizontal Box Example</em>')
>>> slider = widgets.IntSlider()
>>> widgets.HBox([title_widget, slider])
"""
_model_name = Unicode('HBoxModel').tag(sync=True)
_view_name = Unicode('HBoxView').tag(sync=True)
@register
class GridBox(Box):
_model_name = Unicode('GridBoxModel').tag(sync=True)
_view_name = Unicode('GridBoxView').tag(sync=True)