Skip to content

Commit 280e6e3

Browse files
authored
Add clearOutputs() method to ISharedCodeCell (#330)
* add-clearOutputs-method * add-test
1 parent a2a9acb commit 280e6e3

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

javascript/src/api.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,11 @@ export interface ISharedCodeCell
572572
outputs: Array<nbformat.IOutput>
573573
): void;
574574

575+
/**
576+
* Clear all outputs from the cell.
577+
*/
578+
clearOutputs(origin: any): void;
579+
575580
/**
576581
* Serialize the model to JSON.
577582
*/

javascript/src/ycell.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,19 @@ export class YCodeCell
867867
);
868868
}
869869

870+
/**
871+
* Clear all outputs from the cell.
872+
*/
873+
clearOutputs(origin: any = null): void {
874+
this.transact(
875+
() => {
876+
this._youtputs.delete(0, this._youtputs.length);
877+
},
878+
false,
879+
origin
880+
);
881+
}
882+
870883
/**
871884
* Serialize the model to JSON.
872885
*/

javascript/test/ycell.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,22 @@ describe('@jupyter/ydoc', () => {
333333
});
334334
});
335335
});
336+
337+
test('should clear outputs from a code cell', () => {
338+
const codeCell = YCodeCell.create();
339+
const outputs = [
340+
{
341+
data: {
342+
'text/plain': ['Hello, world!']
343+
},
344+
metadata: {},
345+
output_type: 'execute_result'
346+
}
347+
];
348+
349+
codeCell.setOutputs(outputs);
350+
expect(codeCell.getOutputs()).toEqual(outputs);
351+
352+
codeCell.clearOutputs();
353+
expect(codeCell.getOutputs()).toEqual([]);
354+
});

0 commit comments

Comments
 (0)