From 3afdea41911bfffeb6c4ae8536f6db58d1d08b9d 2015-04-06 16:16:42 From: Sylvain Corlay Date: 2015-04-06 16:16:42 Subject: [PATCH] Move _unpack_models to widget.js and widget serialization to widget.py --- diff --git a/IPython/html/static/widgets/js/widget.js b/IPython/html/static/widgets/js/widget.js index a643ea0..5ad4ef5 100644 --- a/IPython/html/static/widgets/js/widget.js +++ b/IPython/html/static/widgets/js/widget.js @@ -10,6 +10,31 @@ define(["widgets/js/manager", ], function(widgetmanager, _, Backbone, $, utils, IPython){ "use strict"; + var unpack_models = function unpack_models(value, model) { + /** + * Replace model ids with models recursively. + */ + var unpacked; + if ($.isArray(value)) { + unpacked = []; + _.each(value, function(sub_value, key) { + unpacked.push(unpack_models(sub_value, model)); + }); + return Promise.all(unpacked); + } else if (value instanceof Object) { + unpacked = {}; + _.each(value, function(sub_value, key) { + unpacked[key] = unpack_models(sub_value, model); + }); + return utils.resolve_promises_dict(unpacked); + } else if (typeof value === 'string' && value.slice(0,10) === "IPY_MODEL_") { + // get_model returns a promise already + return model.widget_manager.get_model(value.slice(10, value.length)); + } else { + return Promise.resolve(value); + } + }; + var WidgetModel = Backbone.Model.extend({ constructor: function (widget_manager, model_id, comm) { /** @@ -767,6 +792,7 @@ define(["widgets/js/manager", }); var widget = { + 'unpack_models': unpack_models, 'WidgetModel': WidgetModel, 'WidgetView': WidgetView, 'DOMWidgetView': DOMWidgetView, diff --git a/IPython/html/static/widgets/js/widget_box.js b/IPython/html/static/widgets/js/widget_box.js index 2b1d478..8cbf212 100644 --- a/IPython/html/static/widgets/js/widget_box.js +++ b/IPython/html/static/widgets/js/widget_box.js @@ -9,34 +9,10 @@ define([ "bootstrap", ], function(widget, $, _, utils){ "use strict"; - var unpack_models = function unpack_models(value, model) { - /** - * Replace model ids with models recursively. - */ - var unpacked; - if ($.isArray(value)) { - unpacked = []; - _.each(value, function(sub_value, key) { - unpacked.push(unpack_models(sub_value, model)); - }); - return Promise.all(unpacked); - } else if (value instanceof Object) { - unpacked = {}; - _.each(value, function(sub_value, key) { - unpacked[key] = unpack_models(sub_value, model); - }); - return utils.resolve_promises_dict(unpacked); - } else if (typeof value === 'string' && value.slice(0,10) === "IPY_MODEL_") { - // get_model returns a promise already - return model.widget_manager.get_model(value.slice(10, value.length)); - } else { - return Promise.resolve(value); - } - }; var BoxModel = widget.WidgetModel.extend({}, { serializers: _.extend({ - children: {deserialize: unpack_models} + children: {deserialize: widget.unpack_models} }, widget.WidgetModel.serializers) }); @@ -179,7 +155,6 @@ define([ }); return { - 'unpack_models': unpack_models, 'BoxModel': BoxModel, 'BoxView': BoxView, 'FlexBoxView': FlexBoxView, diff --git a/IPython/html/widgets/__init__.py b/IPython/html/widgets/__init__.py index bf9a421..a91d3a0 100644 --- a/IPython/html/widgets/__init__.py +++ b/IPython/html/widgets/__init__.py @@ -1,4 +1,4 @@ -from .widget import Widget, DOMWidget, CallbackDispatcher, register +from .widget import Widget, DOMWidget, CallbackDispatcher, register, widget_serialization from .trait_types import Color diff --git a/IPython/html/widgets/widget.py b/IPython/html/widgets/widget.py index 28a24fb..bbc2fb5 100644 --- a/IPython/html/widgets/widget.py +++ b/IPython/html/widgets/widget.py @@ -24,6 +24,33 @@ from IPython.utils.traitlets import Unicode, Dict, Instance, Bool, List, \ from IPython.utils.py3compat import string_types from .trait_types import Color + +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 +} + + #----------------------------------------------------------------------------- # Classes #----------------------------------------------------------------------------- diff --git a/IPython/html/widgets/widget_box.py b/IPython/html/widgets/widget_box.py index b751f89..4a38ee6 100644 --- a/IPython/html/widgets/widget_box.py +++ b/IPython/html/widgets/widget_box.py @@ -6,35 +6,10 @@ Represents a container that can be used to group other widgets. # Copyright (c) IPython Development Team. # Distributed under the terms of the Modified BSD License. -from .widget import DOMWidget, Widget, register +from .widget import DOMWidget, Widget, register, widget_serialization from IPython.utils.traitlets import Unicode, Tuple, TraitError, Int, CaselessStrEnum from .deprecated import DeprecatedClass -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 -} - @register('IPython.Box') class Box(DOMWidget):