widget.js
309 lines
| 12.1 KiB
| application/javascript
|
JavascriptLexer
Jonathan Frederic
|
r15082 | var xor = function (a, b) {return !a ^ !b;}; | ||
Jonathan Frederic
|
r16828 | var isArray = function (a) { | ||
try { | ||||
return Object.toString.call(a) === "[object Array]" || Object.toString.call(a) === "[object RuntimeArray]"; | ||||
} catch (e) { | ||||
return Array.isArray(a); | ||||
} | ||||
}; | ||||
Jonathan Frederic
|
r15082 | var recursive_compare = function(a, b) { | ||
// Recursively compare two objects. | ||||
var same = true; | ||||
Jonathan Frederic
|
r16828 | same = same && !xor(a instanceof Object || typeof a == 'object', b instanceof Object || typeof b == 'object'); | ||
Jonathan Frederic
|
r15082 | same = same && !xor(isArray(a), isArray(b)); | ||
if (same) { | ||||
if (a instanceof Object) { | ||||
Jonathan Frederic
|
r15277 | var key; | ||
for (key in a) { | ||||
Jonathan Frederic
|
r15082 | if (a.hasOwnProperty(key) && !recursive_compare(a[key], b[key])) { | ||
same = false; | ||||
break; | ||||
} | ||||
} | ||||
Jonathan Frederic
|
r15277 | for (key in b) { | ||
Jonathan Frederic
|
r15082 | if (b.hasOwnProperty(key) && !recursive_compare(a[key], b[key])) { | ||
same = false; | ||||
break; | ||||
} | ||||
} | ||||
} else { | ||||
return a === b; | ||||
} | ||||
} | ||||
return same; | ||||
Jonathan Frederic
|
r15277 | }; | ||
Jonathan Frederic
|
r15082 | |||
Jonathan Frederic
|
r14309 | // Test the widget framework. | ||
casper.notebook_test(function () { | ||||
Jonathan Frederic
|
r14435 | var index; | ||
Jonathan Frederic
|
r14350 | |||
Jonathan Frederic
|
r14436 | index = this.append_cell( | ||
Jason Grout
|
r20887 | ['from IPython.html import widgets', | ||
'from IPython.display import display, clear_output', | ||||
'print("Success")'].join('\n')); | ||||
Jonathan Frederic
|
r14436 | this.execute_cell_then(index); | ||
Jonathan Frederic
|
r14435 | |||
Jonathan Frederic
|
r14350 | this.then(function () { | ||
Jonathan Frederic
|
r15277 | // Test multi-set, single touch code. First create a custom widget. | ||
Jonathan Frederic
|
r18899 | this.thenEvaluate(function() { | ||
Jonathan Frederic
|
r15277 | var MultiSetView = IPython.DOMWidgetView.extend({ | ||
render: function(){ | ||||
this.model.set('a', 1); | ||||
this.model.set('b', 2); | ||||
this.model.set('c', 3); | ||||
this.touch(); | ||||
}, | ||||
}); | ||||
Jonathan Frederic
|
r15278 | IPython.WidgetManager.register_widget_view('MultiSetView', MultiSetView); | ||
Jonathan Frederic
|
r15277 | }, {}); | ||
}); | ||||
// Try creating the multiset widget, verify that sets the values correctly. | ||||
var multiset = {}; | ||||
Jason Grout
|
r20887 | multiset.index = this.append_cell([ | ||
'from IPython.utils.traitlets import Unicode, CInt', | ||||
'class MultiSetWidget(widgets.Widget):', | ||||
' _view_name = Unicode("MultiSetView", sync=True)', | ||||
' a = CInt(0, sync=True)', | ||||
' b = CInt(0, sync=True)', | ||||
' c = CInt(0, sync=True)', | ||||
' d = CInt(-1, sync=True)', // See if it sends a full state. | ||||
' def set_state(self, sync_data):', | ||||
Jason Grout
|
r20915 | ' widgets.Widget.set_state(self, sync_data)', | ||
Jason Grout
|
r20887 | ' self.d = len(sync_data)', | ||
'multiset = MultiSetWidget()', | ||||
'display(multiset)', | ||||
'print(multiset.model_id)'].join('\n')); | ||||
Jonathan Frederic
|
r15277 | this.execute_cell_then(multiset.index, function(index) { | ||
multiset.model_id = this.get_output_cell(index).text.trim(); | ||||
}); | ||||
Jonathan Frederic
|
r18917 | this.wait_for_widget(multiset); | ||
Jonathan Frederic
|
r15277 | |||
index = this.append_cell( | ||||
'print("%d%d%d" % (multiset.a, multiset.b, multiset.c))'); | ||||
this.execute_cell_then(index, function(index) { | ||||
this.test.assertEquals(this.get_output_cell(index).text.trim(), '123', | ||||
'Multiple model.set calls and one view.touch update state in back-end.'); | ||||
Jonathan Frederic
|
r14309 | }); | ||
Jonathan Frederic
|
r15282 | index = this.append_cell( | ||
'print("%d" % (multiset.d))'); | ||||
this.execute_cell_then(index, function(index) { | ||||
this.test.assertEquals(this.get_output_cell(index).text.trim(), '3', | ||||
'Multiple model.set calls sent a partial state.'); | ||||
}); | ||||
Jonathan Frederic
|
r14995 | var textbox = {}; | ||
Jason Grout
|
r20887 | throttle_index = this.append_cell([ | ||
'import time', | ||||
'textbox = widgets.Text()', | ||||
'display(textbox)', | ||||
'textbox._dom_classes = ["my-throttle-textbox"]', | ||||
'def handle_change(name, old, new):', | ||||
' display(len(new))', | ||||
' time.sleep(0.5)', | ||||
'textbox.on_trait_change(handle_change, "value")', | ||||
'print(textbox.model_id)'].join('\n')); | ||||
Jonathan Frederic
|
r14463 | this.execute_cell_then(throttle_index, function(index){ | ||
Jonathan Frederic
|
r14995 | textbox.model_id = this.get_output_cell(index).text.trim(); | ||
Jonathan Frederic
|
r14463 | |||
this.test.assert(this.cell_element_exists(index, | ||||
'.widget-area .widget-subarea'), | ||||
'Widget subarea exists.'); | ||||
this.test.assert(this.cell_element_exists(index, | ||||
'.my-throttle-textbox'), 'Textbox exists.'); | ||||
// Send 20 characters | ||||
Jason Grout
|
r20847 | this.sendKeys('.my-throttle-textbox input', '12345678901234567890'); | ||
Jonathan Frederic
|
r14463 | }); | ||
Jonathan Frederic
|
r14995 | this.wait_for_widget(textbox); | ||
this.then(function () { | ||||
MinRK
|
r14797 | var outputs = this.evaluate(function(i) { | ||
return IPython.notebook.get_cell(i).output_area.outputs; | ||||
}, {i : throttle_index}); | ||||
Jonathan Frederic
|
r14463 | |||
// Only 4 outputs should have printed, but because of timing, sometimes | ||||
// 5 outputs will print. All we need to do is verify num outputs <= 5 | ||||
// because that is much less than 20. | ||||
MinRK
|
r14797 | this.test.assert(outputs.length <= 5, 'Messages throttled.'); | ||
Jonathan Frederic
|
r14463 | |||
// We also need to verify that the last state sent was correct. | ||||
MinRK
|
r18592 | var last_state = outputs[outputs.length-1].data['text/plain']; | ||
MinRK
|
r17307 | this.test.assertEquals(last_state, "20", "Last state sent when throttling."); | ||
Jonathan Frederic
|
r14625 | }); | ||
Jason Grout
|
r20887 | |||
Jason Grout
|
r20915 | this.thenEvaluate(function() { | ||
Jason Grout
|
r20936 | define('TestWidget', ['widgets/js/widget', 'base/js/utils', 'underscore'], function(widget, utils, _) { | ||
Jason Grout
|
r20915 | var floatArray = { | ||
deserialize: function (value, model) { | ||||
Jason Grout
|
r20936 | if (value===null) {return null;} | ||
Jason Grout
|
r20915 | // DataView -> float64 typed array | ||
return new Float64Array(value.buffer); | ||||
}, | ||||
Jason Grout
|
r20936 | // serialization automatically handled since the | ||
// attribute is an ArrayBuffer view | ||||
Jason Grout
|
r20915 | }; | ||
var floatList = { | ||||
deserialize: function (value, model) { | ||||
// list of floats -> list of strings | ||||
return value.map(function(x) {return x.toString()}); | ||||
}, | ||||
serialize: function(value, model) { | ||||
// list of strings -> list of floats | ||||
return value.map(function(x) {return parseFloat(x);}) | ||||
} | ||||
}; | ||||
Jason Grout
|
r20936 | |||
var TestWidgetModel = widget.WidgetModel.extend({}, { | ||||
serializers: _.extend({ | ||||
array_list: floatList, | ||||
array_binary: floatArray | ||||
}, widget.WidgetModel.serializers) | ||||
}); | ||||
var TestWidgetView = widget.DOMWidgetView.extend({ | ||||
render: function () { | ||||
this.listenTo(this.model, 'msg:custom', this.handle_msg); | ||||
}, | ||||
handle_msg: function(content, buffers) { | ||||
this.msg = [content, buffers]; | ||||
} | ||||
}); | ||||
return {TestWidgetModel: TestWidgetModel, TestWidgetView: TestWidgetView}; | ||||
Jason Grout
|
r20915 | }); | ||
Jason Grout
|
r20887 | }); | ||
Jason Grout
|
r20915 | var testwidget = {}; | ||
this.append_cell_execute_then([ | ||||
'from IPython.html import widgets', | ||||
'from IPython.utils.traitlets import Unicode, Instance, List', | ||||
'from IPython.display import display', | ||||
'from array import array', | ||||
'def _array_to_memoryview(x):', | ||||
Jason Grout
|
r20936 | ' if x is None: return None', | ||
Jason Grout
|
r20915 | ' try:', | ||
' y = memoryview(x)', | ||||
' except TypeError:', | ||||
Jason Grout
|
r20924 | ' # in python 2, arrays do not support the new buffer protocol', | ||
Jason Grout
|
r20915 | ' y = memoryview(buffer(x))', | ||
Jason Grout
|
r20936 | ' return y', | ||
Jason Grout
|
r20915 | 'def _memoryview_to_array(x):', | ||
Jason Grout
|
r20936 | ' if x is None: return None', | ||
Jason Grout
|
r20924 | ' return array("d", x.tobytes())', | ||
Jason Grout
|
r20915 | 'arrays_binary = {', | ||
Jason Grout
|
r20924 | ' "from_json": _memoryview_to_array,', | ||
' "to_json": _array_to_memoryview', | ||||
Jason Grout
|
r20915 | '}', | ||
'', | ||||
'def _array_to_list(x):', | ||||
Jason Grout
|
r20936 | ' return list(x)', | ||
Jason Grout
|
r20915 | 'def _list_to_array(x):', | ||
Jason Grout
|
r20924 | ' return array("d",x)', | ||
Jason Grout
|
r20915 | 'arrays_list = {', | ||
Jason Grout
|
r20924 | ' "from_json": _list_to_array,', | ||
' "to_json": _array_to_list', | ||||
Jason Grout
|
r20915 | '}', | ||
'', | ||||
'class TestWidget(widgets.DOMWidget):', | ||||
Jason Grout
|
r20936 | ' _model_module = Unicode("TestWidget", sync=True)', | ||
' _model_name = Unicode("TestWidgetModel", sync=True)', | ||||
Jason Grout
|
r20924 | ' _view_module = Unicode("TestWidget", sync=True)', | ||
Jason Grout
|
r20936 | ' _view_name = Unicode("TestWidgetView", sync=True)', | ||
' array_binary = Instance(array, allow_none=True, sync=True, **arrays_binary)', | ||||
' array_list = Instance(array, args=("d", [3.0]), allow_none=False, sync=True, **arrays_list)', | ||||
Jason Grout
|
r20915 | ' msg = {}', | ||
' def __init__(self, **kwargs):', | ||||
' super(widgets.DOMWidget, self).__init__(**kwargs)', | ||||
' self.on_msg(self._msg)', | ||||
' def _msg(self, _, content, buffers):', | ||||
' self.msg = [content, buffers]', | ||||
'x=TestWidget()', | ||||
'display(x)', | ||||
Jason Grout
|
r20926 | 'print(x.model_id)'].join('\n'), function(index){ | ||
Jason Grout
|
r20915 | testwidget.index = index; | ||
testwidget.model_id = this.get_output_cell(index).text.trim(); | ||||
}); | ||||
this.wait_for_widget(testwidget); | ||||
this.append_cell_execute_then('x.array_list = array("d", [1.5, 2.0, 3.1])'); | ||||
this.wait_for_widget(testwidget); | ||||
this.then(function() { | ||||
var result = this.evaluate(function(index) { | ||||
var v = IPython.notebook.get_cell(index).widget_views[0]; | ||||
var result = v.model.get('array_list'); | ||||
var z = result.slice(); | ||||
z[0]+="1234"; | ||||
z[1]+="5678"; | ||||
v.model.set('array_list', z); | ||||
v.touch(); | ||||
return result; | ||||
}, testwidget.index); | ||||
this.test.assertEquals(result, ["1.5", "2", "3.1"], "JSON custom serializer kernel -> js"); | ||||
}); | ||||
Jason Grout
|
r20887 | |||
Jason Grout
|
r20926 | this.assert_output_equals('print(x.array_list.tolist() == [1.51234, 25678.0, 3.1])', | ||
Jason Grout
|
r20915 | 'True', 'JSON custom serializer js -> kernel'); | ||
if (this.slimerjs) { | ||||
this.append_cell_execute_then("x.array_binary=array('d', [1.5,2.5,5])", function() { | ||||
this.evaluate(function(index) { | ||||
var v = IPython.notebook.get_cell(index).widget_views[0]; | ||||
var z = v.model.get('array_binary'); | ||||
z[0]*=3; | ||||
z[1]*=3; | ||||
z[2]*=3; | ||||
// we set to null so that we recognize the change | ||||
// when we set data back to z | ||||
v.model.set('array_binary', null); | ||||
v.model.set('array_binary', z); | ||||
v.touch(); | ||||
}, textwidget.index); | ||||
}); | ||||
this.wait_for_widget(testwidget); | ||||
this.assert_output_equals('x.array_binary.tolist() == [4.5, 7.5, 15.0]', | ||||
'True\n', 'Binary custom serializer js -> kernel') | ||||
this.append_cell_execute_then('x.send("some content", [memoryview(b"binarycontent"), memoryview("morecontent")])'); | ||||
this.wait_for_widget(testwidget); | ||||
this.then(function() { | ||||
var result = this.evaluate(function(index) { | ||||
var v = IPython.notebook.get_cell(index).widget_views[0]; | ||||
var d = new TextDecoder('utf-8'); | ||||
return {text: v.msg[0], | ||||
binary0: d.decode(v.msg[1][0]), | ||||
binary1: d.decode(v.msg[1][1])}; | ||||
}, testwidget.index); | ||||
this.test.assertEquals(result, {text: 'some content', | ||||
binary0: 'binarycontent', | ||||
binary1: 'morecontent'}, | ||||
"Binary widget messages kernel -> js"); | ||||
}); | ||||
this.then(function() { | ||||
this.evaluate(function(index) { | ||||
var v = IPython.notebook.get_cell(index).widget_views[0]; | ||||
v.send('content back', [new Uint8Array([1,2,3,4]), new Float64Array([2.1828, 3.14159])]) | ||||
}, testwidget.index); | ||||
}); | ||||
this.wait_for_widget(testwidget); | ||||
this.assert_output_equals([ | ||||
'all([x.msg[0] == "content back",', | ||||
' x.msg[1][0].tolist() == [1,2,3,4],', | ||||
' array("d", x.msg[1][1].tobytes()).tolist() == [2.1828, 3.14159]])'].join('\n'), | ||||
'True', 'Binary buffers message js -> kernel'); | ||||
} else { | ||||
console.log("skipping binary websocket tests on phantomjs"); | ||||
} | ||||
Jason Grout
|
r20887 | |||
Jonathan Frederic
|
r14454 | }); | ||