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(unstable): add Deno.jupyter.broadcast API #20656

Merged
merged 11 commits into from Sep 27, 2023

Conversation

bartlomieju
Copy link
Member

Closes #20591

@bartlomieju
Copy link
Member Author

@rgbkrk this adds Deno.jupyter.sendIo API as we discussed in the issue, however sending anything (eg. await Deno.jupyter.sendIo({ "text/html": "<h1>Hello there!</h1>" });) has no effect, it's just null that's printed in the notebook. Am I doing something wrong, or did I misunderstand how this is supposed to work?

@rgbkrk
Copy link
Contributor

rgbkrk commented Sep 24, 2023

It looks like you need more of the payload (data, metadata). I'll dive in shortly with this branch.

@bartlomieju
Copy link
Member Author

bartlomieju commented Sep 24, 2023

Ooops, you're right! Even if I change the invocation to await Deno.jupyter.sendIo({ "data": {"text/plain": "<h1>Hello there!!!</h1>" } }); I still don't get anything printed :/ will continue to look what's wrong here.

EDIT: Ah, got it:

await Deno.jupyter.sendIo({ "data": {"text/html": "<h1>Hello there!!!</h1>" }, "metadata": { "text/htmll": {"foo": true } }});

Adding "metadata" does the trick and the message gets displayed:
Screenshot 2023-09-24 at 20 06 39

@rgbkrk
Copy link
Contributor

rgbkrk commented Sep 24, 2023

I think it's ok to show null as opposed to undefined. For me at least what would be helpful is getting back the created message to know that it made what I needed. The simpler layer on top gets built.

I still need some way to send update_display_data which has the same payload but the message type is different.

async sendIo(content) {
await core.opAsync("op_jupyter_send_io", content);
},
},
Copy link
Contributor

Choose a reason for hiding this comment

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

🆒 now I know

@rgbkrk
Copy link
Contributor

rgbkrk commented Sep 24, 2023

PR to add the ability to send an arbitrary IOPub message: bartlomieju#4

feat(jupyter): accept message type in jupyter io publish call
@rgbkrk
Copy link
Contributor

rgbkrk commented Sep 24, 2023

This makes it nice and easy to build progress bars in the notebook and any other kind of report for users. Excited to see this coming in.

updating-displays

@rgbkrk
Copy link
Contributor

rgbkrk commented Sep 25, 2023

I made sure other iopub messages worked:

clear_output

clear_output

stream

image

display_data with application/json

image

comm_open and comm_msg

Of course, I can't tell how properly comm sending is working unless this is connected up with a receiver on the frontend.

image

execute_result

image

Generally I think this is great. I'm not a big fan of the name sendIo. It can look like lo in some fonts. I would at least capitalize the O as well in IO so it reads as sendIO.

Some alternatives I'm a fan of:

Deno.jupyter.iopub.send(...)

Deno.jupyter.broadcast(...)

Deno.jupyter.publishIO(...)

However, I don't wish to hold things up for the name of the function. Let's get this out there!

@@ -178,6 +178,11 @@ const denoNsUnstable = {
Kv: kv.Kv,
KvU64: kv.KvU64,
KvListIterator: kv.KvListIterator,
jupyter: {
Copy link
Member

Choose a reason for hiding this comment

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

I think Deno.jupyter should only exist if we're running within the context of deno jupyter

Copy link
Member Author

Choose a reason for hiding this comment

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

I actually want to make it a getter that will throw an informative error in non-Jupyter context 👍

@rgbkrk
Copy link
Contributor

rgbkrk commented Sep 26, 2023

What else do we need for this? Tests?

I'm delighted to start using it within libraries.

@bartlomieju
Copy link
Member Author

@rgbkrk no blockers, I'll try to finish it tonight so we can ship it in v1.37.1 tomorrow.

@bartlomieju bartlomieju changed the title [WIP] feat(unstable): add sendio for Jupyter feat(unstable): add sendio for Jupyter Sep 26, 2023
@bartlomieju bartlomieju changed the title feat(unstable): add sendio for Jupyter feat(unstable): add Deno.jupyter API Sep 26, 2023
@bartlomieju bartlomieju changed the title feat(unstable): add Deno.jupyter API feat(unstable): add Deno.jupyter.broadcast API Sep 26, 2023
function enableJupyter() {
denoNsUnstable.jupyter = {
async broadcast(msgType, content) {
await core.opAsync("op_jupyter_broadcast", msgType, content);
Copy link
Member

Choose a reason for hiding this comment

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

Nit: ensureFastOps here.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think that makes no difference since it's using serde_json::Value which has no chance to ever be a fast call.

Copy link
Member

Choose a reason for hiding this comment

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

It's better performing than opAsync in general because it doesn't use the spread operator -- we generate overloads for the correct number of args.

In this case it's not critical but generally we should avoid using opAsync.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll change it in a follow up PR

Copy link
Member

@mmastrac mmastrac left a comment

Choose a reason for hiding this comment

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

LGTM

() => Deno.jupyter,
"Deno.jupyter is only available in `deno jupyter` subcommand.",
);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

How should we test the actual functionality under Deno.jupyter? Do we have to somehow monkeypatch to allow it then test it?

Copy link
Member Author

Choose a reason for hiding this comment

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

I added another cell in integration_test.ipynb notebook - if you run it manually there should be no changes in Git

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahhhh beautiful

* @category Jupyter */
export function broadcast(
msgType: string,
content: Record<string, unknown>,
Copy link
Contributor

Choose a reason for hiding this comment

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

🎉

@bartlomieju bartlomieju merged commit 46a4bd5 into denoland:main Sep 27, 2023
13 checks passed
@bartlomieju bartlomieju deleted the jupyter_stdio branch September 27, 2023 00:29
"traceback": [
"Stack trace:",
"TypeError: Cannot read properties of undefined (reading 'broadcast')",
" at <anonymous>:2:20"
Copy link
Contributor

Choose a reason for hiding this comment

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

🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

Oooops, git played a fool out of me. This should have been actual output. I will fix it in a follow up

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.

Deno Userland Jupyter Messaging library: display_data
4 participants