Skip to content

Commit a2bb094

Browse files
committed
Change notebook code cell stream output schema
1 parent 1f4b08a commit a2bb094

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

javascript/src/ycell.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -761,13 +761,37 @@ export class YCodeCell
761761
return JSONExt.deepCopy(this._youtputs.toArray());
762762
}
763763

764+
createOutputs(outputs: Array<nbformat.IOutput>): Array<any> {
765+
const newOutputs: Array<any> = [];
766+
for (const output of outputs) {
767+
let _newOutput: { [id: string]: any };
768+
const newOutput = new Y.Map();
769+
if (output.output_type === 'stream') {
770+
// Set the text field as a Y.Array
771+
const { text, ...outputWithoutText } = output;
772+
_newOutput = outputWithoutText;
773+
const newText = new Y.Array();
774+
newText.push(text as string[]);
775+
_newOutput['text'] = newText;
776+
} else {
777+
_newOutput = output;
778+
}
779+
for (const [key, value] of Object.entries(_newOutput)) {
780+
newOutput.set(key, value);
781+
}
782+
newOutputs.push(newOutput);
783+
}
784+
return newOutputs;
785+
}
786+
764787
/**
765788
* Replace all outputs.
766789
*/
767790
setOutputs(outputs: Array<nbformat.IOutput>): void {
768791
this.transact(() => {
769792
this._youtputs.delete(0, this._youtputs.length);
770-
this._youtputs.insert(0, outputs);
793+
const newOutputs = this.createOutputs(outputs);
794+
this._youtputs.insert(0, newOutputs);
771795
}, false);
772796
}
773797

@@ -789,7 +813,8 @@ export class YCodeCell
789813
end < this._youtputs.length ? end - start : this._youtputs.length - start;
790814
this.transact(() => {
791815
this._youtputs.delete(start, fin);
792-
this._youtputs.insert(start, outputs);
816+
const newOutputs = this.createOutputs(outputs);
817+
this._youtputs.insert(start, newOutputs);
793818
}, false);
794819
}
795820

jupyter_ydoc/ynotebook.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,12 @@ def create_ycell(self, value: Dict[str, Any]) -> Map:
157157
if "attachments" in cell and not cell["attachments"]:
158158
del cell["attachments"]
159159
elif cell_type == "code":
160-
cell["outputs"] = Array(cell.get("outputs", []))
160+
outputs = cell.get("outputs", [])
161+
for idx, output in enumerate(outputs):
162+
if output.get("output_type") == "stream":
163+
output["text"] = Array(output.get("text", []))
164+
outputs[idx] = Map(output)
165+
cell["outputs"] = Array(outputs)
161166

162167
return Map(cell)
163168

0 commit comments

Comments
 (0)