mirror of
https://github.com/jupyter/notebook.git
synced 2025-03-19 13:20:36 +08:00
Merge pull request #6582 from jdfreder/symmetric_state
Symmetric widget get/set_state
This commit is contained in:
commit
0d9db64180
@ -69,7 +69,7 @@ define(["widgets/js/manager",
|
||||
var method = msg.content.data.method;
|
||||
switch (method) {
|
||||
case 'update':
|
||||
this.apply_update(msg.content.data.state);
|
||||
this.set_state(msg.content.data.state);
|
||||
break;
|
||||
case 'custom':
|
||||
this.trigger('msg:custom', msg.content.data.content);
|
||||
@ -80,7 +80,7 @@ define(["widgets/js/manager",
|
||||
}
|
||||
},
|
||||
|
||||
apply_update: function (state) {
|
||||
set_state: function (state) {
|
||||
// Handle when a widget is updated via the python side.
|
||||
this.state_lock = state;
|
||||
try {
|
||||
|
@ -118,8 +118,8 @@ casper.notebook_test(function () {
|
||||
' b = CInt(0, sync=True)\n' +
|
||||
' c = CInt(0, sync=True)\n' +
|
||||
' d = CInt(-1, sync=True)\n' + // See if it sends a full state.
|
||||
' def _handle_receive_state(self, sync_data):\n' +
|
||||
' widgets.Widget._handle_receive_state(self, sync_data)\n'+
|
||||
' def set_state(self, sync_data):\n' +
|
||||
' widgets.Widget.set_state(self, sync_data)\n'+
|
||||
' self.d = len(sync_data)\n' +
|
||||
'multiset = MultiSetWidget()\n' +
|
||||
'display(multiset)\n' +
|
||||
|
@ -208,6 +208,15 @@ class Widget(LoggingConfigurable):
|
||||
value = getattr(self, k)
|
||||
state[k] = f(value)
|
||||
return state
|
||||
|
||||
def set_state(self, sync_data):
|
||||
"""Called when a state is received from the front-end."""
|
||||
for name in self.keys:
|
||||
if name in sync_data:
|
||||
json_value = sync_data[name]
|
||||
from_json = self.trait_metadata(name, 'from_json', self._trait_from_json)
|
||||
with self._lock_property(name, json_value):
|
||||
setattr(self, name, from_json(json_value))
|
||||
|
||||
def send(self, content):
|
||||
"""Sends a custom msg to the widget model in the front-end.
|
||||
@ -304,22 +313,13 @@ class Widget(LoggingConfigurable):
|
||||
# Handle backbone sync methods CREATE, PATCH, and UPDATE all in one.
|
||||
if method == 'backbone' and 'sync_data' in data:
|
||||
sync_data = data['sync_data']
|
||||
self._handle_receive_state(sync_data) # handles all methods
|
||||
self.set_state(sync_data) # handles all methods
|
||||
|
||||
# Handle a custom msg from the front-end
|
||||
elif method == 'custom':
|
||||
if 'content' in data:
|
||||
self._handle_custom_msg(data['content'])
|
||||
|
||||
def _handle_receive_state(self, sync_data):
|
||||
"""Called when a state is received from the front-end."""
|
||||
for name in self.keys:
|
||||
if name in sync_data:
|
||||
json_value = sync_data[name]
|
||||
from_json = self.trait_metadata(name, 'from_json', self._trait_from_json)
|
||||
with self._lock_property(name, json_value):
|
||||
setattr(self, name, from_json(json_value))
|
||||
|
||||
def _handle_custom_msg(self, content):
|
||||
"""Called when a custom msg is received."""
|
||||
self._msg_callbacks(self, content)
|
||||
|
Loading…
x
Reference in New Issue
Block a user