Skip to content

Commit 71fe442

Browse files
committed
widgets: properly delete output buffers when clearing output
1 parent 5c44d99 commit 71fe442

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

src/packages/sync/editor/generic/ipywidgets-state.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,13 @@ export class IpywidgetsState extends EventEmitter {
240240
}
241241
const f = async (path: string) => {
242242
const hash = value?.get(path);
243-
if (hash == null) return;
243+
if (!hash) {
244+
// It is important to look for !hash, since we use hash='' as a sentinel (in this.clearOutputBuffers)
245+
// to indicate that we want to consider a buffer as having been deleted. This is very important
246+
// to do since large outputs are often buffers in output widgets, and clear_output
247+
// then needs to delete those buffers, or output never goes away.
248+
return;
249+
}
244250
const cur = this.arrayBuffers[model_id][path];
245251
if (cur?.hash == hash) {
246252
buffer_paths.push(JSON.parse(path));
@@ -741,7 +747,7 @@ export class IpywidgetsState extends EventEmitter {
741747
this.clear_output[model_id] = true;
742748
} else {
743749
delete this.clear_output[model_id];
744-
this.clearOutputBuffers();
750+
this.clearOutputBuffers(model_id);
745751
this.set_model_value(model_id, { outputs: null });
746752
}
747753
return true;
@@ -755,7 +761,7 @@ export class IpywidgetsState extends EventEmitter {
755761
let outputs: any;
756762
if (this.clear_output[model_id]) {
757763
delete this.clear_output[model_id];
758-
this.clearOutputBuffers();
764+
this.clearOutputBuffers(model_id);
759765
outputs = [];
760766
} else {
761767
outputs = this.get_model_value(model_id).outputs;
@@ -768,7 +774,7 @@ export class IpywidgetsState extends EventEmitter {
768774
return true;
769775
};
770776

771-
private clearOutputBuffers = () => {
777+
private clearOutputBuffers = (model_id: string) => {
772778
// TODO: need to clear all output buffers.
773779
/* Example where if you do not properly clear buffers, then broken output re-appears:
774780
@@ -789,6 +795,21 @@ with out:
789795
print('hi')
790796
*/
791797
// TODO!!!!
798+
799+
const y: any = {};
800+
let n = 0;
801+
for (const jsonPath of this.get(model_id, "buffers")?.keySeq() ?? []) {
802+
const path = JSON.parse(jsonPath);
803+
console.log("path = ", path);
804+
if (path[0] == "outputs") {
805+
y[jsonPath] = "";
806+
n += 1;
807+
}
808+
}
809+
console.log("y = ", y);
810+
if (n > 0) {
811+
this.set(model_id, "buffers", y, true, "shallow");
812+
}
792813
};
793814

794815
private sendCustomMessage = async (

0 commit comments

Comments
 (0)