Skip to content

Commit c0eaa85

Browse files
committed
Add stdin ID, change structure
1 parent e0b747a commit c0eaa85

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

jupyter_ydoc/ystdin.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,39 @@
11
# Copyright (c) Jupyter Development Team.
22
# Distributed under the terms of the Modified BSD License.
33

4+
from uuid import uuid4
5+
46
from pycrdt import Map, Text
57

68

7-
def add_stdin(cell: Map, prompt: str = "", password: bool = False) -> None:
9+
def add_stdin(cell: Map, prompt: str = "", password: bool = False) -> str:
810
"""
9-
Adds an stdin Map in the cell outputs.
11+
Adds an stdin Map in the cell outputs, and returns its ID.
1012
1113
Schema:
1214
1315
.. code-block:: json
1416
1517
{
16-
"state": Map[
17-
"pending": bool,
18-
"password": bool
19-
],
18+
"output_type": "stdin",
19+
"id": str,
20+
"submitted": bool,
21+
"password": bool
2022
"prompt": str,
2123
"input": Text
2224
}
2325
"""
26+
idx = uuid4().hex
2427
stdin = Map(
2528
{
2629
"output_type": "stdin",
27-
"state": {
28-
"pending": True,
29-
"password": password,
30-
},
30+
"id": idx,
31+
"submitted": False,
32+
"password": password,
3133
"prompt": prompt,
3234
"input": Text(),
3335
}
3436
)
3537
outputs = cell.get("outputs")
3638
outputs.append(stdin)
39+
return idx

tests/test_ydocs.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ def test_yblob():
1212
changes = []
1313

1414
def callback(topic, event):
15-
print(topic, event)
1615
changes.append((topic, event))
1716

1817
yblob.observe(callback)
@@ -33,20 +32,22 @@ def test_stdin():
3332
}
3433
)
3534
ycell = ynotebook.ycells[0]
36-
add_stdin(ycell)
35+
add_stdin(ycell, prompt="pwd:", password=True)
36+
stdin = ycell["outputs"][0]["input"]
37+
stdin += "mypassword"
3738
cell = ycell.to_py()
3839
# cell ID is random, ignore that
3940
del cell["id"]
41+
# input ID is random, ignore that
42+
del cell["outputs"][0]["id"]
4043
assert cell == {
4144
"outputs": [
4245
{
4346
"output_type": "stdin",
44-
"input": "",
45-
"prompt": "",
46-
"state": {
47-
"password": False,
48-
"pending": True,
49-
},
47+
"input": "mypassword",
48+
"prompt": "pwd:",
49+
"password": True,
50+
"submitted": False,
5051
}
5152
],
5253
"source": "",

0 commit comments

Comments
 (0)