diff --git a/jupyter_ydoc/ystdin.py b/jupyter_ydoc/ystdin.py index 514dc7a..9b451b9 100644 --- a/jupyter_ydoc/ystdin.py +++ b/jupyter_ydoc/ystdin.py @@ -1,36 +1,39 @@ # Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. +from uuid import uuid4 + from pycrdt import Map, Text -def add_stdin(cell: Map, prompt: str = "", password: bool = False) -> None: +def add_stdin(cell: Map, prompt: str = "", password: bool = False) -> str: """ - Adds an stdin Map in the cell outputs. + Adds an stdin Map in the cell outputs, and returns its ID. Schema: .. code-block:: json { - "state": Map[ - "pending": bool, - "password": bool - ], + "output_type": "stdin", + "id": str, + "submitted": bool, + "password": bool "prompt": str, "input": Text } """ + idx = uuid4().hex stdin = Map( { "output_type": "stdin", - "state": { - "pending": True, - "password": password, - }, + "id": idx, + "submitted": False, + "password": password, "prompt": prompt, "input": Text(), } ) outputs = cell.get("outputs") outputs.append(stdin) + return idx diff --git a/tests/test_ydocs.py b/tests/test_ydocs.py index e003dc5..6138e65 100644 --- a/tests/test_ydocs.py +++ b/tests/test_ydocs.py @@ -12,7 +12,6 @@ def test_yblob(): changes = [] def callback(topic, event): - print(topic, event) changes.append((topic, event)) yblob.observe(callback) @@ -33,20 +32,22 @@ def test_stdin(): } ) ycell = ynotebook.ycells[0] - add_stdin(ycell) + add_stdin(ycell, prompt="pwd:", password=True) + stdin = ycell["outputs"][0]["input"] + stdin += "mypassword" 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": "", - "prompt": "", - "state": { - "password": False, - "pending": True, - }, + "input": "mypassword", + "prompt": "pwd:", + "password": True, + "submitted": False, } ], "source": "",