Skip to content

Commit 96ff3f3

Browse files
committed
Concatenate stream and support for clear_output
1 parent 9804ca7 commit 96ff3f3

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

jupyter_server_nbmodel/handlers.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import asyncio
44
import json
5+
import os
56
import sys
67
import traceback
78
import typing as t
@@ -156,15 +157,39 @@ def _output_hook(ycell, outputs, msg) -> None:
156157
if msg_type in ("display_data", "stream", "execute_result", "error"):
157158
# FIXME support for version
158159
output = nbformat.v4.output_from_msg(msg)
159-
get_logger().info("Got an output. %s", output)
160160
outputs.append(output)
161161

162162
if ycell is not None:
163-
# FIXME support for 'stream'
164-
outputs = ycell["outputs"]
165-
with outputs.doc.transaction():
166-
outputs.append(output)
163+
cell_outputs = ycell["outputs"]
164+
if msg_type == "stream":
165+
with cell_outputs.doc.transaction():
166+
text = output["text"]
167+
168+
# FIXME Logic is quite complex at https://github.com/jupyterlab/jupyterlab/blob/7ae2d436fc410b0cff51042a3350ba71f54f4445/packages/outputarea/src/model.ts#L518
169+
if text.endswith((os.linesep, "\n")):
170+
text = text[:-1]
171+
172+
if (not cell_outputs) or (cell_outputs[-1]["name"] != output["name"]):
173+
output["text"] = [text]
174+
cell_outputs.append(output)
175+
else:
176+
last_output = cell_outputs[-1]
177+
last_output["text"].append(text)
178+
cell_outputs[-1] = last_output
179+
else:
180+
with cell_outputs.doc.transaction():
181+
cell_outputs.append(output)
182+
183+
elif msg_type == "clear_output":
184+
# FIXME msg.content.wait - if true should clear at the next message
185+
outputs.clear()
167186

187+
if ycell is not None:
188+
del ycell["outputs"][:]
189+
190+
elif msg_type == "update_display_data":
191+
# FIXME
192+
...
168193

169194
def _stdin_hook(msg) -> None:
170195
get_logger().info("Code snippet execution is waiting for an input.")

0 commit comments

Comments
 (0)