Skip to content

Commit

Permalink
moving simple output handler into handlers/output
Browse files Browse the repository at this point in the history
  • Loading branch information
kafonek committed Dec 21, 2023
1 parent e566d35 commit e17337f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub mod outputs;
// export Handlers
pub use debug::DebugHandler;
pub use msg_count::MessageCountHandler;
pub use outputs::OutputHandler;
pub use outputs::SimpleOutputHandler;

#[async_trait::async_trait]
pub trait Handler: Debug + Send + Sync {
Expand Down
39 changes: 38 additions & 1 deletion src/handlers/outputs.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use tokio::sync::RwLock;

use crate::handlers::Handler;
use crate::jupyter::response::Response;

Check warning on line 4 in src/handlers/outputs.rs

View workflow job for this annotation

GitHub Actions / lint

Diff in /home/runner/work/kernel-sidecar-rs/kernel-sidecar-rs/src/handlers/outputs.rs
use crate::notebook::Output;

use std::fmt::Debug;
use std::{fmt::Debug, sync::Arc};

#[async_trait::async_trait]
pub trait OutputHandler: Handler + Debug + Send + Sync {
// expect a struct implementing this to have a .clear_on_next_output bool attribute
async fn add_cell_content(&self, content: Output);
async fn clear_cell_content(&self);
async fn sync_display_data(&self, content: Output);

async fn handle_output(&self, msg: &Response) {
match msg {
Expand Down Expand Up @@ -43,3 +47,36 @@ impl<T: OutputHandler + Send + Sync> Handler for T {
self.handle_output(msg).await;
}
}

#[derive(Debug, Clone)]
pub struct SimpleOutputHandler {
pub output: Arc<RwLock<Vec<Output>>>,
}

impl SimpleOutputHandler {
pub fn new() -> Self {
Self {
output: Arc::new(RwLock::new(Vec::new())),
}
}
}

#[async_trait::async_trait]
impl OutputHandler for SimpleOutputHandler {
async fn add_cell_content(&self, content: Output) {
self.output.write().await.push(content);
println!("add_cell_content");
}

async fn clear_cell_content(&self) {
self.output.write().await.clear();
println!("clear_cell_content");
}

async fn sync_display_data(&self, _content: Output) {
// Don't do anything for sync display data in SimpleOutputHandler
// we aren't keeping any reference to the full Notebook document in order to
// update the display data by id outside of this "current cell" context
()
}
}
32 changes: 2 additions & 30 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,11 @@ use tokio::signal::unix::{signal, SignalKind};
use tokio::time::sleep;

use indoc::indoc;
use kernel_sidecar_rs::notebook::Output;

use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;

use kernel_sidecar_rs::handlers::OutputHandler;

#[derive(Debug, Clone)]
struct SimpleOutputHandler {
pub output: Arc<RwLock<Vec<Output>>>,
}

impl SimpleOutputHandler {
pub fn new() -> Self {
Self {
output: Arc::new(RwLock::new(Vec::new())),
}
}
}

#[async_trait::async_trait]
impl OutputHandler for SimpleOutputHandler {
async fn add_cell_content(&self, content: Output) {
self.output.write().await.push(content);
println!("add_cell_content");
}

async fn clear_cell_content(&self) {
self.output.write().await.clear();
println!("clear_cell_content");
}
}
use kernel_sidecar_rs::handlers::SimpleOutputHandler;

#[tokio::main]
async fn main() {
Expand All @@ -61,7 +34,6 @@ async fn main() {
// let action = client.kernel_info_request(handlers).await;
let code = indoc! {r#"
from IPython.display import clear_output
import time
print("Before Clear Output")
Expand Down
31 changes: 2 additions & 29 deletions tests/test_outputs.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,13 @@
#![cfg(feature = "test_ipython")]
use indoc::indoc;
use kernel_sidecar_rs::handlers::{Handler, OutputHandler};
use kernel_sidecar_rs::handlers::{Handler, SimpleOutputHandler};
use kernel_sidecar_rs::jupyter::iopub_content::stream::StreamName;
use kernel_sidecar_rs::notebook::Output;

use std::sync::Arc;
use tokio::sync::RwLock;

mod test_utils;
use test_utils::start_kernel;

#[derive(Debug, Clone)]
struct SimpleOutputHandler {
pub output: Arc<RwLock<Vec<Output>>>,
}

impl SimpleOutputHandler {
pub fn new() -> Self {
Self {
output: Arc::new(RwLock::new(Vec::new())),
}
}
}

#[async_trait::async_trait]
impl OutputHandler for SimpleOutputHandler {
async fn add_cell_content(&self, content: Output) {
self.output.write().await.push(content);
println!("add_cell_content");
}

async fn clear_cell_content(&self) {
self.output.write().await.clear();
println!("clear_cell_content");
}
}

#[tokio::test]
async fn test_mixed_outputs() {
// Show that stream and execute result can be mixed
Expand Down

0 comments on commit e17337f

Please sign in to comment.