Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support stdin output #233

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion javascript/src/ycell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,12 @@ export class YCodeCell
* Execution, display, or stream outputs.
*/
getOutputs(): Array<nbformat.IOutput> {
return JSONExt.deepCopy(this._youtputs.toArray());
return JSONExt.deepCopy(
this._youtputs.toArray().filter(
// Filter out stdin output.
el => !(el instanceof Y.Map && el.get('output_type') === 'stdin')
)
);
}

/**
Expand Down
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_output as add_stdin_output
from .yunicode import YUnicode as YUnicode

# See compatibility note on `group` keyword in
Expand Down
10 changes: 10 additions & 0 deletions jupyter_ydoc/ynotebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ def get_cell(self, index: int) -> Dict[str, Any]:
and not cell["attachments"]
):
del cell["attachments"]
# filter out stdin output
outputs = cell.get("outputs", [])
del_outputs = []
for idx, output in enumerate(outputs):
if output["output_type"] == "stdin":
del_outputs.append(idx)
deleted = 0
for idx in del_outputs:
del outputs[idx - deleted]
deleted += 1
return cell

def append_cell(self, value: Dict[str, Any]) -> None:
Expand Down
34 changes: 34 additions & 0 deletions jupyter_ydoc/ystdin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

from pycrdt import Array, Map, Text


def add_stdin_output(outputs: Array, prompt: str = "", password: bool = False) -> int:
"""
Adds an stdin output Map in the cell outputs, and returns its index.

Schema:

.. code-block:: json

{
"output_type": "stdin",
"submitted": bool,
"password": bool
"prompt": str,
"value": Text
}
"""
stdin_output = Map(
{
"output_type": "stdin",
"submitted": False,
"password": password,
"prompt": prompt,
"value": Text(),
}
)
stdin_idx = len(outputs)
outputs.append(stdin_output)
return stdin_idx
38 changes: 36 additions & 2 deletions 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_output


def test_yblob():
Expand All @@ -12,7 +12,6 @@ def test_yblob():
changes = []

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

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


def test_stdin_output():
ynotebook = YNotebook()
ynotebook.append_cell(
{
"cell_type": "code",
"source": "",
}
)
ycell = ynotebook.ycells[0]
youtputs = ycell["outputs"]
stdin_idx = add_stdin_output(youtputs, prompt="pwd:", password=True)
stdin_output = youtputs[stdin_idx]
stdin = stdin_output["value"]
stdin += "mypassword"
stdin_output["submitted"] = True

cell = ycell.to_py()
# cell ID is random, ignore that
del cell["id"]
assert cell == {
"outputs": [
{
"output_type": "stdin",
"value": "mypassword",
"prompt": "pwd:",
"password": True,
"submitted": True,
}
],
"source": "",
"metadata": {},
"cell_type": "code",
}
Loading