Skip to content

Commit

Permalink
hacking with Kyle, emit outputs to frontend in really naive serializa…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
kafonek authored and rgbkrk committed Jan 15, 2024
1 parent 4efb993 commit 8b57149
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@ use tauri::{Manager, State, Window};
// Structures for the notebook and cells
use log::{debug, info};


use kernel_sidecar::client::Client;
use kernel_sidecar::{client::Client, handlers::SimpleOutputHandler};

use kernel_sidecar::handlers::{DebugHandler, Handler};
use kernel_sidecar::kernels::JupyterKernel;
use kernel_sidecar::notebook::Notebook;



// AppState that holds the mapping from Window to Notebook
struct AppState {
// Notebooks are just the document model, and could exist even if there's no underlying kernel
Expand Down Expand Up @@ -56,7 +53,8 @@ impl AppState {
}

// Perform the cell execution within the AppState context
async fn execute_cell(&self, window_id: &str, cell_id: &str) -> bool {
async fn execute_cell(&self, window: Window, cell_id: &str) -> bool {
let window_id = window.label(); // Use the window label as a unique identifier
debug!(
"Executing cell with ID: {} in window with ID: {}",
cell_id, window_id
Expand All @@ -76,10 +74,16 @@ impl AppState {

let source = cell.get_source();
let debug_handler = Arc::new(Mutex::new(DebugHandler::new()));
let handlers: Vec<Arc<Mutex<dyn Handler>>> = vec![debug_handler.clone()];
let output_handler = Arc::new(Mutex::new(SimpleOutputHandler::new()));
let handlers: Vec<Arc<Mutex<dyn Handler>>> =
vec![debug_handler.clone(), output_handler.clone()];

let action = kernel_client.execute_request(source, handlers);
let action = kernel_client.execute_request(source, handlers).await;
action.await;

let finished_output = &output_handler.lock().await.output;

window.emit("output", finished_output.clone()).unwrap();
return true;
}
}
Expand Down Expand Up @@ -130,8 +134,7 @@ async fn execute_cell(
window: Window,
cell_id: &str,
) -> Result<bool, String> {
let window_id = window.label(); // Use the window label as a unique identifier
Ok(state.execute_cell(window_id, cell_id).await)
Ok(state.execute_cell(window, cell_id).await)
}

#[tauri::command]
Expand Down
6 changes: 6 additions & 0 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import "./index.css";
import { emit, listen } from '@tauri-apps/api/event'

// listen to the `click` event and get a function to remove the event listener
// there's also a `once` function that subscribes to an event and automatically unsubscribes the listener on the first event
const unlisten = await listen('output', (event) => {
console.log(event)
})

ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<React.StrictMode>
Expand Down

0 comments on commit 8b57149

Please sign in to comment.