##// END OF EJS Templates
Merge pull request #7757 from jasongrout/custom-serialization...
Merge pull request #7757 from jasongrout/custom-serialization Custom serialization

File last commit:

r20935:a1dba7fe
r21035:d6e249b0 merge
Show More
widget_box.py
107 lines | 3.7 KiB | text/x-python | PythonLexer
Jonathan Frederic
s/Container/Box
r17637 """Box class.
Jonathan Frederic
Cleaned up Python widget code.
r14283
Represents a container that can be used to group other widgets.
"""
Jonathan Frederic
Allow a widget to be displayed more than once within a parent widget.
r17172
# Copyright (c) IPython Development Team.
Jonathan Frederic
Cleaned up Python widget code.
r14283 # Distributed under the terms of the Modified BSD License.
Jason Grout
Change custom serialization to use custom models, rather than transmitting the serializer name across the wire...
r20935 from .widget import DOMWidget, Widget, register
Jonathan Frederic
Embrace flexible box model
r17596 from IPython.utils.traitlets import Unicode, Tuple, TraitError, Int, CaselessStrEnum
Jonathan Frederic
Renamed *Widget to *,...
r17598 from IPython.utils.warn import DeprecatedClass
Jonathan Frederic
Add container widget
r14239
Jason Grout
Change custom serialization to use custom models, rather than transmitting the serializer name across the wire...
r20935 def _widget_to_json(x):
if isinstance(x, dict):
return {k: _widget_to_json(v) for k, v in x.items()}
elif isinstance(x, (list, tuple)):
return [_widget_to_json(v) for v in x]
elif isinstance(x, Widget):
return "IPY_MODEL_" + x.model_id
else:
return x
def _json_to_widget(x):
if isinstance(x, dict):
return {k: _json_to_widget(v) for k, v in x.items()}
elif isinstance(x, (list, tuple)):
return [_json_to_widget(v) for v in x]
elif isinstance(x, string_types) and x.startswith('IPY_MODEL_') and x[10:] in Widget.widgets:
return Widget.widgets[x[10:]]
else:
return x
widget_serialization = {
'from_json': _json_to_widget,
'to_json': _widget_to_json
}
Sylvain Corlay
jupyter -> IPython
r18533 @register('IPython.Box')
Jonathan Frederic
s/Container/Box
r17637 class Box(DOMWidget):
Jonathan Frederic
Added some doc strings on the widgets....
r17602 """Displays multiple widgets in a group."""
Jason Grout
Change custom serialization to use custom models, rather than transmitting the serializer name across the wire...
r20935 _model_name = Unicode('BoxModel', sync=True)
Jonathan Frederic
s/Container/Box
r17637 _view_name = Unicode('BoxView', sync=True)
Jonathan Frederic
Attempt 1, HBox and VBox implementation.
r14268
MinRK
don't validate ContainerWidget.children...
r15480 # 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.
Jason Grout
work-in-progress for custom js serializers
r20839 children = Tuple(sync=True, **widget_serialization)
Jonathan Frederic
Fix some bugs found by the widget examples,...
r17727
_overflow_values = ['visible', 'hidden', 'scroll', 'auto', 'initial', 'inherit', '']
overflow_x = CaselessStrEnum(
values=_overflow_values,
Sylvain Corlay
removing redundant allow_none=False
r20483 default_value='', sync=True, help="""Specifies what
Jonathan Frederic
Fix some bugs found by the widget examples,...
r17727 happens to content that is too large for the rendered region.""")
overflow_y = CaselessStrEnum(
values=_overflow_values,
Sylvain Corlay
removing redundant allow_none=False
r20483 default_value='', sync=True, help="""Specifies what
Jonathan Frederic
Fix some bugs found by the widget examples,...
r17727 happens to content that is too large for the rendered region.""")
zah
Children fire event...
r15446
Jonathan Frederic
Added Bootstrap specific classes,...
r17728 box_style = CaselessStrEnum(
values=['success', 'info', 'warning', 'danger', ''],
default_value='', allow_none=True, sync=True, help="""Use a
predefined styling for the box.""")
Jason Grout
Container assumes the children attribute is not None...
r17266 def __init__(self, children = (), **kwargs):
Jason Grout
Make Container widgets take children as the first positional argument...
r17261 kwargs['children'] = children
Jonathan Frederic
s/Container/Box
r17637 super(Box, self).__init__(**kwargs)
self.on_displayed(Box._fire_children_displayed)
zah
Children fire event...
r15446
def _fire_children_displayed(self):
Jonathan Frederic
Fixed buggy behavior
r17173 for child in self.children:
zah
Children fire event...
r15446 child._handle_displayed()
Jonathan Frederic
More PEP8 changes
r14607
Sylvain Corlay
jupyter -> IPython
r18533 @register('IPython.FlexBox')
Jonathan Frederic
s/Container/Box
r17637 class FlexBox(Box):
Jonathan Frederic
Added some doc strings on the widgets....
r17602 """Displays multiple widgets using the flexible box model."""
Jonathan Frederic
s/Container/Box
r17637 _view_name = Unicode('FlexBoxView', sync=True)
Jonathan Frederic
Make HBox and VBox helper functions
r17601 orientation = CaselessStrEnum(values=['vertical', 'horizontal'], default_value='vertical', sync=True)
Jonathan Frederic
Embrace flexible box model
r17596 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
Jonathan Frederic
Added baseline and stretch
r17597 _locations = ['start', 'center', 'end', 'baseline', 'stretch']
pack = CaselessStrEnum(
values=_locations,
Sylvain Corlay
removing redundant allow_none=False
r20483 default_value='start', sync=True)
Jonathan Frederic
Added baseline and stretch
r17597 align = CaselessStrEnum(
values=_locations,
Sylvain Corlay
removing redundant allow_none=False
r20483 default_value='start', sync=True)
Jonathan Frederic
Embrace flexible box model
r17596
Jonathan Frederic
Make HBox and VBox helper functions
r17601 def VBox(*pargs, **kwargs):
Jonathan Frederic
Added some doc strings on the widgets....
r17602 """Displays multiple widgets vertically using the flexible box model."""
Jonathan Frederic
Make HBox and VBox helper functions
r17601 kwargs['orientation'] = 'vertical'
Jonathan Frederic
s/Container/Box
r17637 return FlexBox(*pargs, **kwargs)
Jonathan Frederic
Embrace flexible box model
r17596
Jonathan Frederic
Make HBox and VBox helper functions
r17601 def HBox(*pargs, **kwargs):
Jonathan Frederic
Added some doc strings on the widgets....
r17602 """Displays multiple widgets horizontally using the flexible box model."""
Jonathan Frederic
Make HBox and VBox helper functions
r17601 kwargs['orientation'] = 'horizontal'
Jonathan Frederic
s/Container/Box
r17637 return FlexBox(*pargs, **kwargs)
Jonathan Frederic
Embrace flexible box model
r17596
Jonathan Frederic
Renamed *Widget to *,...
r17598
Jonathan Frederic
Added some doc strings on the widgets....
r17602 # Remove in IPython 4.0
Jonathan Frederic
s/Container/Box
r17637 ContainerWidget = DeprecatedClass(Box, 'ContainerWidget')