Last active
November 29, 2024 19:31
-
-
Save 2514millerj/7acb92d862f0ca40b9a57b66cad18db7 to your computer and use it in GitHub Desktop.
Visual Studio Code Jupyter Notebook Watcher Extension API Demo - V1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const vscode = require('vscode'); | |
| function sleep(ms) { | |
| return new Promise(resolve => setTimeout(resolve, ms)); | |
| } | |
| async function handleNotebookKernel(api, uri, context) { | |
| let kernelFound = false; | |
| let kernel = undefined; | |
| // Wait until a kernel is running. This requires the user to run the kernel, having the notebook document open in the workspace is not enough | |
| while(!kernelFound) { | |
| // Get the kernel for the notebook document URI passed to the function | |
| kernel = await api.kernels.getKernel(uri); | |
| if (kernel !== undefined) { | |
| kernelFound = true; | |
| } | |
| await sleep(1000); | |
| } | |
| // Notebook is open and the kernel is running. Add logic to handle the kernel’s status change | |
| context.subscriptions.push(kernel.onDidChangeStatus((e) => { | |
| vscode.window.showInformationMessage(e); | |
| })); | |
| } | |
| async function notebookWatcher(context) { | |
| // Get Jupyter Extension public API implementation and make sure the extension is installed | |
| const jupyterExt = vscode.extensions.getExtension('ms-toolsai.jupyter'); | |
| if (jupyterExt) { | |
| // Wait for the extension to go through its activation function | |
| const api = await jupyterExt.activate(); | |
| let existingDocs = []; | |
| // Iterate forever and continue checking for new notebook documents in the workspace | |
| while(true) { | |
| for (const document of vscode.workspace.notebookDocuments) { | |
| if (!existingDocs.includes(document.uri)) { | |
| // If we haven’t seen this notebook yet, pass it to a handler function | |
| handleNotebookKernel(api, document.uri, context); | |
| existingDocs.push(document.uri); | |
| } | |
| } | |
| await sleep(1000); | |
| } | |
| } | |
| } | |
| /** | |
| * @param {vscode.ExtensionContext} context | |
| */ | |
| async function activate(context) { | |
| //start async Jupyter notebook watcher for when a user opens new notebooks | |
| notebookWatcher(context); | |
| } | |
| // This method is called when your extension is deactivated | |
| function deactivate() {} | |
| module.exports = { | |
| activate, | |
| deactivate | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the v1 reference extension that monitors Jupyter Notebook kernels implemented by MeerkatIO to trigger notifications on cell executions.