Skip to content

Commit

Permalink
Ignore stdin output when converting to JSON, change structure
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed May 30, 2024
1 parent 04ab4de commit fc65622
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 24 deletions.
5 changes: 4 additions & 1 deletion javascript/src/ycell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,10 @@ export class YCodeCell
* Execution, display, or stream outputs.
*/
getOutputs(): Array<nbformat.IOutput> {
return JSONExt.deepCopy(this._youtputs.toArray());
return JSONExt.deepCopy(this._youtputs.toArray().filter(
// Filter out stdin output.
(el) => !((el instanceof Y.Map) && (el.get('output_type') === 'stdin'))
));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion jupyter_ydoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .yblob import YBlob as YBlob
from .yfile import YFile as YFile
from .ynotebook import YNotebook as YNotebook
from .ystdin import add_stdin as add_stdin
from .ystdin import add_stdin_output as add_stdin_output
from .yunicode import YUnicode as YUnicode

# See compatibility note on `group` keyword in
Expand Down
1 change: 1 addition & 0 deletions jupyter_ydoc/ynotebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def get_cell(self, index: int) -> Dict[str, Any]:
and not cell["attachments"]
):
del cell["attachments"]
# filter out stdin output
outputs = cell.get("outputs", [])
del_outputs = []
for idx, output in enumerate(outputs):
Expand Down
22 changes: 8 additions & 14 deletions jupyter_ydoc/ystdin.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

from uuid import uuid4
from pycrdt import Array, Map, Text

from pycrdt import Map, Text


def add_stdin(cell: Map, prompt: str = "", password: bool = False) -> str:
def add_stdin_output(outputs: Array, prompt: str = "", password: bool = False) -> Map:
"""
Adds an stdin Map in the cell outputs, and returns its ID.
Adds an stdin output Map in the cell outputs, and returns it.
Schema:
.. code-block:: json
{
"output_type": "stdin",
"id": str,
"submitted": bool,
"password": bool
"prompt": str,
"input": Text
"value": Text
}
"""
idx = uuid4().hex
stdin = Map(
stdin_output = Map(
{
"output_type": "stdin",
"id": idx,
"submitted": False,
"password": password,
"prompt": prompt,
"input": Text(),
"value": Text(),
}
)
outputs = cell.get("outputs")
outputs.append(stdin)
return idx
outputs.append(stdin_output)
return stdin_output
17 changes: 9 additions & 8 deletions tests/test_ydocs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

from jupyter_ydoc import YBlob, YNotebook, add_stdin
from jupyter_ydoc import YBlob, YNotebook, add_stdin_output


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


def test_stdin():
def test_stdin_output():
ynotebook = YNotebook()
ynotebook.append_cell(
{
Expand All @@ -32,22 +32,23 @@ def test_stdin():
}
)
ycell = ynotebook.ycells[0]
add_stdin(ycell, prompt="pwd:", password=True)
stdin = ycell["outputs"][0]["input"]
youtputs = ycell["outputs"]
stdin_output = add_stdin_output(youtputs, prompt="pwd:", password=True)
stdin = stdin_output["value"]
stdin += "mypassword"
stdin_output["submitted"] = True

cell = ycell.to_py()
# cell ID is random, ignore that
del cell["id"]
# input ID is random, ignore that
del cell["outputs"][0]["id"]
assert cell == {
"outputs": [
{
"output_type": "stdin",
"input": "mypassword",
"value": "mypassword",
"prompt": "pwd:",
"password": True,
"submitted": False,
"submitted": True,
}
],
"source": "",
Expand Down

0 comments on commit fc65622

Please sign in to comment.