-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support requesting kernel runtime states (#15371)
* Support requesting kernel runtime states * lint
- Loading branch information
Showing
3 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
import type { KernelMessage } from '@jupyterlab/services'; | ||
import * as vscode from 'vscode'; | ||
import { IKernel, IKernelProvider } from '../../kernels/types'; | ||
import { execCodeInBackgroundThread } from '../api/kernels/backgroundExecution'; | ||
import { ServiceContainer } from '../../platform/ioc/container'; | ||
import { IControllerRegistration } from '../../notebooks/controllers/types'; | ||
|
||
export async function activate(context: vscode.ExtensionContext): Promise<void> { | ||
context.subscriptions.push( | ||
vscode.commands.registerCommand('jupyter.listPipPackages', async (uri) => { | ||
const documentUri = uri ?? vscode.window.activeNotebookEditor?.notebook.uri; | ||
if (documentUri) { | ||
const kernelProvider = ServiceContainer.instance.get<IKernelProvider>(IKernelProvider); | ||
const kernel = await kernelProvider.get(documentUri); | ||
if (kernel) { | ||
const token = new vscode.CancellationTokenSource().token; | ||
try { | ||
const result = await sendPipListRequest(kernel, token); | ||
if (Array.isArray(result.content)) { | ||
return result.content; | ||
} | ||
} catch (_) { | ||
// ignore | ||
} | ||
} | ||
} | ||
|
||
return []; | ||
}) | ||
); | ||
|
||
context.subscriptions.push( | ||
vscode.commands.registerCommand('jupyter.listVariables', async (uri) => { | ||
const documentUri = uri ?? vscode.window.activeNotebookEditor?.notebook.uri; | ||
|
||
if (!documentUri) { | ||
return []; | ||
} | ||
|
||
const document = vscode.workspace.notebookDocuments.find( | ||
(item) => item.uri.toString() === documentUri.toString() | ||
); | ||
|
||
if (!document) { | ||
return []; | ||
} | ||
|
||
const controllerRegistry = ServiceContainer.instance.get<IControllerRegistration>(IControllerRegistration); | ||
const controller = controllerRegistry.getSelected(document); | ||
if (!controller) { | ||
return []; | ||
} | ||
|
||
const variablesProvider = controller.controller.variableProvider; | ||
|
||
if (!variablesProvider) { | ||
return []; | ||
} | ||
|
||
const token = new vscode.CancellationTokenSource().token; | ||
const variables = variablesProvider.provideVariables( | ||
document, | ||
undefined, | ||
vscode.NotebookVariablesRequestKind.Named, | ||
0, | ||
token | ||
); | ||
|
||
const resolvedVariables = []; | ||
for await (const variable of variables) { | ||
resolvedVariables.push(variable.variable); | ||
} | ||
return resolvedVariables; | ||
}) | ||
); | ||
} | ||
|
||
async function sendPipListRequest(kernel: IKernel, token: vscode.CancellationToken) { | ||
const codeToExecute = `import subprocess | ||
proc = subprocess.Popen(["pip", "list", "--format", "json"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) | ||
stdout, stderr = proc.communicate() | ||
return stdout | ||
`.split('\n'); | ||
|
||
const content = await execCodeInBackgroundThread<KernelMessage.IInspectReplyMsg['content']>( | ||
kernel, | ||
codeToExecute, | ||
token | ||
); | ||
return { content } as KernelMessage.IInspectReplyMsg; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
export function deactivate() {} |