Updates on the MeerkatIO Jupyter Notebook Watcher To Monitor Notebook Execution

Posted: November 29th, 2024

MeerkatIO version 1.9.0 includes a revamped Jupyter Notebook Watcher that significantly simplifies the monitoring for Jupyter Notebooks and triggers more accurate alerts with increased context.

Jupyter Notebook Visual Studio Code Extension API Development Tutorial

Prerequisites

Before we begin, ensure you have the following installed:

  1. Visual Studio Code
  2. Python
  3. Jupyter Notebook Kernel
  4. Jupyter Extension for VS Code

Setting Up Your VS Code Environment

  1. Create a Jupyter Notebook: Open the Command Palette (Ctrl+Shift+P), type Jupyter: Create New Blank Notebook, and select it. This will create a new Jupyter notebook file (.ipynb).

Initial Jupyter Notebook Watcher Drawbacks

My first solution included implementing the Jupyter extension’s API which provided information on kernel state, but was very limited beyond basic state updates. This solution is documented here. While it is still a valid solution, the newest version of the Jupyter Notebook Watcher gets access to many more execution details that can be used to improve notifications and analysis by the MeerkatIO extension.

Jupyter Notebook Watcher V2 Implementation

I recently learned that the VS Code Javascript SDK provides a listener, onDidChangeNotebookDocument, which provides on change data for all active notebooks in the VS Code window. The payload for this listener is incredibly complete, and makes monitoring Jupyter notebook execution a simple task. I plan on taking advantage of more of the attributes available in the callback payload, such as the output of the cell, in order to provide more value to the MeerkatIO notifications and notification analytics.

Structure of NotebookDocumentChangeEvent

Here’s what the object typically includes:

document: The NotebookDocument that was changed.
metadata: Optional. Contains metadata changes for the notebook.
cells: Optional. An array of changes to the cells in the notebook. Each change may include:
    index: The index of the cell in the notebook.
    items: The actual cells that were added, removed, or replaced.
cellMetadata: Optional. Contains metadata changes for individual cells.
contentChanges: Optional. Represents changes in the notebook’s content (e.g., edits within a cell).

Sample Jupyter Notebook Watcher Code

async function notebookWatcher() {
	vscode.workspace.onDidChangeNotebookDocument((event) => {
		for(let cell of event.cellChanges) {
			if (cell.executionSummary?.timing) {
				//Cell Execution Completed When "timing" is available
				let startTime = new Date(cell.executionSummary.timing.startTime);
				let endTime = new Date(cell.executionSummary.timing.endTime);
				let message = `
                    Cell #${cell.cell.index} in notebook ${event.notebook.uri.path.split("/").pop()} ${cell.executionSummary.success ? "Completed Successfully" : "Execution Failed"}
                    
                    Start Time: ${startTime}
                    End Time: ${endTime}
                `;
				
        vscode.window.showInformationMessage(message);
			}
		}
	});
}

Full Code Example

Check out this GitHub Gist for the complete code for this example extension:

https://gist.github.com/2514millerj/7acb92d862f0ca40b9a57b66cad18db7

Example Use Case

MeerkatIO extends this example to monitor the state of execution of Jupyter Notebook cells in order to trigger personal notifications according to the user's preferences. If the cell execution data matches the MeerkatIO extension settings, a notification will be triggered by the MeerkatIO extension to the user's preferred notification channel.

Happy Hacking,
Justin
MeerkatIO.com


Sign up for MeerkatIO today to reclaim your free time and boost your productivity. With MeerkatIO, you can enjoy the peace of mind that comes from knowing you're not missing a beat while your code is running. Start your journey to a more efficient work-from-home experience today!

Back to Blog