Skip to content

Commit 2d88880

Browse files
feat: implement Output widget that mimics a frontend
This is a port of voila-dashboards/voila#91 and subsequent fixes.
1 parent 6510bd9 commit 2d88880

File tree

4 files changed

+1117
-1
lines changed

4 files changed

+1117
-1
lines changed

nbclient/client.py

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import datetime
21
import base64
2+
import collections
3+
import datetime
34
from textwrap import dedent
45

56
from async_generator import asynccontextmanager
@@ -22,6 +23,7 @@
2223
CellExecutionError
2324
)
2425
from .util import run_sync, ensure_async
26+
from .output_widget import OutputWidget
2527

2628

2729
def timestamp():
@@ -307,6 +309,11 @@ def reset_execution_trackers(self):
307309
self._display_id_map = {}
308310
self.widget_state = {}
309311
self.widget_buffers = {}
312+
# maps to list of hooks, where the last is used, this is used
313+
# to support nested use of output widgets.
314+
self.output_hook_stack = collections.defaultdict(list)
315+
# our front-end mimicing Output widgets
316+
self.output_widget_objects = {}
310317

311318
def start_kernel_manager(self):
312319
"""Creates a new kernel manager.
@@ -787,6 +794,14 @@ def process_message(self, msg, cell, cell_index):
787794
def output(self, outs, msg, display_id, cell_index):
788795
msg_type = msg['msg_type']
789796

797+
parent_msg_id = msg['parent_header'].get('msg_id')
798+
if self.output_hook_stack[parent_msg_id]:
799+
# if we have a hook registered, it will overrride our
800+
# default output behaviour (e.g. OutputWidget)
801+
hook = self.output_hook_stack[parent_msg_id][-1]
802+
hook.output(outs, msg, display_id, cell_index)
803+
return
804+
790805
try:
791806
out = output_from_msg(msg)
792807
except ValueError:
@@ -812,6 +827,15 @@ def output(self, outs, msg, display_id, cell_index):
812827

813828
def clear_output(self, outs, msg, cell_index):
814829
content = msg['content']
830+
831+
parent_msg_id = msg['parent_header'].get('msg_id')
832+
if self.output_hook_stack[parent_msg_id]:
833+
# if we have a hook registered, it will overrride our
834+
# default clear_output behaviour (e.g. OutputWidget)
835+
hook = self.output_hook_stack[parent_msg_id][-1]
836+
hook.clear_output(outs, msg, cell_index)
837+
return
838+
815839
if content.get('wait'):
816840
self.log.debug('Wait to clear output')
817841
self.clear_before_next_output = True
@@ -832,6 +856,24 @@ def handle_comm_msg(self, outs, msg, cell_index):
832856
self.widget_state.setdefault(content['comm_id'], {}).update(data['state'])
833857
if 'buffer_paths' in data and data['buffer_paths']:
834858
self.widget_buffers[content['comm_id']] = self._get_buffer_data(msg)
859+
# There are cases where we need to mimic a frontend, to get similar behaviour as
860+
# when using the Output widget from Jupyter lab/notebook
861+
if msg['msg_type'] == 'comm_open' and msg['content'].get('target_name') == 'jupyter.widget':
862+
content = msg['content']
863+
data = content['data']
864+
state = data['state']
865+
comm_id = msg['content']['comm_id']
866+
if state['_model_module'] == '@jupyter-widgets/output' and\
867+
state['_model_name'] == 'OutputModel':
868+
self.output_widget_objects[comm_id] = OutputWidget(comm_id, state, self.kc, self)
869+
elif msg['msg_type'] == 'comm_msg':
870+
content = msg['content']
871+
data = content['data']
872+
if 'state' in data:
873+
state = data['state']
874+
comm_id = msg['content']['comm_id']
875+
if comm_id in self.output_widget_objects:
876+
self.output_widget_objects[comm_id].set_state(state)
835877

836878
def _serialize_widget_state(self, state):
837879
"""Serialize a widget state, following format in @jupyter-widgets/schema."""
@@ -856,6 +898,22 @@ def _get_buffer_data(self, msg):
856898
)
857899
return encoded_buffers
858900

901+
def register_output_hook(self, msg_id, hook):
902+
"""Registers an override object that handles output/clear_output instead.
903+
904+
Multiple hooks can be registered, where the last one will be used (stack based)
905+
"""
906+
# mimics
907+
# https://jupyterlab.github.io/jupyterlab/services/interfaces/kernel.ikernelconnection.html#registermessagehook
908+
self.output_hook_stack[msg_id].append(hook)
909+
910+
def remove_output_hook(self, msg_id, hook):
911+
"""Unregisters an override object that handles output/clear_output instead"""
912+
# mimics
913+
# https://jupyterlab.github.io/jupyterlab/services/interfaces/kernel.ikernelconnection.html#removemessagehook
914+
removed_hook = self.output_hook_stack[msg_id].pop()
915+
assert removed_hook == hook
916+
859917

860918
def execute(nb, cwd=None, km=None, **kwargs):
861919
"""Execute a notebook's code, updating outputs within the notebook object.

nbclient/jsonutil.py

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
"""Utilities to manipulate JSON objects."""
2+
3+
# NOTE: this is a copy of ipykernel/jsonutils.py (+blackified)
4+
5+
# Copyright (c) IPython Development Team.
6+
# Distributed under the terms of the Modified BSD License.
7+
8+
from binascii import b2a_base64
9+
import math
10+
import re
11+
import types
12+
from datetime import datetime
13+
import numbers
14+
15+
16+
from ipython_genutils import py3compat
17+
from ipython_genutils.py3compat import unicode_type, iteritems
18+
19+
next_attr_name = '__next__' if py3compat.PY3 else 'next'
20+
21+
# -----------------------------------------------------------------------------
22+
# Globals and constants
23+
# -----------------------------------------------------------------------------
24+
25+
# timestamp formats
26+
ISO8601 = "%Y-%m-%dT%H:%M:%S.%f"
27+
ISO8601_PAT = re.compile(
28+
r"^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(\.\d{1,6})?Z?([\+\-]\d{2}:?\d{2})?$"
29+
)
30+
31+
# holy crap, strptime is not threadsafe.
32+
# Calling it once at import seems to help.
33+
datetime.strptime("1", "%d")
34+
35+
# -----------------------------------------------------------------------------
36+
# Classes and functions
37+
# -----------------------------------------------------------------------------
38+
39+
40+
# constants for identifying png/jpeg data
41+
PNG = b'\x89PNG\r\n\x1a\n'
42+
# front of PNG base64-encoded
43+
PNG64 = b'iVBORw0KG'
44+
JPEG = b'\xff\xd8'
45+
# front of JPEG base64-encoded
46+
JPEG64 = b'/9'
47+
# constants for identifying gif data
48+
GIF_64 = b'R0lGODdh'
49+
GIF89_64 = b'R0lGODlh'
50+
# front of PDF base64-encoded
51+
PDF64 = b'JVBER'
52+
53+
54+
def encode_images(format_dict):
55+
"""b64-encodes images in a displaypub format dict
56+
57+
Perhaps this should be handled in json_clean itself?
58+
59+
Parameters
60+
----------
61+
62+
format_dict : dict
63+
A dictionary of display data keyed by mime-type
64+
65+
Returns
66+
-------
67+
68+
format_dict : dict
69+
A copy of the same dictionary,
70+
but binary image data ('image/png', 'image/jpeg' or 'application/pdf')
71+
is base64-encoded.
72+
73+
"""
74+
75+
# no need for handling of ambiguous bytestrings on Python 3,
76+
# where bytes objects always represent binary data and thus
77+
# base64-encoded.
78+
if py3compat.PY3:
79+
return format_dict
80+
81+
encoded = format_dict.copy()
82+
83+
pngdata = format_dict.get('image/png')
84+
if isinstance(pngdata, bytes):
85+
# make sure we don't double-encode
86+
if not pngdata.startswith(PNG64):
87+
pngdata = b2a_base64(pngdata)
88+
encoded['image/png'] = pngdata.decode('ascii')
89+
90+
jpegdata = format_dict.get('image/jpeg')
91+
if isinstance(jpegdata, bytes):
92+
# make sure we don't double-encode
93+
if not jpegdata.startswith(JPEG64):
94+
jpegdata = b2a_base64(jpegdata)
95+
encoded['image/jpeg'] = jpegdata.decode('ascii')
96+
97+
gifdata = format_dict.get('image/gif')
98+
if isinstance(gifdata, bytes):
99+
# make sure we don't double-encode
100+
if not gifdata.startswith((GIF_64, GIF89_64)):
101+
gifdata = b2a_base64(gifdata)
102+
encoded['image/gif'] = gifdata.decode('ascii')
103+
104+
pdfdata = format_dict.get('application/pdf')
105+
if isinstance(pdfdata, bytes):
106+
# make sure we don't double-encode
107+
if not pdfdata.startswith(PDF64):
108+
pdfdata = b2a_base64(pdfdata)
109+
encoded['application/pdf'] = pdfdata.decode('ascii')
110+
111+
return encoded
112+
113+
114+
def json_clean(obj):
115+
"""Clean an object to ensure it's safe to encode in JSON.
116+
117+
Atomic, immutable objects are returned unmodified. Sets and tuples are
118+
converted to lists, lists are copied and dicts are also copied.
119+
120+
Note: dicts whose keys could cause collisions upon encoding (such as a dict
121+
with both the number 1 and the string '1' as keys) will cause a ValueError
122+
to be raised.
123+
124+
Parameters
125+
----------
126+
obj : any python object
127+
128+
Returns
129+
-------
130+
out : object
131+
132+
A version of the input which will not cause an encoding error when
133+
encoded as JSON. Note that this function does not *encode* its inputs,
134+
it simply sanitizes it so that there will be no encoding errors later.
135+
136+
"""
137+
# types that are 'atomic' and ok in json as-is.
138+
atomic_ok = (unicode_type, type(None))
139+
140+
# containers that we need to convert into lists
141+
container_to_list = (tuple, set, types.GeneratorType)
142+
143+
# Since bools are a subtype of Integrals, which are a subtype of Reals,
144+
# we have to check them in that order.
145+
146+
if isinstance(obj, bool):
147+
return obj
148+
149+
if isinstance(obj, numbers.Integral):
150+
# cast int to int, in case subclasses override __str__ (e.g. boost enum, #4598)
151+
return int(obj)
152+
153+
if isinstance(obj, numbers.Real):
154+
# cast out-of-range floats to their reprs
155+
if math.isnan(obj) or math.isinf(obj):
156+
return repr(obj)
157+
return float(obj)
158+
159+
if isinstance(obj, atomic_ok):
160+
return obj
161+
162+
if isinstance(obj, bytes):
163+
if py3compat.PY3:
164+
# unanmbiguous binary data is base64-encoded
165+
# (this probably should have happened upstream)
166+
return b2a_base64(obj).decode('ascii')
167+
else:
168+
# Python 2 bytestr is ambiguous,
169+
# needs special handling for possible binary bytestrings.
170+
# imperfect workaround: if ascii, assume text.
171+
# otherwise assume binary, base64-encode (py3 behavior).
172+
try:
173+
return obj.decode('ascii')
174+
except UnicodeDecodeError:
175+
return b2a_base64(obj).decode('ascii')
176+
177+
if isinstance(obj, container_to_list) or (
178+
hasattr(obj, '__iter__') and hasattr(obj, next_attr_name)
179+
):
180+
obj = list(obj)
181+
182+
if isinstance(obj, list):
183+
return [json_clean(x) for x in obj]
184+
185+
if isinstance(obj, dict):
186+
# First, validate that the dict won't lose data in conversion due to
187+
# key collisions after stringification. This can happen with keys like
188+
# True and 'true' or 1 and '1', which collide in JSON.
189+
nkeys = len(obj)
190+
nkeys_collapsed = len(set(map(unicode_type, obj)))
191+
if nkeys != nkeys_collapsed:
192+
raise ValueError(
193+
'dict cannot be safely converted to JSON: '
194+
'key collision would lead to dropped values'
195+
)
196+
# If all OK, proceed by making the new dict that will be json-safe
197+
out = {}
198+
for k, v in iteritems(obj):
199+
out[unicode_type(k)] = json_clean(v)
200+
return out
201+
if isinstance(obj, datetime):
202+
return obj.strftime(ISO8601)
203+
204+
# we don't understand it, it's probably an unserializable object
205+
raise ValueError("Can't clean for JSON: %r" % obj)

nbclient/output_widget.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
from .jsonutil import json_clean
2+
from nbformat.v4 import output_from_msg
3+
4+
5+
class OutputWidget:
6+
"""This class mimics a front end output widget"""
7+
def __init__(self, comm_id, state, kernel_client, executor):
8+
self.comm_id = comm_id
9+
self.state = state
10+
self.kernel_client = kernel_client
11+
self.executor = executor
12+
self.topic = ('comm-%s' % self.comm_id).encode('ascii')
13+
self.outputs = self.state['outputs']
14+
self.clear_before_next_output = False
15+
16+
def clear_output(self, outs, msg, cell_index):
17+
self.parent_header = msg['parent_header']
18+
content = msg['content']
19+
if content.get('wait'):
20+
self.clear_before_next_output = True
21+
else:
22+
self.outputs = []
23+
# sync back the state to the kernel
24+
self.sync_state()
25+
if hasattr(self.executor, 'widget_state'):
26+
# sync the state to the nbconvert state as well, since that is used for testing
27+
self.executor.widget_state[self.comm_id]['outputs'] = self.outputs
28+
29+
def sync_state(self):
30+
state = {'outputs': self.outputs}
31+
msg = {'method': 'update', 'state': state, 'buffer_paths': []}
32+
self.send(msg)
33+
34+
def _publish_msg(self, msg_type, data=None, metadata=None, buffers=None, **keys):
35+
"""Helper for sending a comm message on IOPub"""
36+
data = {} if data is None else data
37+
metadata = {} if metadata is None else metadata
38+
content = json_clean(dict(data=data, comm_id=self.comm_id, **keys))
39+
msg = self.kernel_client.session.msg(msg_type, content=content, parent=self.parent_header,
40+
metadata=metadata)
41+
self.kernel_client.shell_channel.send(msg)
42+
43+
def send(self, data=None, metadata=None, buffers=None):
44+
self._publish_msg('comm_msg', data=data, metadata=metadata, buffers=buffers)
45+
46+
def output(self, outs, msg, display_id, cell_index):
47+
if self.clear_before_next_output:
48+
self.outputs = []
49+
self.clear_before_next_output = False
50+
self.parent_header = msg['parent_header']
51+
output = output_from_msg(msg)
52+
53+
if self.outputs:
54+
# try to coalesce/merge output text
55+
last_output = self.outputs[-1]
56+
if (last_output['output_type'] == 'stream'
57+
and output['output_type'] == 'stream'
58+
and last_output['name'] == output['name']):
59+
last_output['text'] += output['text']
60+
else:
61+
self.outputs.append(output)
62+
else:
63+
self.outputs.append(output)
64+
self.sync_state()
65+
if hasattr(self.executor, 'widget_state'):
66+
# sync the state to the nbconvert state as well, since that is used for testing
67+
self.executor.widget_state[self.comm_id]['outputs'] = self.outputs
68+
69+
def set_state(self, state):
70+
if 'msg_id' in state:
71+
msg_id = state.get('msg_id')
72+
if msg_id:
73+
self.executor.register_output_hook(msg_id, self)
74+
self.msg_id = msg_id
75+
else:
76+
self.executor.remove_output_hook(self.msg_id, self)
77+
self.msg_id = msg_id

0 commit comments

Comments
 (0)