Skip to content

Commit

Permalink
Add stdin ID, change structure
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed May 17, 2024
1 parent e0b747a commit c0eaa85
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
23 changes: 13 additions & 10 deletions jupyter_ydoc/ystdin.py
Original file line number Diff line number Diff line change
@@ -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
17 changes: 9 additions & 8 deletions tests/test_ydocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def test_yblob():
changes = []

def callback(topic, event):
print(topic, event)
changes.append((topic, event))

yblob.observe(callback)
Expand All @@ -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": "",
Expand Down

0 comments on commit c0eaa85

Please sign in to comment.