diff --git a/javascript/src/ycell.ts b/javascript/src/ycell.ts index ccafbf8..bcf0936 100644 --- a/javascript/src/ycell.ts +++ b/javascript/src/ycell.ts @@ -753,12 +753,38 @@ export class YCodeCell set outputs(v: Array) { this.setOutputs(v); } + get youtputs(): Y.Array { + return this._youtputs; + } /** * Execution, display, or stream outputs. */ getOutputs(): Array { - return JSONExt.deepCopy(this._youtputs.toArray()); + return JSONExt.deepCopy(this._youtputs.toJSON()); + } + + createOutputs(outputs: Array): Array { + const newOutputs: Array = []; + for (const output of outputs) { + let _newOutput: { [id: string]: any }; + const newOutput = new Y.Map(); + if (output.output_type === 'stream') { + // Set the text field as a Y.Array + const { text, ...outputWithoutText } = output; + _newOutput = outputWithoutText; + const newText = new Y.Array(); + newText.push(text as string[]); + _newOutput['text'] = newText; + } else { + _newOutput = output; + } + for (const [key, value] of Object.entries(_newOutput)) { + newOutput.set(key, value); + } + newOutputs.push(newOutput); + } + return newOutputs; } /** @@ -767,7 +793,8 @@ export class YCodeCell setOutputs(outputs: Array): void { this.transact(() => { this._youtputs.delete(0, this._youtputs.length); - this._youtputs.insert(0, outputs); + const newOutputs = this.createOutputs(outputs); + this._youtputs.insert(0, newOutputs); }, false); } @@ -789,7 +816,8 @@ export class YCodeCell end < this._youtputs.length ? end - start : this._youtputs.length - start; this.transact(() => { this._youtputs.delete(start, fin); - this._youtputs.insert(start, outputs); + const newOutputs = this.createOutputs(outputs); + this._youtputs.insert(start, newOutputs); }, false); } diff --git a/jupyter_ydoc/ynotebook.py b/jupyter_ydoc/ynotebook.py index 5166af1..89afdf6 100644 --- a/jupyter_ydoc/ynotebook.py +++ b/jupyter_ydoc/ynotebook.py @@ -157,7 +157,12 @@ def create_ycell(self, value: Dict[str, Any]) -> Map: if "attachments" in cell and not cell["attachments"]: del cell["attachments"] elif cell_type == "code": - cell["outputs"] = Array(cell.get("outputs", [])) + outputs = cell.get("outputs", []) + for idx, output in enumerate(outputs): + if output.get("output_type") == "stream": + output["text"] = Array(output.get("text", [])) + outputs[idx] = Map(output) + cell["outputs"] = Array(outputs) return Map(cell) diff --git a/tests/conftest.py b/tests/conftest.py index 9b89cd3..f4ae9f0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -43,6 +43,10 @@ async def yws_server(request): @pytest.fixture def yjs_client(request): client_id = request.param - p = subprocess.Popen(f"yarn node {here / 'yjs_client_'}{client_id}.js", shell=True) + p = subprocess.Popen(["node", f"{here / 'yjs_client_'}{client_id}.js"]) yield p - p.kill() + p.terminate() + try: + p.wait(timeout=10) + except Exception: + p.kill() diff --git a/tests/files/nb1.ipynb b/tests/files/nb1.ipynb new file mode 100644 index 0000000..4ed7180 --- /dev/null +++ b/tests/files/nb1.ipynb @@ -0,0 +1,43 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "4166c837-41c7-4ada-b86e-fd9a7720a409", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello," + ] + } + ], + "source": [ + "print(\"Hello,\", end=\"\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/tests/test_pycrdt_yjs.py b/tests/test_pycrdt_yjs.py index f928c81..a9f3720 100644 --- a/tests/test_pycrdt_yjs.py +++ b/tests/test_pycrdt_yjs.py @@ -74,6 +74,31 @@ async def test_ypy_yjs_0(yws_server, yjs_client): assert ytest.source == nb +@pytest.mark.asyncio +@pytest.mark.parametrize("yjs_client", "1", indirect=True) +async def test_ypy_yjs_1(yws_server, yjs_client): + ydoc = Doc() + ynotebook = YNotebook(ydoc) + nb = stringify_source(json.loads((files_dir / "nb1.ipynb").read_text())) + ynotebook.source = nb + async with connect("ws://localhost:1234/my-roomname") as websocket, WebsocketProvider( + ydoc, websocket + ): + output_text = ynotebook.ycells[0]["outputs"][0]["text"] + assert output_text.to_py() == ["Hello,"] + event = Event() + + def callback(_event): + event.set() + + output_text.observe(callback) + + with move_on_after(10): + await event.wait() + + assert output_text.to_py() == ["Hello,", " World!"] + + def test_plotly_renderer(): """This test checks in particular that the type cast is not breaking the data.""" ydoc = Doc() diff --git a/tests/yjs_client_1.js b/tests/yjs_client_1.js new file mode 100644 index 0000000..04daa23 --- /dev/null +++ b/tests/yjs_client_1.js @@ -0,0 +1,23 @@ +/* + * Copyright (c) Jupyter Development Team. + * Distributed under the terms of the Modified BSD License. + */ + +import { YNotebook } from '@jupyter/ydoc' +import { WebsocketProvider } from 'y-websocket' +import ws from 'ws' + +const notebook = new YNotebook() + +const wsProvider = new WebsocketProvider( + 'ws://localhost:1234', 'my-roomname', + notebook.ydoc, + { WebSocketPolyfill: ws } +) + +wsProvider.on('sync', (isSynced) => { + const cell = notebook.getCell(0) + const youtput = cell.youtputs.get(0) + const text = youtput.get('text') + text.insert(1, [' World!']) +})