Skip to content

Commit

Permalink
update comments in main.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
kafonek committed Dec 22, 2023
1 parent a0a42c8 commit 0e051f2
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::time::Duration;

#[tokio::main]
async fn main() {
// Effectively create an in-memory Notebook, not reading from disk
// Create a blank in-memory Notebook, not reading existing file from disk
let builder = NotebookBuilder::new();

// Start ipykernel child process
Expand All @@ -24,22 +24,29 @@ async fn main() {
// small sleep to make sure iopub is connected,
sleep(Duration::from_millis(50)).await;

// Add a new cell to the Notebook, it will make a random cell id. Returns Cell object
// Add a new cell to the Notebook. Assigns random cell id. Returns cloned Cell object.
// If thinking ahead towards CRDT's, could think of this as "dirty" (not synced to others)
// but we're only using it to send source code in execute request, no big deal.
let cell = builder.add_code_cell("2 + 3").await;

let debug_handler = DebugHandler::new(); // Just for debug, prints out all ZMQ messages
let msg_count_handler = MessageCountHandler::new(); // Just for debug, prints count of msg type
let output_handler = builder.output_handler(cell.id()); // Updates in-memory Notebook with cell output
// Just for debug, prints out all ZMQ messages
let debug_handler = DebugHandler::new();
// Just for debug, prints count of msg types at the end of script
let msg_count_handler = MessageCountHandler::new();
// Updates in-memory builder Notebook with cell output
let output_handler = builder.output_handler(cell.id());
let handlers = vec![
Arc::new(debug_handler) as Arc<dyn Handler>,
Arc::new(msg_count_handler.clone()) as Arc<dyn Handler>,
Arc::new(output_handler) as Arc<dyn Handler>,
];

// Send the cell source code over as an execute request.
// The BuilderOutputHandler will update the in-memory Notebook with cell output
// Send the cell source code over as an execute request, every ZMQ response gets processed
// by all three handlers sequentially
let action = client.execute_request(cell.source(), handlers).await;

// Signal handling to support ctrl-c in the off chance something goes wrong and this script
// never completes (missing expected shell/iopub messages for status or execute_reply?)
let mut sigint = signal(SignalKind::interrupt()).expect("Failed to set up signal handler");

tokio::select! {
Expand All @@ -50,9 +57,10 @@ async fn main() {
println!("SIGINT received");
}
}
// Debug: print count of ZMQ response message types
println!("Message counts: {:?}", msg_count_handler.counts);
// Print out in-memory Notebook cell output
// Print out in-memory Notebook cell (source and outputs)
println!("Cell: {:?}", builder.get_cell(cell.id()).await);
// Save in-memory Notebook to disk
// See what it looks like when saving in-memory Notebook to disk (serde for serialization)
builder.save("test.ipynb").await;
}

0 comments on commit 0e051f2

Please sign in to comment.