From 7c555322a2833d1297eae0a409cc486fc464a415 Mon Sep 17 00:00:00 2001 From: David Brochart Date: Thu, 30 May 2024 10:48:11 +0200 Subject: [PATCH] Ignore stdin output when converting to JSON, change structure --- javascript/src/ycell.ts | 7 ++++++- jupyter_ydoc/__init__.py | 2 +- jupyter_ydoc/ynotebook.py | 1 + jupyter_ydoc/ystdin.py | 23 +++++++++-------------- tests/test_ydocs.py | 18 ++++++++++-------- 5 files changed, 27 insertions(+), 24 deletions(-) diff --git a/javascript/src/ycell.ts b/javascript/src/ycell.ts index ccafbf8..1dab677 100644 --- a/javascript/src/ycell.ts +++ b/javascript/src/ycell.ts @@ -758,7 +758,12 @@ export class YCodeCell * Execution, display, or stream outputs. */ getOutputs(): Array { - 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') + ) + ); } /** diff --git a/jupyter_ydoc/__init__.py b/jupyter_ydoc/__init__.py index 72304f0..9161b2b 100644 --- a/jupyter_ydoc/__init__.py +++ b/jupyter_ydoc/__init__.py @@ -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 diff --git a/jupyter_ydoc/ynotebook.py b/jupyter_ydoc/ynotebook.py index 3ce4f4f..f20bf7d 100644 --- a/jupyter_ydoc/ynotebook.py +++ b/jupyter_ydoc/ynotebook.py @@ -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): diff --git a/jupyter_ydoc/ystdin.py b/jupyter_ydoc/ystdin.py index 9b451b9..dac96a4 100644 --- a/jupyter_ydoc/ystdin.py +++ b/jupyter_ydoc/ystdin.py @@ -1,14 +1,12 @@ # 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) -> int: """ - Adds an stdin Map in the cell outputs, and returns its ID. + Adds an stdin output Map in the cell outputs, and returns its index. Schema: @@ -16,24 +14,21 @@ def add_stdin(cell: Map, prompt: str = "", password: bool = False) -> str: { "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 + stdin_idx = len(outputs) + outputs.append(stdin_output) + return stdin_idx diff --git a/tests/test_ydocs.py b/tests/test_ydocs.py index 6138e65..7be48fe 100644 --- a/tests/test_ydocs.py +++ b/tests/test_ydocs.py @@ -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(): @@ -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( { @@ -32,22 +32,24 @@ def test_stdin(): } ) ycell = ynotebook.ycells[0] - add_stdin(ycell, prompt="pwd:", password=True) - stdin = ycell["outputs"][0]["input"] + youtputs = ycell["outputs"] + stdin_idx = add_stdin_output(youtputs, prompt="pwd:", password=True) + stdin_output = youtputs[stdin_idx] + 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": "",