Skip to content

Commit 90145e6

Browse files
committed
Ignore stdin output when converting to JSON, change structure
1 parent 04ab4de commit 90145e6

File tree

5 files changed

+25
-24
lines changed

5 files changed

+25
-24
lines changed

javascript/src/ycell.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,12 @@ export class YCodeCell
758758
* Execution, display, or stream outputs.
759759
*/
760760
getOutputs(): Array<nbformat.IOutput> {
761-
return JSONExt.deepCopy(this._youtputs.toArray());
761+
return JSONExt.deepCopy(
762+
this._youtputs.toArray().filter(
763+
// Filter out stdin output.
764+
el => !(el instanceof Y.Map && el.get('output_type') === 'stdin')
765+
)
766+
);
762767
}
763768

764769
/**

jupyter_ydoc/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from .yblob import YBlob as YBlob
88
from .yfile import YFile as YFile
99
from .ynotebook import YNotebook as YNotebook
10-
from .ystdin import add_stdin as add_stdin
10+
from .ystdin import add_stdin_output as add_stdin_output
1111
from .yunicode import YUnicode as YUnicode
1212

1313
# See compatibility note on `group` keyword in

jupyter_ydoc/ynotebook.py

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ def get_cell(self, index: int) -> Dict[str, Any]:
109109
and not cell["attachments"]
110110
):
111111
del cell["attachments"]
112+
# filter out stdin output
112113
outputs = cell.get("outputs", [])
113114
del_outputs = []
114115
for idx, output in enumerate(outputs):

jupyter_ydoc/ystdin.py

+8-14
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,33 @@
11
# Copyright (c) Jupyter Development Team.
22
# Distributed under the terms of the Modified BSD License.
33

4-
from uuid import uuid4
4+
from pycrdt import Array, Map, Text
55

6-
from pycrdt import Map, Text
76

8-
9-
def add_stdin(cell: Map, prompt: str = "", password: bool = False) -> str:
7+
def add_stdin_output(outputs: Array, prompt: str = "", password: bool = False) -> Map:
108
"""
11-
Adds an stdin Map in the cell outputs, and returns its ID.
9+
Adds an stdin output Map in the cell outputs, and returns it.
1210
1311
Schema:
1412
1513
.. code-block:: json
1614
1715
{
1816
"output_type": "stdin",
19-
"id": str,
2017
"submitted": bool,
2118
"password": bool
2219
"prompt": str,
23-
"input": Text
20+
"value": Text
2421
}
2522
"""
26-
idx = uuid4().hex
27-
stdin = Map(
23+
stdin_output = Map(
2824
{
2925
"output_type": "stdin",
30-
"id": idx,
3126
"submitted": False,
3227
"password": password,
3328
"prompt": prompt,
34-
"input": Text(),
29+
"value": Text(),
3530
}
3631
)
37-
outputs = cell.get("outputs")
38-
outputs.append(stdin)
39-
return idx
32+
outputs.append(stdin_output)
33+
return stdin_output

tests/test_ydocs.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (c) Jupyter Development Team.
22
# Distributed under the terms of the Modified BSD License.
33

4-
from jupyter_ydoc import YBlob, YNotebook, add_stdin
4+
from jupyter_ydoc import YBlob, YNotebook, add_stdin_output
55

66

77
def test_yblob():
@@ -23,7 +23,7 @@ def callback(topic, event):
2323
assert event.keys["bytes"]["newValue"] == b"345"
2424

2525

26-
def test_stdin():
26+
def test_stdin_output():
2727
ynotebook = YNotebook()
2828
ynotebook.append_cell(
2929
{
@@ -32,22 +32,23 @@ def test_stdin():
3232
}
3333
)
3434
ycell = ynotebook.ycells[0]
35-
add_stdin(ycell, prompt="pwd:", password=True)
36-
stdin = ycell["outputs"][0]["input"]
35+
youtputs = ycell["outputs"]
36+
stdin_output = add_stdin_output(youtputs, prompt="pwd:", password=True)
37+
stdin = stdin_output["value"]
3738
stdin += "mypassword"
39+
stdin_output["submitted"] = True
40+
3841
cell = ycell.to_py()
39-
# cell ID is random, ignore that
40-
del cell["id"]
4142
# input ID is random, ignore that
4243
del cell["outputs"][0]["id"]
4344
assert cell == {
4445
"outputs": [
4546
{
4647
"output_type": "stdin",
47-
"input": "mypassword",
48+
"value": "mypassword",
4849
"prompt": "pwd:",
4950
"password": True,
50-
"submitted": False,
51+
"submitted": True,
5152
}
5253
],
5354
"source": "",

0 commit comments

Comments
 (0)