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