Skip to content

Commit

Permalink
Support stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed May 16, 2024
1 parent a3c01bc commit d9d7e32
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions jupyter_ydoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .yblob import YBlob as YBlob
from .yfile import YFile as YFile
from .ynotebook import YNotebook as YNotebook
from .ystdin import add_stdin as add_stdin
from .yunicode import YUnicode as YUnicode

# See compatibility note on `group` keyword in
Expand Down
36 changes: 36 additions & 0 deletions jupyter_ydoc/ystdin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

from pycrdt import Map, Text


def add_stdin(cell: Map, prompt: str = "", password: bool = False) -> None:
"""
Adds an stdin Map in the cell outputs.
Schema:
.. code-block:: json
{
"state": Map[
"pending": bool,
"password": bool
],
"prompt": str,
"input": Text
}
"""
stdin = Map(
{
"output_type": "stdin",
"state": {
"pending": True,
"password": password,
},
"prompt": prompt,
"input": Text(),
}
)
outputs = cell.get("outputs")
outputs.append(stdin)
33 changes: 32 additions & 1 deletion tests/test_ydocs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

from jupyter_ydoc import YBlob
from jupyter_ydoc import YBlob, YNotebook, add_stdin


def test_yblob():
Expand All @@ -22,3 +22,34 @@ def callback(topic, event):
assert topic == "source"
assert event.keys["bytes"]["oldValue"] == b"012"
assert event.keys["bytes"]["newValue"] == b"345"


def test_stdin():
ynotebook = YNotebook()
ynotebook.append_cell(
{
"cell_type": "code",
"source": "",
}
)
ycell = ynotebook.ycells[0]
add_stdin(ycell)
cell = ycell.to_py()
# cell ID is random, ignore that
del cell["id"]
assert cell == {
"outputs": [
{
"output_type": "stdin",
"input": "",
"prompt": "",
"state": {
"password": False,
"pending": True,
},
}
],
"source": "",
"metadata": {},
"cell_type": "code",
}

0 comments on commit d9d7e32

Please sign in to comment.