-
Notifications
You must be signed in to change notification settings - Fork 5.6k
feat(jupyter): support confirm
and prompt
in notebooks
#23592
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
Changes from 6 commits
27ef078
f9a7a3e
49c60d7
2162e33
eb7ed61
f4faf22
6600ddd
310a793
dba8b73
ac4c0c0
e5d1965
16e5e2f
1cc0ccd
5517f93
ba2c242
29339f8
31ea718
1394e7f
8fcc263
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,13 +10,15 @@ use crate::tools::jupyter::server::StdioMsg; | |
use deno_core::error::AnyError; | ||
use deno_core::op2; | ||
use deno_core::serde_json; | ||
use deno_core::serde_json::json; | ||
use deno_core::OpState; | ||
use tokio::sync::mpsc; | ||
use tokio::sync::Mutex; | ||
|
||
deno_core::extension!(deno_jupyter, | ||
ops = [ | ||
op_jupyter_broadcast, | ||
op_jupyter_input, | ||
], | ||
options = { | ||
sender: mpsc::UnboundedSender<StdioMsg>, | ||
|
@@ -30,6 +32,61 @@ deno_core::extension!(deno_jupyter, | |
}, | ||
); | ||
|
||
#[op2(async)] | ||
#[string] | ||
pub async fn op_jupyter_input( | ||
state: Rc<RefCell<OpState>>, | ||
#[string] prompt: String, | ||
#[serde] is_password: serde_json::Value, | ||
) -> Result<Option<String>, AnyError> { | ||
let (_iopub_socket, last_execution_request, stdin_socket) = { | ||
let s = state.borrow(); | ||
|
||
( | ||
s.borrow::<Arc<Mutex<Connection<zeromq::PubSocket>>>>() | ||
.clone(), | ||
s.borrow::<Rc<RefCell<Option<JupyterMessage>>>>().clone(), | ||
s.borrow::<Arc<Mutex<Connection<zeromq::RouterSocket>>>>() | ||
.clone(), | ||
) | ||
}; | ||
|
||
let mut stdin = stdin_socket.lock().await; | ||
|
||
let maybe_last_request = last_execution_request.borrow().clone(); | ||
if let Some(last_request) = maybe_last_request { | ||
if !last_request.allow_stdin() { | ||
return Ok(None); | ||
} | ||
|
||
/* | ||
* 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. | ||
* """ | ||
*/ | ||
last_request | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAIK, I think this is correct. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you 👏 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
.new_message("input_request") | ||
.with_identities(&last_request) | ||
.with_content(json!({ | ||
"prompt": prompt, | ||
"password": is_password, | ||
})) | ||
.send(&mut *stdin) | ||
.await?; | ||
|
||
let response = JupyterMessage::read(&mut *stdin).await?; | ||
|
||
return Ok(Some(response.value().to_string())); | ||
} | ||
|
||
Ok(None) | ||
} | ||
|
||
#[op2(async)] | ||
pub async fn op_jupyter_broadcast( | ||
state: Rc<RefCell<OpState>>, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -180,6 +180,14 @@ impl JupyterMessage { | |
self.content["comm_id"].as_str().unwrap_or("") | ||
} | ||
|
||
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("") | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
// Creates a new child message of this message. ZMQ identities are not transferred. | ||
pub(crate) fn new_message(&self, msg_type: &str) -> JupyterMessage { | ||
let mut header = self.header.clone(); | ||
|
@@ -235,6 +243,14 @@ impl JupyterMessage { | |
self | ||
} | ||
|
||
pub(crate) fn with_identities( | ||
mut self, | ||
msg: &JupyterMessage, | ||
) -> JupyterMessage { | ||
self.zmq_identities = msg.zmq_identities.clone(); | ||
self | ||
} | ||
|
||
pub(crate) async fn send<S: zeromq::SocketSend>( | ||
&self, | ||
connection: &mut Connection<S>, | ||
|
Uh oh!
There was an error while loading. Please reload this page.