Skip to content

Commit 9834a39

Browse files
committed
Make the widget update message the same no matter the direction of syncing.
1 parent c5b88cd commit 9834a39

File tree

3 files changed

+17
-35
lines changed

3 files changed

+17
-35
lines changed

ipywidgets/widgets/widget.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -508,14 +508,12 @@ def _handle_msg(self, msg):
508508
data = msg['content']['data']
509509
method = data['method']
510510

511-
# Handle backbone sync methods CREATE, PATCH, and UPDATE all in one.
512-
if method == 'backbone':
513-
if 'sync_data' in data:
514-
# get binary buffers too
515-
sync_data = data['sync_data']
511+
if method == 'update':
512+
if 'state' in data:
513+
state = data['state']
516514
if 'buffer_paths' in data:
517-
_put_buffers(sync_data, data['buffer_paths'], msg['buffers'])
518-
self.set_state(sync_data) # handles all methods
515+
_put_buffers(state, data['buffer_paths'], msg['buffers'])
516+
self.set_state(state)
519517

520518
# Handle a state request.
521519
elif method == 'request_state':

jupyter-js-widgets/src/widget.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,13 @@ class WidgetModel extends Backbone.Model {
237237
if (msg.content.execution_state ==='idle') {
238238
// Send buffer if this message caused another message to be
239239
// throttled.
240+
// TODO: make sure this is handled just like the original syncing.
241+
// Right now, it doesn't take into account the new binary_paths key
240242
if (this.msg_buffer !== null &&
241243
(this.get('msg_throttle') || 1) === this.pending_msgs) {
242244
var data = {
243-
method: 'backbone',
244-
sync_method: 'update',
245-
sync_data: this.msg_buffer
245+
method: 'update',
246+
state: this.msg_buffer
246247
};
247248
this.comm.send(data, callbacks);
248249
this.msg_buffer = null;
@@ -398,8 +399,8 @@ class WidgetModel extends Backbone.Model {
398399
// on the python side the inverse happens
399400
var split = utils.remove_buffers(state);
400401
this.comm.send({
401-
method: 'backbone',
402-
sync_data: split.state,
402+
method: 'update',
403+
state: split.state,
403404
buffer_paths: split.buffer_paths
404405
}, callbacks, {}, split.buffers);
405406
}).catch((error) => {

jupyter-widgets-schema/messages.md

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ In order to display widgets in both the classic notebook and JupyterLab, ipywidg
210210

211211

212212

213-
# Widget messaging protocol, version 2
213+
# Widget messaging protocol, version 2 (not finalized)
214214

215215
A Jupyter widget has both a frontend and kernel object communicating with each other using the `comm` messages provided by the Jupyter kernel messaging protocol. The primary communication that happens is synchronizing widget state, represented in the messages by a dictionary of key-value pairs. The Jupyter widget messaging protocol covers `comm` messages for the following actions:
216216

@@ -227,6 +227,8 @@ Throughout this document, relevant parts of messages to the discussion are quote
227227

228228
The `jupyter.widget.version` comm target and associated version validation messages are gone. Instead, it is up to the package maintainers to ensure that the versions of the packages speak the same widget message protocol. We encourage kernel and frontend package developers to clearly indicate which protocol version the package supports.
229229

230+
The sync update event from the frontend to the kernel was restructured to have the same field names as the event from the kernel to the frontend, namely the method field is `'update'` and the state data is in the `state` attribute.
231+
230232
## Implementating the Jupyter widgets protocol in the kernel
231233

232234
In this section, we concentrate on implementing the Jupyter widget messaging protocol in the kernel.
@@ -275,9 +277,9 @@ The type of widget to be instantiated in the frontend is determined by the `_mod
275277

276278
### State synchronization
277279

278-
#### Synchronizing from kernel to frontend: `update`
280+
#### Synchronizing widget state: `update`
279281

280-
When a widget's state changes in the kernel, the changed state keys and values are sent to the frontend over the widget's comm channel using an `update` message:
282+
When a widget's state changes in either the kernel or the frontend, the changed state keys and values are sent to the other side over the widget's comm channel using an `update` message:
281283

282284
```
283285
{
@@ -298,26 +300,7 @@ Comm messages for state synchronization may contain binary buffers. The optional
298300

299301
See the [Model state](modelstate.md) documentation for the attributes of core Jupyter widgets.
300302

301-
#### Synchronizing from frontend to kernel: `backbone`
302-
303-
When a widget's state changes in the frontend, the changed keys are sent to the kernel over the widget's comm channel using a `backbone` message:
304-
305-
```
306-
{
307-
'comm_id' : 'u-u-i-d',
308-
'data' : {
309-
'method': 'backbone',
310-
'sync_data': { <dictionary of widget state> }
311-
'buffer_keys': [ <optional list of state keys corresponding to binary buffers in the message> ]
312-
}
313-
}
314-
```
315-
316-
The state update is split between values that are serializable with JSON (in the `data.sync_data` dictionary), and binary values (represented in `data.buffer_keys`).
317-
318-
The `data.sync_data` value is a dictionary of widget state keys and values that can be serialized to JSON.
319-
320-
Comm messages for state synchronization may contain binary buffers. The `data.buffer_keys` optional value contains a list of keys corresponding to the binary buffers. For example, if `data.buffer_keys` is `['x', 'y']`, then the first binary buffer is the value of the `'x'` state attribute and the second binary buffer is the value of the `'y'` state attribute.
303+
TODO: update protocol for changes in https://github.com/ipython/ipywidgets/pull/1194
321304

322305
#### State requests: `request_state`
323306

0 commit comments

Comments
 (0)