Skip to content

Commit 8fa77d4

Browse files
committed
widgets: first pass at comm message that sets binary buffers
- with file upload this works, then is immediately overwritten by empty data, thus breaking it. So not done yet.
1 parent 2ddbad4 commit 8fa77d4

File tree

7 files changed

+102
-105
lines changed

7 files changed

+102
-105
lines changed

src/packages/frontend/jupyter/browser-actions.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
} from "../misc/local-storage";
3636
import { parse_headings } from "./contents";
3737
import { webapp_client } from "@cocalc/frontend/webapp-client";
38+
import { bufferToBase64 } from "@cocalc/util/base64";
3839

3940
export class JupyterActions extends JupyterActions0 {
4041
public widget_manager?: WidgetManager;
@@ -452,16 +453,24 @@ export class JupyterActions extends JupyterActions0 {
452453
comm_id,
453454
target_name,
454455
data,
456+
buffers,
455457
}: {
456458
msg_id?: string;
457459
comm_id: string;
458460
target_name: string;
459-
data: any;
461+
data: unknown;
462+
buffers?: ArrayBuffer[] | ArrayBufferView[];
460463
}): Promise<string> => {
461464
if (!msg_id) {
462465
msg_id = uuid();
463466
}
464-
const msg = { msg_id, target_name, comm_id, data };
467+
let buffers64;
468+
if (buffers != null) {
469+
buffers64 = buffers.map(bufferToBase64);
470+
} else {
471+
buffers64 = [];
472+
}
473+
const msg = { msg_id, target_name, comm_id, data, buffers64 };
465474
await this.api_call("comm", msg);
466475
// console.log("send_comm_message_to_kernel", "sent", msg);
467476
return msg_id;

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type DeserializedModelState = { [key: string]: any };
3030
export type SendCommFunction = (string, data) => string;
3131

3232
const log = console.log;
33+
// const log = (..._args) => {};
3334

3435
export class WidgetManager {
3536
public ipywidgets_state: IpywidgetsState;
@@ -552,10 +553,15 @@ VBox([s1, s2])
552553
metadata?: JSONObject,
553554
buffers?: ArrayBuffer[] | ArrayBufferView[],
554555
): string {
555-
// TODO: buffers! These need to get sent somehow.
556556
log("comm.send", { data, buffers, metadata, callbacks });
557557
const msg_id = uuid();
558-
send_comm_message_to_kernel({ msg_id, comm_id, target_name, data });
558+
send_comm_message_to_kernel({
559+
msg_id,
560+
comm_id,
561+
target_name,
562+
data,
563+
buffers,
564+
});
559565
return msg_id;
560566
},
561567

src/packages/jupyter/kernel/kernel.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import { VERSION } from "@cocalc/jupyter/kernel/version";
7979
import type { NbconvertParams } from "@cocalc/jupyter/types/nbconvert";
8080
import type { Client } from "@cocalc/sync/client/types";
8181
import { getLogger } from "@cocalc/backend/logger";
82+
import { base64ToBuffer } from "@cocalc/util/base64";
8283

8384
const MAX_KERNEL_SPAWN_TIME = 120 * 1000;
8485

@@ -997,13 +998,21 @@ class JupyterKernel extends EventEmitter implements JupyterKernelInterface {
997998
comm_id,
998999
target_name,
9991000
data,
1001+
buffers64,
10001002
}: {
10011003
msg_id: string;
10021004
comm_id: string;
10031005
target_name: string;
10041006
data: any;
1007+
buffers64?: string[];
10051008
}): void {
10061009
const dbg = this.dbg("send_comm_message_to_kernel");
1010+
dbg({ msg_id, comm_id, target_name, data, buffers64 });
1011+
const buffers = buffers64?.map((x) => new Buffer(base64ToBuffer(x))) ?? [];
1012+
dbg(
1013+
"buffers lengths = ",
1014+
buffers.map((x) => x.byteLength),
1015+
);
10071016

10081017
const message = {
10091018
parent_header: {},
@@ -1018,6 +1027,7 @@ class JupyterKernel extends EventEmitter implements JupyterKernelInterface {
10181027
version: VERSION,
10191028
date: new Date().toISOString(),
10201029
},
1030+
buffers,
10211031
};
10221032

10231033
dbg(message);

src/packages/jupyter/redux/project-actions.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,8 +1330,8 @@ export class JupyterActions extends JupyterActions0 {
13301330
throw Error("syncdb's ipywidgets_state must be defined!");
13311331
}
13321332
for (const key of keys) {
1333-
dbg("key = ", key);
13341333
const [, model_id, type] = JSON.parse(key);
1334+
dbg({ key, model_id, type });
13351335
let data: any;
13361336
if (type === "value") {
13371337
const state = this.syncdb.ipywidgets_state.get_model_value(model_id);
@@ -1352,11 +1352,6 @@ export class JupyterActions extends JupyterActions0 {
13521352
// process data, save file to disk, etc).
13531353
// We need to be careful though to not send buffers to the kernel that the kernel sent us,
13541354
// since that would be a waste.
1355-
// TODO
1356-
// I think the format for the send_comm_mmessage_to_kernel call here is easy to deduce
1357-
// from what process_comm_message_from_kernel unpacks.
1358-
// ***
1359-
// TODO
13601355
} else if (type === "state") {
13611356
// TODO: currently ignoring this, since it seems chatty and pointless,
13621357
// and could lead to race conditions probably with multiple users, etc.

0 commit comments

Comments
 (0)