Skip to content

Handle binary data (buffers) transfer from client -> server #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions js/src/comm.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { encode } from "base64-arraybuffer";
import { Throttler } from "./utils";

// This class is a striped down version of Comm from @jupyter-widgets/base
Expand Down Expand Up @@ -29,11 +30,17 @@ export class ShinyComm {
metadata?: any,
buffers?: ArrayBuffer[] | ArrayBufferView[]
): string {

// Encode buffers as base64 before stringifying the message
const buffers_64 = (buffers || []).map((b: ArrayBuffer | ArrayBufferView) => {
const buffer = b instanceof ArrayBuffer ? b : b.buffer;
return encode(buffer);
});

const msg = {
content: {comm_id: this.comm_id, data: data},
metadata: metadata,
// TODO: need to _encode_ any buffers into base64 (JSON.stringify just drops them)
buffers: buffers || [],
buffers: buffers_64,
// this doesn't seem relevant to the widget?
header: {}
};
Expand Down
2 changes: 2 additions & 0 deletions shinywidgets/_shinywidgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def _():
msg = json.loads(msg_txt)
comm_id = msg["content"]["comm_id"]
comm: ShinyComm = COMM_MANAGER.comms[comm_id]
# TODO: seems we probably need to transform (base64 encoded) buffers back into bytes/memoryviews?
comm.handle_msg(msg)

def _restore_state():
Expand All @@ -147,6 +148,7 @@ def _restore_state():
# Reactivity
# --------------------------------------


def reactive_read(widget: Widget, names: Union[str, Sequence[str]]) -> Any:
"""
Reactively read a widget trait
Expand Down
4 changes: 2 additions & 2 deletions shinywidgets/static/output.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading