Skip to content

Commit

Permalink
Remove the Proxy and PlaceProxy widgets.
Browse files Browse the repository at this point in the history
Many uses of the Proxy widget can use a box with a single child instead. The PlaceProxy widget modifies arbitrary DOM elements on a page, which gets very tricky especially with multiple copies of the same notebook in a browser window. If there is a compelling need for something like PlaceProxy, we can spin it off into a separate widget package.
  • Loading branch information
jasongrout committed Feb 17, 2017
1 parent e13f32e commit 4aeb918
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 152 deletions.
2 changes: 0 additions & 2 deletions docs/source/examples/Widget List.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@
" 'Jupyter.IntSlider',\n",
" 'Jupyter.IntText',\n",
" 'Jupyter.Label',\n",
" 'Jupyter.PlaceProxy',\n",
" 'Jupyter.Play',\n",
" 'Jupyter.Proxy',\n",
" 'Jupyter.RadioButtons',\n",
" 'Jupyter.Select',\n",
" 'Jupyter.SelectMultiple',\n",
Expand Down
2 changes: 1 addition & 1 deletion ipywidgets/widgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .widget_core import CoreWidget
from .widget_bool import Checkbox, ToggleButton, Valid
from .widget_button import Button, ButtonStyle
from .widget_box import Box, Proxy, PlaceProxy, HBox, VBox
from .widget_box import Box, HBox, VBox
from .widget_float import FloatText, BoundedFloatText, FloatSlider, FloatProgress, FloatRangeSlider
from .widget_image import Image
from .widget_int import IntText, BoundedIntText, IntSlider, IntProgress, IntRangeSlider, Play, SliderStyle
Expand Down
30 changes: 0 additions & 30 deletions ipywidgets/widgets/widget_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,33 +53,3 @@ class HBox(Box):
"""Displays multiple widgets horizontally using the flexible box model."""
_model_name = Unicode('HBoxModel').tag(sync=True)
_view_name = Unicode('HBoxView').tag(sync=True)


@register('Jupyter.Proxy')
class Proxy(DOMWidget, CoreWidget):
"""A DOMWidget that holds another DOMWidget or nothing."""
_model_module = Unicode('jupyter-js-widgets').tag(sync=True)
_view_module = Unicode('jupyter-js-widgets').tag(sync=True)
_model_name = Unicode('ProxyModel').tag(sync=True)
_view_name = Unicode('ProxyView').tag(sync=True)

# Child widget of the Proxy
child = Instance(DOMWidget, allow_none=True).tag(sync=True, **widget_serialization)

def __init__(self, child, **kwargs):
kwargs['child'] = child
super(Proxy, self).__init__(**kwargs)
self.on_displayed(Proxy._fire_child_displayed)

def _fire_child_displayed(self):
if self.child is not None:
self.child._handle_displayed()


@register('Jupyter.PlaceProxy')
class PlaceProxy(Proxy):
"""Renders the child widget at the specified selector."""
_view_name = Unicode('PlaceProxyView').tag(sync=True)
_model_name = Unicode('PlaceProxyModel').tag(sync=True)
selector = Unicode().tag(sync=True)

119 changes: 0 additions & 119 deletions jupyter-js-widgets/src/widget_box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,125 +109,6 @@ class VBoxModel extends BoxModel {
}
}

export
class ProxyModel extends CoreDOMWidgetModel {
defaults() {
return _.extend(super.defaults(), {
_view_name: 'ProxyView',
_model_name: 'ProxyModel',
child: null
});
}

static serializers = _.extend({
child: {deserialize: unpack_models}
}, CoreDOMWidgetModel.serializers);
}

export
class ProxyView extends DOMWidgetView {
_createElement(tagName: string) {
this.pWidget = new JupyterPhosphorPanelWidget({ view: this });
return this.pWidget.node;
}

_setElement(el: HTMLElement) {
if (this.el || el !== this.pWidget.node) {
// Proxies don't allow setting the element beyond the initial creation.
throw new Error('Cannot reset the DOM element.');
}

this.el = this.pWidget.node;
this.$el = $(this.pWidget.node);
}

initialize(parameters) {
// Public constructor
super.initialize(parameters);
this.el.classList.add('jupyter-widgets');
this.el.classList.add('widget-container');
this.box = this.el;
this.child_promise = Promise.resolve();
}

render() {
let child_view = this.set_child(this.model.get('child'));
this.listenTo(this.model, 'change:child', (model, value) => {
this.set_child(value);
});
return child_view;
}

remove() {
this.child_promise.then(() => {
if (this.child) {
this.child.remove();
}
});
super.remove();
}

set_child(value) {
this.child_promise = this.child_promise.then(()=> {
if (this.child) {
this.child.remove();
}
});
if (value) {
this.child_promise = this.child_promise.then(() => {
return this.create_child_view(value).then((view: DOMWidgetView) => {
this.pWidget.addWidget(view.pWidget);
this.child = view;
this.trigger('child:created');
}).catch(reject('Could not add child view to proxy', true));
});
}
return this.child_promise;
}

pWidget: JupyterPhosphorPanelWidget;
/**
* The element that contains the child
*/
box: HTMLElement;
/**
* TODO: Should be Promise<DOMWidgetView>, but we set it to Promise<void> at the start. Why???
*/
child_promise: Promise<any>;
child: any;

}

export
class PlaceProxyModel extends ProxyModel {
defaults() {
return _.extend(super.defaults(), {
_view_name: 'PlaceProxyView',
_model_name: 'PlaceProxyModel',
selector: ''
})
}
}

export
class PlaceProxyView extends ProxyView {
render() {
this.update_selector(this.model, this.model.get('selector'));
this.listenTo(this.model, 'change:selector', this.update_selector);
return super.render();
}

update_selector(model, selector) {
// attach ourselves to the selected element
if (selector) {
let host = document.querySelector(selector);
if (host) {
Widget.attach(this.pWidget, host);
}
}
}
}

export
class BoxView extends DOMWidgetView {

Expand Down

0 comments on commit 4aeb918

Please sign in to comment.