|
| 1 | +/* ----------------------------------------------------------------------------- |
| 2 | +| Copyright (c) Jupyter Development Team. |
| 3 | +| Distributed under the terms of the Modified BSD License. |
| 4 | +|----------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { type ICodeCellModel, type MarkdownCell } from '@jupyterlab/cells'; |
| 7 | +import { URLExt } from '@jupyterlab/coreutils'; |
| 8 | +import { INotebookCellExecutor } from '@jupyterlab/notebook'; |
| 9 | +import { Contents, ServerConnection } from '@jupyterlab/services'; |
| 10 | +import { nullTranslator } from '@jupyterlab/translation'; |
| 11 | +import { ICollaborativeDrive } from './tokens'; |
| 12 | +import { Dialog, showDialog } from '@jupyterlab/apputils'; |
| 13 | + |
| 14 | +export class NotebookCellServerExecutor implements INotebookCellExecutor { |
| 15 | + private _contents: Contents.IManager; |
| 16 | + private _drive: ICollaborativeDrive; |
| 17 | + private _serverSettings: ServerConnection.ISettings; |
| 18 | + |
| 19 | + constructor(options: { |
| 20 | + contents: Contents.IManager; |
| 21 | + drive: ICollaborativeDrive; |
| 22 | + serverSettings?: ServerConnection.ISettings; |
| 23 | + }) { |
| 24 | + this._contents = options.contents; |
| 25 | + this._drive = options.drive; |
| 26 | + this._serverSettings = |
| 27 | + options.serverSettings ?? ServerConnection.makeSettings(); |
| 28 | + } |
| 29 | + |
| 30 | + async runCell({ |
| 31 | + cell, |
| 32 | + notebook, |
| 33 | + notebookConfig, |
| 34 | + onCellExecuted, |
| 35 | + onCellExecutionScheduled, |
| 36 | + sessionContext, |
| 37 | + sessionDialogs, |
| 38 | + translator |
| 39 | + }: INotebookCellExecutor.IRunCellOptions): Promise<boolean> { |
| 40 | + translator = translator ?? nullTranslator; |
| 41 | + const trans = translator.load('jupyterlab'); |
| 42 | + |
| 43 | + switch (cell.model.type) { |
| 44 | + case 'markdown': |
| 45 | + (cell as MarkdownCell).rendered = true; |
| 46 | + cell.inputHidden = false; |
| 47 | + onCellExecuted({ cell, success: true }); |
| 48 | + break; |
| 49 | + case 'code': |
| 50 | + if (sessionContext) { |
| 51 | + if (sessionContext.isTerminating) { |
| 52 | + await showDialog({ |
| 53 | + title: trans.__('Kernel Terminating'), |
| 54 | + body: trans.__( |
| 55 | + 'The kernel for %1 appears to be terminating. You can not run any cell for now.', |
| 56 | + sessionContext.session?.path |
| 57 | + ), |
| 58 | + buttons: [Dialog.okButton()] |
| 59 | + }); |
| 60 | + break; |
| 61 | + } |
| 62 | + if (sessionContext.pendingInput) { |
| 63 | + await showDialog({ |
| 64 | + title: trans.__('Cell not executed due to pending input'), |
| 65 | + body: trans.__( |
| 66 | + 'The cell has not been executed to avoid kernel deadlock as there is another pending input! Submit your pending input and try again.' |
| 67 | + ), |
| 68 | + buttons: [Dialog.okButton()] |
| 69 | + }); |
| 70 | + return false; |
| 71 | + } |
| 72 | + if (sessionContext.hasNoKernel) { |
| 73 | + const shouldSelect = await sessionContext.startKernel(); |
| 74 | + if (shouldSelect && sessionDialogs) { |
| 75 | + await sessionDialogs.selectKernel(sessionContext); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if (sessionContext.hasNoKernel) { |
| 80 | + cell.model.sharedModel.transact(() => { |
| 81 | + (cell.model as ICodeCellModel).clearExecution(); |
| 82 | + }); |
| 83 | + return true; |
| 84 | + } |
| 85 | + |
| 86 | + const kernelId = sessionContext?.session?.kernel?.id; |
| 87 | + const apiURL = URLExt.join( |
| 88 | + this._serverSettings.baseUrl, |
| 89 | + `api/kernels/${kernelId}/execute` |
| 90 | + ); |
| 91 | + const cellId = cell.model.sharedModel.getId(); |
| 92 | + |
| 93 | + // jupyverse case - it is undefined in jupyter-server |
| 94 | + const fileId = notebook.sharedModel.getState('file_id') ?? ''; |
| 95 | + let documentId = `json:notebook:${fileId}`; |
| 96 | + if (!fileId) { |
| 97 | + if ( |
| 98 | + this._contents.driveName(sessionContext.path) === this._drive.name |
| 99 | + ) { |
| 100 | + const localPath = this._contents.localPath(sessionContext.path); |
| 101 | + documentId = |
| 102 | + this._drive.getRoomName({ |
| 103 | + localPath: localPath, |
| 104 | + format: 'json', |
| 105 | + type: 'notebook' |
| 106 | + }) ?? ''; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + const init = { |
| 111 | + method: 'POST', |
| 112 | + body: JSON.stringify({ cell_id: cellId, document_id: documentId }) |
| 113 | + }; |
| 114 | + onCellExecutionScheduled({ cell }); |
| 115 | + let success = false; |
| 116 | + try { |
| 117 | + // FIXME quid of deletedCells and timing record |
| 118 | + const response = await ServerConnection.makeRequest( |
| 119 | + apiURL, |
| 120 | + init, |
| 121 | + this._serverSettings |
| 122 | + ); |
| 123 | + const data = await response.json(); |
| 124 | + success = data['status'] === 'ok'; |
| 125 | + } catch (error: any) { |
| 126 | + if (cell.isDisposed) { |
| 127 | + return false; |
| 128 | + } else { |
| 129 | + onCellExecuted({ cell, success: false, error }); |
| 130 | + throw await ServerConnection.ResponseError.create(error); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + onCellExecuted({ cell, success }); |
| 135 | + |
| 136 | + return true; |
| 137 | + } |
| 138 | + cell.model.sharedModel.transact(() => { |
| 139 | + (cell.model as ICodeCellModel).clearExecution(); |
| 140 | + }, false); |
| 141 | + break; |
| 142 | + default: |
| 143 | + break; |
| 144 | + } |
| 145 | + return Promise.resolve(true); |
| 146 | + } |
| 147 | +} |
0 commit comments