Skip to content

Commit 74e0922

Browse files
committed
Change notebook code cell stream output schema
1 parent eed983a commit 74e0922

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

javascript/src/ycell.ts

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

764+
createOutputs(outputs: Array<nbformat.IOutput>): Array<any> {
765+
const new_outputs: Array<any> = [];
766+
for (const output of outputs) {
767+
if (output.output_type === 'stream') {
768+
const { text, ...output_without_text } = output;
769+
const _new_output: { [id: string]: any } = output_without_text;
770+
const new_text = new Y.Array();
771+
new_text.push(text as string[]);
772+
_new_output['text'] = new_text;
773+
const new_output = new Y.Map();
774+
for (const [key, value] of Object.entries(_new_output)) {
775+
new_output.set(key, value);
776+
}
777+
new_outputs.push(new_output);
778+
} else {
779+
new_outputs.push(output);
780+
}
781+
}
782+
return new_outputs;
783+
}
784+
764785
/**
765786
* Replace all outputs.
766787
*/
767788
setOutputs(outputs: Array<nbformat.IOutput>): void {
768789
this.transact(() => {
769790
this._youtputs.delete(0, this._youtputs.length);
770-
this._youtputs.insert(0, outputs);
791+
const new_outputs = this.createOutputs(outputs);
792+
this._youtputs.insert(0, new_outputs);
771793
}, false);
772794
}
773795

@@ -789,7 +811,8 @@ export class YCodeCell
789811
end < this._youtputs.length ? end - start : this._youtputs.length - start;
790812
this.transact(() => {
791813
this._youtputs.delete(start, fin);
792-
this._youtputs.insert(start, outputs);
814+
const new_outputs = this.createOutputs(outputs);
815+
this._youtputs.insert(start, new_outputs);
793816
}, false);
794817
}
795818

jupyter_ydoc/ynotebook.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,12 @@ def create_ycell(self, value: Dict[str, Any]) -> Y.YMap:
170170
if "attachments" in cell and not cell["attachments"]:
171171
del cell["attachments"]
172172
elif cell_type == "code":
173-
cell["outputs"] = Y.YArray(cell.get("outputs", []))
173+
outputs = cell.get("outputs", [])
174+
for idx, output in enumerate(outputs):
175+
if output.get("output_type") == "stream":
176+
output["text"] = Y.YArray(output.get("text", []))
177+
outputs[idx] = Y.YMap(output)
178+
cell["outputs"] = Y.YArray(outputs)
174179

175180
return Y.YMap(cell)
176181

0 commit comments

Comments
 (0)