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): support confirm and prompt in notebooks #23592

Merged
merged 19 commits into from
Jul 4, 2024

Conversation

zph
Copy link
Contributor

@zph zph commented Apr 28, 2024

Closes: #22633

Supports confirm and prompt with custom versions used when inside a Jupyter Notebook with Deno kernel.

The desired behavior (per python reference and docs):

  • confirm or prompt will trigger kernel to request the frontend to get user's input and return it to backend for processing. If stdin not supported, skip the request.

We accomplish this by creating custom versions of confirm and prompt that call into an op_jupyter_input rust function with access to the stdin_socket.

confirm and prompt are instantiated in the jupyter specific TS interface, so they only override the standard functions in jupyter context.

Jupyter requires us to clone zmq_identities for this "input_request" message as documented in comments:

* Using with_identities() because of jupyter client docs instruction
* Requires cloning identities per :
* https://jupyter-client.readthedocs.io/en/latest/messaging.html#messages-on-the-stdin-router-dealer-channel
* The stdin socket of the client is required to have the
*  same zmq IDENTITY as the client’s shell socket.
*  Because of this, the input_request must be sent with the same IDENTITY
*  routing prefix as the execute_reply in order for the frontend to receive the message.

TODO

  • Get review on Jupyter mechanics to confirm correctness
  • Get review on Rust implementation / code style
  • Add JSDoc to confirm/prompt for jupyter

PR guidelines

  • Before submitting a PR, please read https://docs.deno.com/runtime/manual/references/contributing
  • Comply with title guidelines
  • Ensure there is a related issue and it is referenced in the PR text.
  • Ensure there are tests that cover the changes.
  • Ensure cargo test passes.
  • Ensure ./tools/format.js passes without changing files.
  • Ensure ./tools/lint.js passes.
  • Open as a draft PR if your work is still in progress. The CI won't run
    all steps, but you can add '[ci]' to a commit message to force it to.
    If you would like to run the benchmarks on the CI, add the 'ci-bench' label.

@CLAassistant
Copy link

CLAassistant commented Apr 28, 2024

CLA assistant check
All committers have signed the CLA.

@zph
Copy link
Contributor Author

zph commented Apr 28, 2024

@rgbkrk 👋 I see you've been advising on the Jupyter side for Deno's kernel. If you have a moment to sanity check this for the approach I took re: jupyter protocols, I'd appreciate it.

This PR enables confirm and prompt behavior in deno based Jupyter notebooks (ala input prompts in Python notebooks).

Specifically, the way I did zmq_identities copying works, but is it the right way?

Otherwise, I've taken what I saw as python's approach for input, which looks to be overriding input with a custom builtin in the context of the ipython notebook.

Thanks for checking out this draft if you have availability 🙏 .

I'm trying to bring feature parity for Deno notebooks with Python as the reference and I find myself relying on this functionality in python notebooks when used as operational runbooks.

* routing prefix as the execute_reply in order for the frontend to receive the message.
* """
*/
last_request
Copy link
Contributor

Choose a reason for hiding this comment

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

AFAIK, I think this is correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you 👏

Copy link
Contributor

Choose a reason for hiding this comment

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

Confirmed this by the way and incorporated it directly into the new library to make it easier to do.

@zph zph marked this pull request as ready for review April 30, 2024 02:03
zph added 3 commits April 29, 2024 19:05
Supports `confirm` and `prompt` with custom versions used when inside a
Jupyter Notebook with Deno kernel.

The desired behavior (per python reference and docs):
* confirm or prompt will trigger kernel to request the frontend to get
  user's input and return it to backend for processing

We accomplish this by creating custom versions of confirm and prompt
that call into an op_jupyter_input rust function with access to the
stdin_socket.

`confirm` and `prompt` are instantiated in the jupyter specific TS
interface, so they only override the standard functions in jupyter
context.

Jupyter requires us to clone zmq_identities for this "input_request"
message as documented in comments:

```
* Using with_identities() because of jupyter client docs instruction
* Requires cloning identities per :
* https://jupyter-client.readthedocs.io/en/latest/messaging.html#messages-on-the-stdin-router-dealer-channel
* The stdin socket of the client is required to have the
*  same zmq IDENTITY as the client’s shell socket.
*  Because of this, the input_request must be sent with the same IDENTITY
*  routing prefix as the execute_reply in order for the frontend to receive the message.
```
@zph zph force-pushed the jupyter-add-confirm-and-prompt-support branch from 9d07554 to 1cd77e2 Compare April 30, 2024 02:05
@zph zph force-pushed the jupyter-add-confirm-and-prompt-support branch from 1cd77e2 to 2162e33 Compare April 30, 2024 02:07
@zph
Copy link
Contributor Author

zph commented Apr 30, 2024

☹ leading whitespace on PR title. Pushing fix :)

image

Thanks for bumping this to kick off CI @dsherret! I've been enjoying using dax also as part of these notebooks.

@zph zph changed the title feat(cli/tools/jupyter) support confirm and prompt in notebooks feat(cli/tools/jupyter) support confirm and prompt in notebooks Apr 30, 2024
@dsherret dsherret changed the title feat(cli/tools/jupyter) support confirm and prompt in notebooks feat(jupyter): support confirm and prompt in notebooks Apr 30, 2024
@zph
Copy link
Contributor Author

zph commented Apr 30, 2024

@dsherret I'll treat this as blocked pending: #23617

@bartlomieju bartlomieju added this to the 1.44 milestone May 5, 2024
Comment on lines 183 to 189
pub(crate) fn allow_stdin(&self) -> bool {
self.content["allow_stdin"].as_bool().unwrap_or(false)
}

pub(crate) fn value(&self) -> &str {
self.content["value"].as_str().unwrap_or("")
}
Copy link
Member

Choose a reason for hiding this comment

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

Are you sure that these values are always present in the Jupyter messages? If not, we should use self.content.get(...) instead and provide default values that are "falsy".

Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

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

Really nice work @zph! This is a great improvement to our Jupyter kernel support 👍

Now that #23592 has landed I think we are ready to rewrite op_jupyter_input to be a synchronous op and finalize this PR so it can land in Deno v1.44.

Let me know if you need any help with rebasing and/or turning that op into a sync code.

@zph
Copy link
Contributor Author

zph commented May 5, 2024

Great! I'll look at it this weekend and see if I can get it working ☺️

@bartlomieju
Copy link
Member

Ooops, I actually meant #23622 and @dsherret told me that it's still not enough to address #23617.

@zph
Copy link
Contributor Author

zph commented May 6, 2024

Ooops, I actually meant #23622 and @dsherret told me that it's still not enough to address #23617.

No problem, I subscribed to #23617 and will keep an eye out for it landing!

@rgbkrk
Copy link
Contributor

rgbkrk commented May 16, 2024

I've done some deeper typing of the jupyter structures into a library we can share across projects and integrated it with deno here: #23826

It'll be great to bring your work directly into it.

@zph
Copy link
Contributor Author

zph commented May 16, 2024

I've done some deeper typing of the jupyter structures into a library we can share across projects and integrated it with deno here: #23826

It'll be great to bring your work directly into it.

Awesome! I'll wait a bit for yours to land :) and then happy to incorporate/build-on.

@rgbkrk
Copy link
Contributor

rgbkrk commented May 22, 2024

Landed #23826! You can either rebase/merge main or kick off a new PR. Feel free to ping me, I'm happy to help.

@zph
Copy link
Contributor Author

zph commented May 22, 2024

Awesome, thanks @rgbkrk

@bartlomieju
Copy link
Member

@zph I got this working using #24373. Do you want me to push to your PR or open a new one based on your code?

@zph
Copy link
Contributor Author

zph commented Jul 1, 2024 via email

@zph
Copy link
Contributor Author

zph commented Jul 1, 2024

@zph I got this working using #24373. Do you want me to push to your PR or open a new one based on your code?

@bartlomieju I'm excited you got it working! I took a few hours on it but didn't make much progress and think I need more rust under my belt to solve it after the earlier refactor.

This will be awesome to see in the language and it's cool that some base of my code will make it into deno

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

You're welcome to push on to this PR! 😁

Great, I will do just that.

@bartlomieju I'm excited you got it working! I took a few hours on it but didn't make much progress and think I need more rust under my belt to solve it after the earlier refactor.

No worries, you tackled a really tough one which looked easy on the surface. Kudos for prompting us (pun intended) to address this one.

This will be awesome to see in the language and it's cool that some base of my code will make it into deno

Keep it coming!

@bartlomieju
Copy link
Member

This is now working:
Screenshot 2024-07-01 at 02 19 45

I'm gonna land it as is and add tests in a follow up PR, because they require to rewrite a bunch of testing infra and I want to land changes from #24250 first.

Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

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

LGTM, exciting work @zph! Thanks for the PR

@bartlomieju bartlomieju enabled auto-merge (squash) July 4, 2024 21:44
@zph
Copy link
Contributor Author

zph commented Jul 4, 2024

LGTM, exciting work @zph! Thanks for the PR

Awesome! Thank you for carrying this along 😺. I'll pull it down and take it for a spin.

@bartlomieju bartlomieju merged commit f00f0f9 into denoland:main Jul 4, 2024
17 checks passed
@zph
Copy link
Contributor Author

zph commented Jul 4, 2024

Confirmed looks good in:

  • vscode's jupyter notebook environment
  • web ui for jupyter notebooks

@zph zph deleted the jupyter-add-confirm-and-prompt-support branch July 4, 2024 22:13
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
zebreus pushed a commit to zebreus/deno that referenced this pull request Jul 8, 2024
…23592)

Closes: denoland#22633

This commit adds support for `confirm` and `prompt` APIs,
that instead of reading from stdin are using notebook frontend
to show modal boxes and wait for answers.

---------

Co-authored-by: Bartek Iwańczuk <[email protected]>
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.

[feature-request] [jupyter] support entering a value inline, with the web standard function prompt
5 participants