Skip to content

Commit 536a0e8

Browse files
committed
widgets: refactoring and cleanup -- doesn't fix anything
1 parent c7b3eec commit 536a0e8

File tree

5 files changed

+47
-39
lines changed

5 files changed

+47
-39
lines changed

src/packages/frontend/jupyter/output-messages/ipywidget.tsx

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ export function IpyWidget({ value, actions }: WidgetProps) {
3838
// console.log("IpyWidget: not rendering due to actions=null");
3939
return;
4040
}
41-
if (divRef.current == null) {
41+
const div = divRef.current;
42+
if (div == null) {
4243
// console.log("IpyWidget: not rendering due to divRef.current=null");
4344
return;
4445
}
@@ -60,27 +61,7 @@ export function IpyWidget({ value, actions }: WidgetProps) {
6061
// console.log("IpyWidget: not rendering due to manager not being set");
6162
return;
6263
}
63-
try {
64-
manager.render(id, divRef.current);
65-
66-
// HACK: because upstream ipywidgets only checks for MathJax.Hub to be defined then
67-
// crashes on load -- they don't see this bug because user has to explicitly re-evaluate
68-
// code to see anything on page refresh, due to all state being on the frontend.
69-
// @ts-ignore
70-
if (window.MathJax != null && window.MathJax.Hub == null) {
71-
// @ts-ignore
72-
MathJax.Hub.Queue = () => {};
73-
}
74-
setTimeout(() => {
75-
// Run mathjax on labels: widgets.HBox([widgets.Label(value="The $m$ in $E=mc^2$:"), widgets.FloatSlider()])
76-
// @ts-ignore
77-
$(divRef.current).find(".widget-label").katex?.({ preProcess: true });
78-
// @ts-ignore
79-
$(divRef.current).find(".widget-htmlmath").katex?.({ preProcess: true });
80-
}, 0);
81-
} catch (err) {
82-
console.warn(err);
83-
}
64+
render({ manager, id, div });
8465
}, []);
8566

8667
if (unknown) {
@@ -99,3 +80,27 @@ export function IpyWidget({ value, actions }: WidgetProps) {
9980
</div>
10081
);
10182
}
83+
84+
async function render({ manager, id, div }) {
85+
try {
86+
await manager.render(id, div);
87+
88+
// HACK: because upstream ipywidgets only checks for MathJax.Hub to be defined then
89+
// crashes on load -- they don't see this bug because user has to explicitly re-evaluate
90+
// code to see anything on page refresh, due to all state being on the frontend.
91+
// @ts-ignore
92+
if (window.MathJax != null && window.MathJax.Hub == null) {
93+
// @ts-ignore
94+
MathJax.Hub.Queue = () => {};
95+
}
96+
setTimeout(() => {
97+
// @ts-ignore
98+
const elt = $(div) as any;
99+
// Run mathjax on labels: widgets.HBox([widgets.Label(value="The $m$ in $E=mc^2$:"), widgets.FloatSlider()])
100+
elt.find(".widget-label").katex?.({ preProcess: true });
101+
elt.find(".widget-htmlmath").katex?.({ preProcess: true });
102+
}, 0);
103+
} catch (err) {
104+
console.error("Error Rendering Widget:", err);
105+
}
106+
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ 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";
2019

2120
/*
2221
NOTES: Third party custom widgets:
@@ -592,7 +591,7 @@ export class WidgetManager extends base.ManagerBase<HTMLElement> {
592591
} else if (moduleName === "k3d") {
593592
// NOTE: I completely rewrote the entire k3d widget interface, since
594593
// it made tons of assumptions that break RTC.
595-
module = k3d;
594+
module = await import('k3d');
596595
} else if (moduleName === "jupyter-matplotlib") {
597596
//module = await import("jupyter-matplotlib");
598597
throw Error(`custom widgets: ${moduleName} not installed`);

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,8 @@ export class WidgetManager {
6969
if (this.ipywidgets_state.get_state() != "ready") {
7070
return;
7171
}
72-
log("initAllModels");
7372
for (const { model_id, type } of this.ipywidgets_state.keys()) {
7473
if (type == "state") {
75-
log("initAllModels", model_id);
7674
(async () => {
7775
try {
7876
await this.manager.get_model(model_id);
@@ -258,12 +256,13 @@ export class WidgetManager {
258256
*/
259257
const { buffer_paths, buffers } =
260258
await this.ipywidgets_state.get_model_buffers(model_id);
261-
log("handleBuffersChange: ", { model_id, buffer_paths, buffers });
262259
if (buffer_paths.length == 0) {
263260
return;
264261
}
262+
log("handleBuffersChange: ", { model_id, buffer_paths, buffers });
265263
const state = this.ipywidgets_state.get_model_state(model_id);
266264
if (state == null) {
265+
log("handleBuffersChange: no state data", { model_id });
267266
return;
268267
}
269268
const change: { [key: string]: any } = {};
@@ -272,11 +271,15 @@ export class WidgetManager {
272271
setInObject(state, buffer_paths[i], buffers[i]);
273272
change[key] = state[key];
274273
}
275-
log("handleBuffersChange: ", model_id, { change });
276274
if (len(change) > 0) {
277275
const model = await this.manager.get_model(model_id);
276+
const deserializedChange = await this.deserializeState(model, change);
277+
log("handleBuffersChange: settings ", model_id, {
278+
change,
279+
deserializedChange,
280+
});
278281
try {
279-
model.set(change);
282+
model.set(deserializedChange);
280283
} catch (err) {
281284
// window.y = { model_id, model, change, buffer_paths, buffers };
282285
console.error("saved to y", err);
@@ -490,7 +493,7 @@ export class WidgetManager {
490493
comm_id,
491494
target_name,
492495
data,
493-
metadata,
496+
metadata: _metadata,
494497
buffers,
495498
}: {
496499
comm_id: string;
@@ -499,7 +502,7 @@ export class WidgetManager {
499502
metadata?: JSONValue;
500503
buffers?: ArrayBuffer[];
501504
}): Promise<IClassicComm> => {
502-
log("openCommChannel", { comm_id, target_name, data, buffers, metadata });
505+
// log("openCommChannel", { comm_id, target_name, data, buffers, metadata });
503506
const { send_comm_message_to_kernel } = this.actions;
504507

505508
// TODO: we do not currently have anything at all that
@@ -585,7 +588,7 @@ class Environment implements WidgetEnvironment {
585588
}
586589
}
587590

588-
async getModelState(model_id) {
591+
async getSerializedModelState(model_id) {
589592
// log("getModelState", model_id);
590593
if (this.manager.ipywidgets_state.get_state() != "ready") {
591594
await once(this.manager.ipywidgets_state, "ready");
@@ -608,6 +611,7 @@ class Environment implements WidgetEnvironment {
608611
model_id,
609612
"k3d: waiting for state.type to be defined",
610613
);
614+
611615
await once(this.manager.ipywidgets_state, "change");
612616
state = this.manager.ipywidgets_state.get_model_state(model_id);
613617
}
@@ -631,9 +635,9 @@ class Environment implements WidgetEnvironment {
631635
setInObject(state, buffer_paths[i], buffer);
632636
}
633637
}
634-
setTimeout(() => this.manager.watchModel(model_id), 1);
635638

636639
log("getModelState", { model_id, state });
640+
setTimeout(() => this.manager.watchModel(model_id), 1);
637641
return {
638642
modelName: state._model_name,
639643
modelModule: state._model_module,

src/packages/frontend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"@cocalc/local-storage-lru": "^2.4.3",
4646
"@cocalc/sync": "workspace:*",
4747
"@cocalc/util": "workspace:*",
48-
"@cocalc/widgets": "^1.1.1",
48+
"@cocalc/widgets": "^1.1.2",
4949
"@cocalc/xpra-lz4": "^1.1.0",
5050
"@dnd-kit/core": "^6.0.7",
5151
"@dnd-kit/modifiers": "^6.0.1",

src/packages/pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)