Skip to content

Commit 19d36f9

Browse files
committed
Support stdin
1 parent a3c01bc commit 19d36f9

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

jupyter_ydoc/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55

66
from ._version import __version__ as __version__
7+
from .ystdin import add_stdin as add_stdin
78
from .yblob import YBlob as YBlob
89
from .yfile import YFile as YFile
910
from .ynotebook import YNotebook as YNotebook

jupyter_ydoc/ystdin.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
"output_type": "stdin",
23+
"state": {
24+
"pending": True,
25+
"password": password,
26+
},
27+
"prompt": prompt,
28+
"input": Text(),
29+
})
30+
outputs = cell.get("outputs")
31+
outputs.append(stdin)

tests/test_ydocs.py

Lines changed: 30 additions & 1 deletion
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,32 @@ 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+
"cell_type": "code",
31+
"source": "",
32+
})
33+
ycell = ynotebook.ycells[0]
34+
add_stdin(ycell)
35+
cell = ycell.to_py()
36+
# cell ID is random, ignore that
37+
del cell["id"]
38+
assert cell == {
39+
"outputs": [
40+
{
41+
"output_type": "stdin",
42+
"input": "",
43+
"prompt": "",
44+
"state": {
45+
"password": False,
46+
"pending": True,
47+
}
48+
}
49+
],
50+
"source": "",
51+
"metadata": {},
52+
"cell_type": "code",
53+
}

0 commit comments

Comments
 (0)