Skip to content
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

feat(jupyter): add handling for comms #24250

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

bartlomieju
Copy link
Member

No description provided.

cli/js/40_jupyter.js Outdated Show resolved Hide resolved
cli/tools/jupyter/mod.rs Outdated Show resolved Hide resolved
cli/tools/jupyter/server.rs Outdated Show resolved Hide resolved
cli/tools/jupyter/server.rs Outdated Show resolved Hide resolved
cli/tools/jupyter/server.rs Outdated Show resolved Hide resolved
...data,
buffers,
close() {
if (closed) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need to use an op_jupyter_ I assume, to clean up the mapping on the Rust side.

cli/js/40_jupyter.js Outdated Show resolved Hide resolved
Co-authored-by: Trevor Manz <[email protected]>
Signed-off-by: Bartek Iwańczuk <[email protected]>
Comment on lines 420 to 423
msgCallback?.({
...data,
buffers,
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow up on comment below, to mirror the send API

Suggested change
msgCallback?.({
...data,
buffers,
});
msgCallback?.(data, buffers);

@manzt
Copy link
Contributor

manzt commented Jun 26, 2024

Bikeshedding the API... right now you still need to open the comm with broadcast separately ("comm_open") message.

let comm = await Deno.jupyter.commOpen(commId, {
  targetName: "jupyter.widgets",
  data: { /* anywidget stuff */ },
  metadata: { version: "2.1.0" },
});

// either only allow one callback or manage a set of them
comm.on((data, buffers) => {

});

comm.close();

comm.send(data, buffers);

or something more webby (e.g., EventTarget, AudioContext as inspo):

let comm = new Deno.jupyter.Comm(commId, {
  targetName: "jupyter.widgets",
  data: { /* anywidget stuff */ },
  metadata: { version: "2.1.0" },
});

comm.addEventListener("message", (event) => {
  let { data, buffers } = event.detail;
});

comm.removeEventListener("message", /* function */ );

comm.dispatchEvent(
  new CustomEvent("message", { detail: { data, buffers } })
  // or something custom - new Deno.jupyter.MessageEvent(data, { buffers });
);

comm.close();

the latter would be easy to extend for other messages https://jupyter-client.readthedocs.io/en/latest/messaging.html#tearing-down-comms:

comm.addEventListener("close", () => {
  // front-end closed cleanup stuff
});

let Some(comm) = maybe_comm else {
return (serde_json::Value::Null, vec![]);
};
comm.receiver.resubscribe()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@manzt @rgbkrk if we go with EventListener approach for a comm then we can expect more than one consumer of the "comm". That means we need to use tokio::sync::broadcast channel which is buffered. We can go with a rather big number for the buffer like 1024 or 65536. If we expect only a single consumer then we could use an mpsc channel that is unbounded. Which one should we go with?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For widgets, I think it will basically only be one comm listener. There is new comm created for each new widget instance.

bartlomieju added a commit that referenced this pull request Jul 2, 2024
Moves the ZeroMQ messaging server to a separate thread.

This will allow to run blocking JS code and maintain communication
with the notebook frontend.

Towards #23592
Towards #24250
Closes #23617
@bartlomieju
Copy link
Member Author

@manzt quick question

let comm = await Deno.jupyter.commOpen(commId, {
  targetName: "jupyter.widgets",
  data: { /* anywidget stuff */ },
  metadata: { version: "2.1.0" },
});

What is metadata supposed to do when opening the comm? According to https://jupyter-client.readthedocs.io/en/latest/messaging.html#opening-a-comm it's not used.

@manzt
Copy link
Contributor

manzt commented Jul 6, 2024

What is metadata supposed to do when opening the comm? According to jupyter-client.readthedocs.io/en/latest/messaging.html it's not used.

The metadata is required for "comm_open" for with jupyter.widget target. With this PR I can successfully communicate with the anywidget frontend, sending frontend ESM via comms with updates.

The docs say it's often not used, but in the case of the jupyter.widgets front-end target it is required (there is an exception in the frontend when it's missing). So it's metadata that's specific to the targetName, and not necessary for opening any comm I assume.

zebreus pushed a commit to zebreus/deno that referenced this pull request Jul 8, 2024
…24373)

Moves the ZeroMQ messaging server to a separate thread.

This will allow to run blocking JS code and maintain communication
with the notebook frontend.

Towards denoland#23592
Towards denoland#24250
Closes denoland#23617
@bartlomieju bartlomieju added this to the 2.1.0 milestone Sep 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants