Skip to content

Commit

Permalink
Change notebook code cell stream output schema
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Nov 27, 2023
1 parent eed983a commit 74e0922
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
27 changes: 25 additions & 2 deletions javascript/src/ycell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -761,13 +761,35 @@ export class YCodeCell
return JSONExt.deepCopy(this._youtputs.toArray());
}

createOutputs(outputs: Array<nbformat.IOutput>): Array<any> {
const new_outputs: Array<any> = [];
for (const output of outputs) {
if (output.output_type === 'stream') {
const { text, ...output_without_text } = output;
const _new_output: { [id: string]: any } = output_without_text;
const new_text = new Y.Array();
new_text.push(text as string[]);
_new_output['text'] = new_text;
const new_output = new Y.Map();
for (const [key, value] of Object.entries(_new_output)) {
new_output.set(key, value);
}
new_outputs.push(new_output);
} else {
new_outputs.push(output);
}
}
return new_outputs;
}

/**
* Replace all outputs.
*/
setOutputs(outputs: Array<nbformat.IOutput>): void {
this.transact(() => {
this._youtputs.delete(0, this._youtputs.length);
this._youtputs.insert(0, outputs);
const new_outputs = this.createOutputs(outputs);
this._youtputs.insert(0, new_outputs);
}, false);
}

Expand All @@ -789,7 +811,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 new_outputs = this.createOutputs(outputs);
this._youtputs.insert(start, new_outputs);
}, false);
}

Expand Down
7 changes: 6 additions & 1 deletion jupyter_ydoc/ynotebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,12 @@ def create_ycell(self, value: Dict[str, Any]) -> Y.YMap:
if "attachments" in cell and not cell["attachments"]:
del cell["attachments"]
elif cell_type == "code":
cell["outputs"] = Y.YArray(cell.get("outputs", []))
outputs = cell.get("outputs", [])
for idx, output in enumerate(outputs):
if output.get("output_type") == "stream":
output["text"] = Y.YArray(output.get("text", []))
outputs[idx] = Y.YMap(output)
cell["outputs"] = Y.YArray(outputs)

return Y.YMap(cell)

Expand Down

0 comments on commit 74e0922

Please sign in to comment.