Skip to content

Commit dc770c9

Browse files
committed
widgets: do not load our k3d; set buffers properly (using full path)
1 parent cb5efda commit dc770c9

File tree

3 files changed

+31
-45
lines changed

3 files changed

+31
-45
lines changed

src/packages/frontend/jupyter/widgets/k3d/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
console.log("LOADING forked k3d");
2+
13
import ObjectModel from "./object-model";
24
import ChunkModel from "./chunk-model";
35
import ObjectView from "./object-view";

src/packages/frontend/jupyter/widgets/manager.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import * as react_output from "./output";
1616
import * as react_controls from "./controls";
1717
import { size } from "lodash";
1818
import { delay } from "awaiting";
19-
import * as k3d from "./k3d";
19+
//import * as k3d from "./k3d";
2020

2121
/*
2222
NOTES: Third party custom widgets:
@@ -591,7 +591,8 @@ export class WidgetManager extends base.ManagerBase<HTMLElement> {
591591
module = react_output;
592592
} else if (moduleName === "k3d") {
593593
// NOTE: I completely rewrote the entire k3d widget interface...
594-
module = k3d;
594+
//module = k3d;
595+
throw Error("k3d disabled");
595596
} else if (moduleName === "jupyter-matplotlib") {
596597
//module = await import("jupyter-matplotlib");
597598
throw Error(`custom widgets: ${moduleName} not installed`);

src/packages/frontend/jupyter/widgets/manager2.ts

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -242,33 +242,18 @@ export class WidgetManager {
242242
const model = await this.manager.get_model(model_id);
243243
const { buffer_paths, buffers } =
244244
await this.ipywidgets_state.get_model_buffers(model_id);
245-
log("handleBuffersChange: ", model_id, buffer_paths);
245+
log("handleBuffersChange: ", { model_id, buffer_paths, buffers });
246246
const deserialized_state = model.get_state(true);
247-
const serializers = (model.constructor as any).serializers;
248247
const change: { [key: string]: any } = {};
249248
for (let i = 0; i < buffer_paths.length; i++) {
250-
// TODO/concern: what if buffer_paths is deeper (length > 1)?
251-
// Will that break something? We do set things properly later.
252249
const key = buffer_paths[i][0];
253-
const buffer = buffers[i];
254-
if (deserialized_state[key] == null || buffer == null) {
255-
change[key] = null;
256-
continue;
257-
}
258-
let s;
259-
const f = serializers[key];
260-
if (f != null) {
261-
s = f.serialize(deserialized_state[key]);
262-
s.value = { buffer };
263-
s = f.deserialize(s);
264-
} else {
265-
s = { ...deserialized_state[key], value: buffer };
266-
}
267-
change[key] = s;
250+
setInObject(deserialized_state[key], buffer_paths[i], buffers[i]);
251+
change[key] = deserialized_state[key];
268252
}
269253
log("handleBuffersChange: ", model_id, change);
270-
window.x = { model, model_id, change };
271-
model.set_state(change);
254+
if (len(change) > 0) {
255+
model.set_state(change);
256+
}
272257
};
273258

274259
private ipywidgets_state_MessageChange = async (model_id: string) => {
@@ -580,15 +565,10 @@ class Environment implements WidgetEnvironment {
580565
}
581566
const { buffer_paths, buffers } =
582567
await this.manager.ipywidgets_state.get_model_buffers(model_id);
568+
583569
for (let i = 0; i < buffer_paths.length; i++) {
584-
// TODO/concern: what if buffer_paths is deeper (length > 1)?
585-
// Will that break something? We do set things properly later.
586570
const buffer = buffers[i];
587-
const key = buffer_paths[i][0];
588-
// TODO -- but what about deeply nested key!?
589-
// I only figured out to use value:{buffer} from looking at
590-
// the bqplot source code.
591-
state[key] = { ...state[key], value: { buffer } };
571+
setInObject(state, buffer_paths[i], buffer);
592572
}
593573

594574
setTimeout(() => this.manager.watchModel(model_id), 1);
@@ -646,7 +626,7 @@ WidgetModel.prototype.sync = () => {};
646626
// to be non-fatal, so it's more flexible wrt to our realtime sync setup.
647627
export function put_buffers(
648628
state,
649-
buffer_paths: (string | number)[][],
629+
buffer_paths: string[][],
650630
buffers: any[],
651631
): void {
652632
for (let i = 0; i < buffer_paths.length; i++) {
@@ -658,19 +638,22 @@ export function put_buffers(
658638
buffer instanceof ArrayBuffer ? buffer : buffer.buffer,
659639
);
660640
}
661-
// say we want to set state[x][y][z] = buffer
662-
let obj = state as any;
663-
// we first get obj = state[x][y]
664-
for (let j = 0; j < buffer_path.length - 1; j++) {
665-
if (obj[buffer_path[j]] == null) {
666-
// doesn't exist, so create it. This makes things work in
667-
// possibly more random order, rather than crashing. I hit this,
668-
// e.g., when defining animations for k3d.
669-
obj[buffer_path[j]] = {};
670-
}
671-
obj = obj[buffer_path[j]];
672-
}
673-
// and then set: obj[z] = buffer
674-
obj[buffer_path[buffer_path.length - 1]] = buffer;
641+
setInObject(state, buffer_path, buffer);
642+
}
643+
}
644+
645+
function setInObject(obj: any, path: string[], value: any) {
646+
// say we want to set obj[x][y][z] = value
647+
// we first get obj = state[x][y]
648+
for (let j = 0; j < path.length - 1; j++) {
649+
if (obj[path[j]] == null) {
650+
// doesn't exist, so create it. This makes things work in
651+
// possibly more random order, rather than crashing. I hit this,
652+
// e.g., when defining animations for k3d.
653+
obj[path[j]] = {};
654+
}
655+
obj = obj[path[j]];
675656
}
657+
// and then set: obj[z] = value
658+
obj[path[path.length - 1]] = value;
676659
}

0 commit comments

Comments
 (0)