-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
basic api for cell execution and jupyter extension support
Signed-off-by: Jonah Iden <[email protected]>
- Loading branch information
1 parent
eb95328
commit a79e28a
Showing
22 changed files
with
1,024 additions
and
110 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
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
120 changes: 120 additions & 0 deletions
120
packages/notebook/src/browser/service/notebook-execution-service.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,120 @@ | ||
// ***************************************************************************** | ||
// Copyright (C) 2023 TypeFox and others. | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License v. 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0. | ||
// | ||
// This Source Code may also be made available under the following Secondary | ||
// Licenses when the conditions for such availability set forth in the Eclipse | ||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
// with the GNU Classpath Exception which is available at | ||
// https://www.gnu.org/software/classpath/license.html. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 | ||
// ***************************************************************************** | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { inject, injectable } from '@theia/core/shared/inversify'; | ||
import { CellExecution, NotebookExecutionStateService } from '../service/notebook-execution-state-service'; | ||
import { CellKind, NotebookCellExecutionState } from '../../common'; | ||
import { NotebookCellModel } from '../view-model/notebook-cell-model'; | ||
import { NotebookModel } from '../view-model/notebook-model'; | ||
import { NotebookKernelService } from './notebook-kernel-service'; | ||
import { Disposable } from '@theia/core'; | ||
|
||
export interface CellExecutionParticipant { | ||
onWillExecuteCell(executions: CellExecution[]): Promise<void>; | ||
} | ||
|
||
@injectable() | ||
export class NotebookExecutionService { | ||
|
||
@inject(NotebookExecutionStateService) | ||
protected notebookExecutionStateService: NotebookExecutionStateService; | ||
|
||
@inject(NotebookKernelService) | ||
protected notebookKernelService: NotebookKernelService; | ||
|
||
private readonly cellExecutionParticipants = new Set<CellExecutionParticipant>(); | ||
|
||
async executeNotebookCells(notebook: NotebookModel, cells: Iterable<NotebookCellModel>): Promise<void> { | ||
const cellsArr = Array.from(cells) | ||
.filter(c => c.cellKind === CellKind.Code); | ||
if (!cellsArr.length) { | ||
return; | ||
} | ||
|
||
console.debug(`NotebookExecutionService#executeNotebookCells ${JSON.stringify(cellsArr.map(c => c.handle))}`); | ||
|
||
// create cell executions | ||
const cellExecutions: [NotebookCellModel, CellExecution][] = []; | ||
for (const cell of cellsArr) { | ||
const cellExe = this.notebookExecutionStateService.getCellExecution(cell.uri); | ||
if (!cellExe) { | ||
cellExecutions.push([cell, this.notebookExecutionStateService.createCellExecution(notebook.uri, cell.handle)]); | ||
} | ||
} | ||
|
||
const kernel = this.notebookKernelService.getSelectedOrSuggestedKernel(notebook); | ||
|
||
if (!kernel) { | ||
// clear all pending cell executions | ||
cellExecutions.forEach(cellExe => cellExe[1].complete({})); | ||
return; | ||
} | ||
|
||
// filter cell executions based on selected kernel | ||
const validCellExecutions: CellExecution[] = []; | ||
for (const [cell, cellExecution] of cellExecutions) { | ||
if (!kernel.supportedLanguages.includes(cell.language)) { | ||
cellExecution.complete({}); | ||
} else { | ||
validCellExecutions.push(cellExecution); | ||
} | ||
} | ||
|
||
// request execution | ||
if (validCellExecutions.length > 0) { | ||
await this.runExecutionParticipants(validCellExecutions); | ||
|
||
this.notebookKernelService.selectKernelForNotebook(kernel, notebook); | ||
await kernel.executeNotebookCellsRequest(notebook.uri, validCellExecutions.map(c => c.cellHandle)); | ||
// the connecting state can change before the kernel resolves executeNotebookCellsRequest | ||
const unconfirmed = validCellExecutions.filter(exe => exe.state === NotebookCellExecutionState.Unconfirmed); | ||
if (unconfirmed.length) { | ||
console.debug(`NotebookExecutionService#executeNotebookCells completing unconfirmed executions ${JSON.stringify(unconfirmed.map(exe => exe.cellHandle))}`); | ||
unconfirmed.forEach(exe => exe.complete({})); | ||
} | ||
} | ||
} | ||
|
||
registerExecutionParticipant(participant: CellExecutionParticipant): Disposable { | ||
this.cellExecutionParticipants.add(participant); | ||
return Disposable.create(() => this.cellExecutionParticipants.delete(participant)); | ||
} | ||
|
||
private async runExecutionParticipants(executions: CellExecution[]): Promise<void> { | ||
for (const participant of this.cellExecutionParticipants) { | ||
await participant.onWillExecuteCell(executions); | ||
} | ||
return; | ||
} | ||
|
||
async cancelNotebookCellHandles(notebook: NotebookModel, cells: Iterable<number>): Promise<void> { | ||
const cellsArr = Array.from(cells); | ||
console.debug(`NotebookExecutionService#cancelNotebookCellHandles ${JSON.stringify(cellsArr)}`); | ||
const kernel = this.notebookKernelService.getSelectedOrSuggestedKernel(notebook); | ||
if (kernel) { | ||
await kernel.cancelNotebookCellExecution(notebook.uri, cellsArr); | ||
|
||
} | ||
} | ||
|
||
async cancelNotebookCells(notebook: NotebookModel, cells: Iterable<NotebookCellModel>): Promise<void> { | ||
this.cancelNotebookCellHandles(notebook, Array.from(cells, cell => cell.handle)); | ||
} | ||
} |
Oops, something went wrong.