Skip to content

Commit fd7d841

Browse files
committed
Support stdin
1 parent a3c01bc commit fd7d841

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

jupyter_ydoc/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from .yblob import YBlob as YBlob
88
from .yfile import YFile as YFile
99
from .ynotebook import YNotebook as YNotebook
10+
from .ystdin import add_stdin as add_stdin
1011
from .yunicode import YUnicode as YUnicode
1112

1213
# See compatibility note on `group` keyword in

jupyter_ydoc/ystdin.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from pycrdt import Map, Text
2+
3+
4+
def add_stdin(cell: Map, prompt: str = "", password: bool = False) -> None:
5+
"""
6+
Adds an stdin Map in the cell outputs.
7+
8+
Schema:
9+
10+
.. code-block:: json
11+
12+
{
13+
"state": Map[
14+
"pending": bool,
15+
"password": bool
16+
],
17+
"prompt": str,
18+
"input": Text
19+
}
20+
"""
21+
stdin = Map(
22+
{
23+
"output_type": "stdin",
24+
"state": {
25+
"pending": True,
26+
"password": password,
27+
},
28+
"prompt": prompt,
29+
"input": Text(),
30+
}
31+
)
32+
outputs = cell.get("outputs")
33+
outputs.append(stdin)

tests/test_ydocs.py

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (c) Jupyter Development Team.
22
# Distributed under the terms of the Modified BSD License.
33

4-
from jupyter_ydoc import YBlob
4+
from jupyter_ydoc import YBlob, YNotebook, add_stdin
55

66

77
def test_yblob():
@@ -22,3 +22,34 @@ def callback(topic, event):
2222
assert topic == "source"
2323
assert event.keys["bytes"]["oldValue"] == b"012"
2424
assert event.keys["bytes"]["newValue"] == b"345"
25+
26+
27+
def test_stdin():
28+
ynotebook = YNotebook()
29+
ynotebook.append_cell(
30+
{
31+
"cell_type": "code",
32+
"source": "",
33+
}
34+
)
35+
ycell = ynotebook.ycells[0]
36+
add_stdin(ycell)
37+
cell = ycell.to_py()
38+
# cell ID is random, ignore that
39+
del cell["id"]
40+
assert cell == {
41+
"outputs": [
42+
{
43+
"output_type": "stdin",
44+
"input": "",
45+
"prompt": "",
46+
"state": {
47+
"password": False,
48+
"pending": True,
49+
},
50+
}
51+
],
52+
"source": "",
53+
"metadata": {},
54+
"cell_type": "code",
55+
}

0 commit comments

Comments
 (0)