-
Notifications
You must be signed in to change notification settings - Fork 294
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10155 from microsoft/kernelConnectionWrapperStep2
- Loading branch information
Showing
9 changed files
with
882 additions
and
564 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
71 changes: 71 additions & 0 deletions
71
src/notebooks/execution/cellExecutionMessageHandlerService.ts
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,71 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import { Kernel, KernelMessage } from '@jupyterlab/services'; | ||
import { NotebookCell, NotebookCellExecution, NotebookController, NotebookDocument, workspace } from 'vscode'; | ||
import { ITracebackFormatter } from '../../kernels/types'; | ||
import { IApplicationShell } from '../../platform/common/application/types'; | ||
import { disposeAllDisposables } from '../../platform/common/helpers'; | ||
import { IDisposable, IExtensionContext } from '../../platform/common/types'; | ||
import { CellOutputDisplayIdTracker } from './cellDisplayIdTracker'; | ||
import { CellExecutionMessageHandler } from './cellExecutionMessageHandler'; | ||
|
||
export class CellExecutionMessageHandlerService { | ||
private readonly disposables: IDisposable[] = []; | ||
private notebook?: NotebookDocument; | ||
private readonly messageHandlers = new WeakMap<NotebookCell, CellExecutionMessageHandler>(); | ||
constructor( | ||
private readonly appShell: IApplicationShell, | ||
private readonly controller: NotebookController, | ||
private readonly outputDisplayIdTracker: CellOutputDisplayIdTracker, | ||
private readonly context: IExtensionContext, | ||
private readonly formatters: ITracebackFormatter[] | ||
) { | ||
workspace.onDidChangeNotebookDocument( | ||
(e) => { | ||
if (e.notebook !== this.notebook) { | ||
return; | ||
} | ||
e.contentChanges.forEach((change) => | ||
// If the cell is deleted, then dispose the corresponding handler. | ||
change.removedCells.forEach((cell) => this.messageHandlers.get(cell)?.dispose()) | ||
); | ||
}, | ||
this, | ||
this.disposables | ||
); | ||
} | ||
dispose() { | ||
disposeAllDisposables(this.disposables); | ||
if (this.notebook) { | ||
this.notebook.getCells().forEach((cell) => this.messageHandlers.get(cell)?.dispose()); | ||
} | ||
} | ||
public registerListener( | ||
cell: NotebookCell, | ||
options: { | ||
kernel: Kernel.IKernelConnection; | ||
request: Kernel.IShellFuture<KernelMessage.IExecuteRequestMsg, KernelMessage.IExecuteReplyMsg>; | ||
cellExecution: NotebookCellExecution; | ||
onErrorHandlingExecuteRequestIOPubMessage: (error: Error) => void; | ||
} | ||
): CellExecutionMessageHandler { | ||
this.notebook = cell.notebook; | ||
// Always dispose any previous handlers & create new ones. | ||
this.messageHandlers.get(cell)?.dispose(); | ||
const handler = new CellExecutionMessageHandler( | ||
cell, | ||
this.appShell, | ||
this.controller, | ||
this.outputDisplayIdTracker, | ||
this.context, | ||
this.formatters, | ||
options.kernel, | ||
options.request, | ||
options.cellExecution | ||
); | ||
// This object must be kept in memory has it monitors the kernel messages. | ||
this.messageHandlers.set(cell, handler); | ||
return handler; | ||
} | ||
} |
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
3 changes: 3 additions & 0 deletions
3
src/platform/common/refBool.node.ts → src/platform/common/refBool.ts
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
39 changes: 39 additions & 0 deletions
39
src/test/datascience/widgets/notebooks/button_widget_comm_msg.ipynb
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,39 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import ipywidgets as widgets\n", | ||
"from IPython.display import display\n", | ||
"button = widgets.Button(description=\"Click Me!\")\n", | ||
"\n", | ||
"display(button)\n", | ||
"\n", | ||
"def on_button_clicked(b):\n", | ||
" print(\"Button clicked.\")\n", | ||
"\n", | ||
"button.on_click(on_button_clicked)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"display(button)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"language_info": { | ||
"name": "python" | ||
}, | ||
"orig_nbformat": 4 | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
47 changes: 47 additions & 0 deletions
47
src/test/datascience/widgets/notebooks/button_widget_comm_msg_matplotlib.ipynb
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,47 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%matplotlib widget\n", | ||
"from IPython.display import display\n", | ||
"from ipywidgets import widgets\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"import numpy as np\n", | ||
"\n", | ||
"\n", | ||
"button = widgets.Button(description=\"Click Me!\")\n", | ||
"\n", | ||
"def on_button_clicked(b):\n", | ||
"\tX = np.linspace(0, 2*np.pi)\n", | ||
"\tY = np.sin(X)\n", | ||
"\n", | ||
"\t_, ax = plt.subplots()\n", | ||
"\tax.plot(X, Y)\n", | ||
"\n", | ||
"display(button)\n", | ||
"button.on_click(on_button_clicked)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"display(button)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"language_info": { | ||
"name": "python" | ||
}, | ||
"orig_nbformat": 4 | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
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