mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 15:24:46 -05:00
8698e80304
This brings in [`runtimelib`](https://github.com/runtimed/runtimed) to use: ## Fully typed structs for Jupyter Messages ```rust let msg = connection.read().await?; self .send_iopub( runtimelib::Status::busy().as_child_of(msg), ) .await?; ``` ## Jupyter paths Jupyter paths are implemented in Rust, allowing the Deno kernel to be installed completely via Deno without a requirement on Python or Jupyter. Deno users will be able to install and use the kernel with just VS Code or other editors that support Jupyter. ```rust pub fn status() -> Result<(), AnyError> { let user_data_dir = user_data_dir()?; let kernel_spec_dir_path = user_data_dir.join("kernels").join("deno"); let kernel_spec_path = kernel_spec_dir_path.join("kernel.json"); if kernel_spec_path.exists() { log::info!("✅ Deno kernel already installed"); Ok(()) } else { log::warn!("ℹ️ Deno kernel is not yet installed, run `deno jupyter --install` to set it up"); Ok(()) } } ``` Closes https://github.com/denoland/deno/issues/21619
102 lines
2.5 KiB
Rust
102 lines
2.5 KiB
Rust
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
|
|
use std::cell::RefCell;
|
|
use std::rc::Rc;
|
|
use std::sync::Arc;
|
|
|
|
use runtimelib::JupyterMessage;
|
|
use runtimelib::JupyterMessageContent;
|
|
use runtimelib::KernelIoPubConnection;
|
|
use runtimelib::StreamContent;
|
|
|
|
use deno_core::error::AnyError;
|
|
use deno_core::op2;
|
|
use deno_core::serde_json;
|
|
use deno_core::OpState;
|
|
use tokio::sync::mpsc;
|
|
use tokio::sync::Mutex;
|
|
|
|
deno_core::extension!(deno_jupyter,
|
|
ops = [
|
|
op_jupyter_broadcast,
|
|
],
|
|
options = {
|
|
sender: mpsc::UnboundedSender<StreamContent>,
|
|
},
|
|
middleware = |op| match op.name {
|
|
"op_print" => op_print(),
|
|
_ => op,
|
|
},
|
|
state = |state, options| {
|
|
state.put(options.sender);
|
|
},
|
|
);
|
|
|
|
#[op2(async)]
|
|
pub async fn op_jupyter_broadcast(
|
|
state: Rc<RefCell<OpState>>,
|
|
#[string] message_type: String,
|
|
#[serde] content: serde_json::Value,
|
|
#[serde] metadata: serde_json::Value,
|
|
#[serde] buffers: Vec<deno_core::JsBuffer>,
|
|
) -> Result<(), AnyError> {
|
|
let (iopub_connection, last_execution_request) = {
|
|
let s = state.borrow();
|
|
|
|
(
|
|
s.borrow::<Arc<Mutex<KernelIoPubConnection>>>().clone(),
|
|
s.borrow::<Rc<RefCell<Option<JupyterMessage>>>>().clone(),
|
|
)
|
|
};
|
|
|
|
let maybe_last_request = last_execution_request.borrow().clone();
|
|
if let Some(last_request) = maybe_last_request {
|
|
let content = JupyterMessageContent::from_type_and_content(
|
|
&message_type,
|
|
content.clone(),
|
|
)
|
|
.map_err(|err| {
|
|
log::error!(
|
|
"Error deserializing content from jupyter.broadcast, message_type: {}:\n\n{}\n\n{}",
|
|
&message_type,
|
|
content,
|
|
err
|
|
);
|
|
err
|
|
})?;
|
|
|
|
let mut jupyter_message = JupyterMessage::new(content, Some(&last_request));
|
|
|
|
jupyter_message.metadata = metadata;
|
|
jupyter_message.buffers =
|
|
buffers.into_iter().map(|b| b.to_vec().into()).collect();
|
|
jupyter_message.set_parent(last_request);
|
|
|
|
(iopub_connection.lock().await)
|
|
.send(jupyter_message)
|
|
.await?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[op2(fast)]
|
|
pub fn op_print(
|
|
state: &mut OpState,
|
|
#[string] msg: &str,
|
|
is_err: bool,
|
|
) -> Result<(), AnyError> {
|
|
let sender = state.borrow_mut::<mpsc::UnboundedSender<StreamContent>>();
|
|
|
|
if is_err {
|
|
if let Err(err) = sender.send(StreamContent::stderr(msg.into())) {
|
|
log::error!("Failed to send stderr message: {}", err);
|
|
}
|
|
return Ok(());
|
|
}
|
|
|
|
if let Err(err) = sender.send(StreamContent::stdout(msg.into())) {
|
|
log::error!("Failed to send stdout message: {}", err);
|
|
}
|
|
Ok(())
|
|
}
|