@jupyterlab/notebook 4.0.0-alpha.21 → 4.0.0-alpha.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/actions.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ISessionContext } from '@jupyterlab/apputils';
1
+ import { ISessionContext, ISessionContextDialogs } from '@jupyterlab/apputils';
2
2
  import { Cell } from '@jupyterlab/cells';
3
3
  import * as nbformat from '@jupyterlab/nbformat';
4
4
  import { KernelMessage } from '@jupyterlab/services';
@@ -173,8 +173,9 @@ export declare namespace NotebookActions {
173
173
  * Run the selected cell(s).
174
174
  *
175
175
  * @param notebook - The target notebook widget.
176
- *
177
- * @param sessionContext - The optional client session object.
176
+ * @param sessionContext - The client session object.
177
+ * @param sessionDialogs - The session dialogs.
178
+ * @param translator - The application translator.
178
179
  *
179
180
  * #### Notes
180
181
  * The last selected cell will be activated, but not scrolled into view.
@@ -182,13 +183,14 @@ export declare namespace NotebookActions {
182
183
  * An execution error will prevent the remaining code cells from executing.
183
184
  * All markdown cells will be rendered.
184
185
  */
185
- function run(notebook: Notebook, sessionContext?: ISessionContext): Promise<boolean>;
186
+ function run(notebook: Notebook, sessionContext?: ISessionContext, sessionDialogs?: ISessionContextDialogs, translator?: ITranslator): Promise<boolean>;
186
187
  /**
187
188
  * Run the selected cell(s) and advance to the next cell.
188
189
  *
189
190
  * @param notebook - The target notebook widget.
190
- *
191
- * @param sessionContext - The optional client session object.
191
+ * @param sessionContext - The client session object.
192
+ * @param sessionDialogs - The session dialogs.
193
+ * @param translator - The application translator.
192
194
  *
193
195
  * #### Notes
194
196
  * The existing selection will be cleared.
@@ -198,13 +200,14 @@ export declare namespace NotebookActions {
198
200
  * If the last selected cell is the last cell, a new code cell
199
201
  * will be created in `'edit'` mode. The new cell creation can be undone.
200
202
  */
201
- function runAndAdvance(notebook: Notebook, sessionContext?: ISessionContext): Promise<boolean>;
203
+ function runAndAdvance(notebook: Notebook, sessionContext?: ISessionContext, sessionDialogs?: ISessionContextDialogs, translator?: ITranslator): Promise<boolean>;
202
204
  /**
203
205
  * Run the selected cell(s) and insert a new code cell.
204
206
  *
205
207
  * @param notebook - The target notebook widget.
206
- *
207
- * @param sessionContext - The optional client session object.
208
+ * @param sessionContext - The client session object.
209
+ * @param sessionDialogs - The session dialogs.
210
+ * @param translator - The application translator.
208
211
  *
209
212
  * #### Notes
210
213
  * An execution error will prevent the remaining code cells from executing.
@@ -214,13 +217,14 @@ export declare namespace NotebookActions {
214
217
  * The cell insert can be undone.
215
218
  * The new cell will be scrolled into view.
216
219
  */
217
- function runAndInsert(notebook: Notebook, sessionContext?: ISessionContext): Promise<boolean>;
220
+ function runAndInsert(notebook: Notebook, sessionContext?: ISessionContext, sessionDialogs?: ISessionContextDialogs, translator?: ITranslator): Promise<boolean>;
218
221
  /**
219
222
  * Run all of the cells in the notebook.
220
223
  *
221
224
  * @param notebook - The target notebook widget.
222
- *
223
- * @param sessionContext - The optional client session object.
225
+ * @param sessionContext - The client session object.
226
+ * @param sessionDialogs - The session dialogs.
227
+ * @param translator - The application translator.
224
228
  *
225
229
  * #### Notes
226
230
  * The existing selection will be cleared.
@@ -228,14 +232,15 @@ export declare namespace NotebookActions {
228
232
  * All markdown cells will be rendered.
229
233
  * The last cell in the notebook will be activated and scrolled into view.
230
234
  */
231
- function runAll(notebook: Notebook, sessionContext?: ISessionContext): Promise<boolean>;
232
- function renderAllMarkdown(notebook: Notebook, sessionContext?: ISessionContext): Promise<boolean>;
235
+ function runAll(notebook: Notebook, sessionContext?: ISessionContext, sessionDialogs?: ISessionContextDialogs, translator?: ITranslator): Promise<boolean>;
236
+ function renderAllMarkdown(notebook: Notebook): Promise<boolean>;
233
237
  /**
234
238
  * Run all of the cells before the currently active cell (exclusive).
235
239
  *
236
240
  * @param notebook - The target notebook widget.
237
- *
238
- * @param sessionContext - The optional client session object.
241
+ * @param sessionContext - The client session object.
242
+ * @param sessionDialogs - The session dialogs.
243
+ * @param translator - The application translator.
239
244
  *
240
245
  * #### Notes
241
246
  * The existing selection will be cleared.
@@ -243,13 +248,14 @@ export declare namespace NotebookActions {
243
248
  * All markdown cells will be rendered.
244
249
  * The currently active cell will remain selected.
245
250
  */
246
- function runAllAbove(notebook: Notebook, sessionContext?: ISessionContext): Promise<boolean>;
251
+ function runAllAbove(notebook: Notebook, sessionContext?: ISessionContext, sessionDialogs?: ISessionContextDialogs, translator?: ITranslator): Promise<boolean>;
247
252
  /**
248
253
  * Run all of the cells after the currently active cell (inclusive).
249
254
  *
250
255
  * @param notebook - The target notebook widget.
251
- *
252
- * @param sessionContext - The optional client session object.
256
+ * @param sessionContext - The client session object.
257
+ * @param sessionDialogs - The session dialogs.
258
+ * @param translator - The application translator.
253
259
  *
254
260
  * #### Notes
255
261
  * The existing selection will be cleared.
@@ -257,7 +263,7 @@ export declare namespace NotebookActions {
257
263
  * All markdown cells will be rendered.
258
264
  * The last cell in the notebook will be activated and scrolled into view.
259
265
  */
260
- function runAllBelow(notebook: Notebook, sessionContext?: ISessionContext): Promise<boolean>;
266
+ function runAllBelow(notebook: Notebook, sessionContext?: ISessionContext, sessionDialogs?: ISessionContextDialogs, translator?: ITranslator): Promise<boolean>;
261
267
  /**
262
268
  * Replaces the selection in the active cell of the notebook.
263
269
  *
package/lib/actions.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // Copyright (c) Jupyter Development Team.
2
2
  // Distributed under the terms of the Modified BSD License.
3
- import { Clipboard, Dialog, sessionContextDialogs, showDialog } from '@jupyterlab/apputils';
3
+ import { Clipboard, Dialog, showDialog } from '@jupyterlab/apputils';
4
4
  import { CodeCell, isMarkdownCellModel, isRawCellModel, MarkdownCell } from '@jupyterlab/cells';
5
5
  import { signalToPromise } from '@jupyterlab/coreutils';
6
6
  import { nullTranslator } from '@jupyterlab/translation';
@@ -396,8 +396,9 @@ export class NotebookActions {
396
396
  * Run the selected cell(s).
397
397
  *
398
398
  * @param notebook - The target notebook widget.
399
- *
400
- * @param sessionContext - The optional client session object.
399
+ * @param sessionContext - The client session object.
400
+ * @param sessionDialogs - The session dialogs.
401
+ * @param translator - The application translator.
401
402
  *
402
403
  * #### Notes
403
404
  * The last selected cell will be activated, but not scrolled into view.
@@ -405,12 +406,12 @@ export class NotebookActions {
405
406
  * An execution error will prevent the remaining code cells from executing.
406
407
  * All markdown cells will be rendered.
407
408
  */
408
- function run(notebook, sessionContext) {
409
+ function run(notebook, sessionContext, sessionDialogs, translator) {
409
410
  if (!notebook.model || !notebook.activeCell) {
410
411
  return Promise.resolve(false);
411
412
  }
412
413
  const state = Private.getState(notebook);
413
- const promise = Private.runSelected(notebook, sessionContext);
414
+ const promise = Private.runSelected(notebook, sessionContext, sessionDialogs, translator);
414
415
  Private.handleRunState(notebook, state, false);
415
416
  return promise;
416
417
  }
@@ -419,8 +420,9 @@ export class NotebookActions {
419
420
  * Run the selected cell(s) and advance to the next cell.
420
421
  *
421
422
  * @param notebook - The target notebook widget.
422
- *
423
- * @param sessionContext - The optional client session object.
423
+ * @param sessionContext - The client session object.
424
+ * @param sessionDialogs - The session dialogs.
425
+ * @param translator - The application translator.
424
426
  *
425
427
  * #### Notes
426
428
  * The existing selection will be cleared.
@@ -430,13 +432,13 @@ export class NotebookActions {
430
432
  * If the last selected cell is the last cell, a new code cell
431
433
  * will be created in `'edit'` mode. The new cell creation can be undone.
432
434
  */
433
- async function runAndAdvance(notebook, sessionContext) {
435
+ async function runAndAdvance(notebook, sessionContext, sessionDialogs, translator) {
434
436
  var _a;
435
437
  if (!notebook.model || !notebook.activeCell) {
436
438
  return Promise.resolve(false);
437
439
  }
438
440
  const state = Private.getState(notebook);
439
- const promise = Private.runSelected(notebook, sessionContext);
441
+ const promise = Private.runSelected(notebook, sessionContext, sessionDialogs, translator);
440
442
  const model = notebook.model;
441
443
  if (notebook.activeCellIndex === notebook.widgets.length - 1) {
442
444
  // Do not use push here, as we want an widget insertion
@@ -463,8 +465,9 @@ export class NotebookActions {
463
465
  * Run the selected cell(s) and insert a new code cell.
464
466
  *
465
467
  * @param notebook - The target notebook widget.
466
- *
467
- * @param sessionContext - The optional client session object.
468
+ * @param sessionContext - The client session object.
469
+ * @param sessionDialogs - The session dialogs.
470
+ * @param translator - The application translator.
468
471
  *
469
472
  * #### Notes
470
473
  * An execution error will prevent the remaining code cells from executing.
@@ -474,13 +477,13 @@ export class NotebookActions {
474
477
  * The cell insert can be undone.
475
478
  * The new cell will be scrolled into view.
476
479
  */
477
- async function runAndInsert(notebook, sessionContext) {
480
+ async function runAndInsert(notebook, sessionContext, sessionDialogs, translator) {
478
481
  var _a;
479
482
  if (!notebook.model || !notebook.activeCell) {
480
483
  return Promise.resolve(false);
481
484
  }
482
485
  const state = Private.getState(notebook);
483
- const promise = Private.runSelected(notebook, sessionContext);
486
+ const promise = Private.runSelected(notebook, sessionContext, sessionDialogs, translator);
484
487
  const model = notebook.model;
485
488
  model.sharedModel.insertCell(notebook.activeCellIndex + 1, {
486
489
  cell_type: notebook.notebookConfig.defaultCell
@@ -500,8 +503,9 @@ export class NotebookActions {
500
503
  * Run all of the cells in the notebook.
501
504
  *
502
505
  * @param notebook - The target notebook widget.
503
- *
504
- * @param sessionContext - The optional client session object.
506
+ * @param sessionContext - The client session object.
507
+ * @param sessionDialogs - The session dialogs.
508
+ * @param translator - The application translator.
505
509
  *
506
510
  * #### Notes
507
511
  * The existing selection will be cleared.
@@ -509,7 +513,7 @@ export class NotebookActions {
509
513
  * All markdown cells will be rendered.
510
514
  * The last cell in the notebook will be activated and scrolled into view.
511
515
  */
512
- function runAll(notebook, sessionContext) {
516
+ function runAll(notebook, sessionContext, sessionDialogs, translator) {
513
517
  if (!notebook.model || !notebook.activeCell) {
514
518
  return Promise.resolve(false);
515
519
  }
@@ -517,12 +521,12 @@ export class NotebookActions {
517
521
  notebook.widgets.forEach(child => {
518
522
  notebook.select(child);
519
523
  });
520
- const promise = Private.runSelected(notebook, sessionContext);
524
+ const promise = Private.runSelected(notebook, sessionContext, sessionDialogs, translator);
521
525
  Private.handleRunState(notebook, state, true);
522
526
  return promise;
523
527
  }
524
528
  NotebookActions.runAll = runAll;
525
- function renderAllMarkdown(notebook, sessionContext) {
529
+ function renderAllMarkdown(notebook) {
526
530
  if (!notebook.model || !notebook.activeCell) {
527
531
  return Promise.resolve(false);
528
532
  }
@@ -539,7 +543,7 @@ export class NotebookActions {
539
543
  if (notebook.activeCell.model.type !== 'markdown') {
540
544
  return Promise.resolve(true);
541
545
  }
542
- const promise = Private.runSelected(notebook, sessionContext);
546
+ const promise = Private.runSelected(notebook);
543
547
  notebook.activeCellIndex = previousIndex;
544
548
  Private.handleRunState(notebook, state, true);
545
549
  return promise;
@@ -549,8 +553,9 @@ export class NotebookActions {
549
553
  * Run all of the cells before the currently active cell (exclusive).
550
554
  *
551
555
  * @param notebook - The target notebook widget.
552
- *
553
- * @param sessionContext - The optional client session object.
556
+ * @param sessionContext - The client session object.
557
+ * @param sessionDialogs - The session dialogs.
558
+ * @param translator - The application translator.
554
559
  *
555
560
  * #### Notes
556
561
  * The existing selection will be cleared.
@@ -558,7 +563,7 @@ export class NotebookActions {
558
563
  * All markdown cells will be rendered.
559
564
  * The currently active cell will remain selected.
560
565
  */
561
- function runAllAbove(notebook, sessionContext) {
566
+ function runAllAbove(notebook, sessionContext, sessionDialogs, translator) {
562
567
  const { activeCell, activeCellIndex, model } = notebook;
563
568
  if (!model || !activeCell || activeCellIndex < 1) {
564
569
  return Promise.resolve(false);
@@ -569,7 +574,7 @@ export class NotebookActions {
569
574
  for (let i = 0; i < notebook.activeCellIndex; ++i) {
570
575
  notebook.select(notebook.widgets[i]);
571
576
  }
572
- const promise = Private.runSelected(notebook, sessionContext);
577
+ const promise = Private.runSelected(notebook, sessionContext, sessionDialogs, translator);
573
578
  notebook.activeCellIndex++;
574
579
  Private.handleRunState(notebook, state, true);
575
580
  return promise;
@@ -579,8 +584,9 @@ export class NotebookActions {
579
584
  * Run all of the cells after the currently active cell (inclusive).
580
585
  *
581
586
  * @param notebook - The target notebook widget.
582
- *
583
- * @param sessionContext - The optional client session object.
587
+ * @param sessionContext - The client session object.
588
+ * @param sessionDialogs - The session dialogs.
589
+ * @param translator - The application translator.
584
590
  *
585
591
  * #### Notes
586
592
  * The existing selection will be cleared.
@@ -588,7 +594,7 @@ export class NotebookActions {
588
594
  * All markdown cells will be rendered.
589
595
  * The last cell in the notebook will be activated and scrolled into view.
590
596
  */
591
- function runAllBelow(notebook, sessionContext) {
597
+ function runAllBelow(notebook, sessionContext, sessionDialogs, translator) {
592
598
  if (!notebook.model || !notebook.activeCell) {
593
599
  return Promise.resolve(false);
594
600
  }
@@ -597,7 +603,7 @@ export class NotebookActions {
597
603
  for (let i = notebook.activeCellIndex; i < notebook.widgets.length; ++i) {
598
604
  notebook.select(notebook.widgets[i]);
599
605
  }
600
- const promise = Private.runSelected(notebook, sessionContext);
606
+ const promise = Private.runSelected(notebook, sessionContext, sessionDialogs, translator);
601
607
  Private.handleRunState(notebook, state, true);
602
608
  return promise;
603
609
  }
@@ -1742,8 +1748,13 @@ var Private;
1742
1748
  Private.handleRunState = handleRunState;
1743
1749
  /**
1744
1750
  * Run the selected cells.
1751
+ *
1752
+ * @param notebook Notebook
1753
+ * @param sessionContext Notebook session context
1754
+ * @param sessionDialogs Session dialogs
1755
+ * @param translator Application translator
1745
1756
  */
1746
- function runSelected(notebook, sessionContext) {
1757
+ function runSelected(notebook, sessionContext, sessionDialogs, translator) {
1747
1758
  notebook.mode = 'command';
1748
1759
  let lastIndex = notebook.activeCellIndex;
1749
1760
  const selected = notebook.widgets.filter((child, index) => {
@@ -1755,7 +1766,7 @@ var Private;
1755
1766
  });
1756
1767
  notebook.activeCellIndex = lastIndex;
1757
1768
  notebook.deselectAll();
1758
- return Promise.all(selected.map(child => runCell(notebook, child, sessionContext)))
1769
+ return Promise.all(selected.map(child => runCell(notebook, child, sessionContext, sessionDialogs, translator)))
1759
1770
  .then(results => {
1760
1771
  if (notebook.isDisposed) {
1761
1772
  return false;
@@ -1793,7 +1804,7 @@ var Private;
1793
1804
  /**
1794
1805
  * Run a cell.
1795
1806
  */
1796
- function runCell(notebook, cell, sessionContext, translator) {
1807
+ async function runCell(notebook, cell, sessionContext, sessionDialogs, translator) {
1797
1808
  var _a, _b, _c;
1798
1809
  translator = translator || nullTranslator;
1799
1810
  const trans = translator.load('jupyterlab');
@@ -1806,7 +1817,7 @@ var Private;
1806
1817
  case 'code':
1807
1818
  if (sessionContext) {
1808
1819
  if (sessionContext.isTerminating) {
1809
- void showDialog({
1820
+ await showDialog({
1810
1821
  title: trans.__('Kernel Terminating'),
1811
1822
  body: trans.__('The kernel for %1 appears to be terminating. You can not run any cell for now.', (_a = sessionContext.session) === null || _a === void 0 ? void 0 : _a.path),
1812
1823
  buttons: [Dialog.okButton()]
@@ -1814,55 +1825,71 @@ var Private;
1814
1825
  break;
1815
1826
  }
1816
1827
  if (sessionContext.pendingInput) {
1817
- void showDialog({
1828
+ await showDialog({
1818
1829
  title: trans.__('Cell not executed due to pending input'),
1819
1830
  body: trans.__('The cell has not been executed to avoid kernel deadlock as there is another pending input! Submit your pending input and try again.'),
1820
1831
  buttons: [Dialog.okButton()]
1821
1832
  });
1822
- return Promise.resolve(false);
1833
+ return false;
1823
1834
  }
1824
1835
  if (sessionContext.hasNoKernel) {
1825
- void sessionContextDialogs.selectKernel(sessionContext);
1826
- return Promise.resolve(false);
1836
+ const shouldSelect = await sessionContext.startKernel();
1837
+ if (shouldSelect && sessionDialogs) {
1838
+ await sessionDialogs.selectKernel(sessionContext);
1839
+ }
1840
+ }
1841
+ if (sessionContext.hasNoKernel) {
1842
+ cell.model.sharedModel.transact(() => {
1843
+ cell.model.clearExecution();
1844
+ });
1845
+ return true;
1827
1846
  }
1828
1847
  const deletedCells = (_c = (_b = notebook.model) === null || _b === void 0 ? void 0 : _b.deletedCells) !== null && _c !== void 0 ? _c : [];
1829
1848
  Private.executionScheduled.emit({ notebook, cell });
1830
- return CodeCell.execute(cell, sessionContext, {
1831
- deletedCells,
1832
- recordTiming: notebook.notebookConfig.recordTiming
1833
- })
1834
- .then(reply => {
1849
+ let ran = false;
1850
+ try {
1851
+ const reply = await CodeCell.execute(cell, sessionContext, {
1852
+ deletedCells,
1853
+ recordTiming: notebook.notebookConfig.recordTiming
1854
+ });
1835
1855
  deletedCells.splice(0, deletedCells.length);
1836
- if (cell.isDisposed) {
1837
- return false;
1838
- }
1839
- if (!reply) {
1840
- return true;
1841
- }
1842
- if (reply.content.status === 'ok') {
1843
- const content = reply.content;
1844
- if (content.payload && content.payload.length) {
1845
- handlePayload(content, notebook, cell);
1856
+ ran = (() => {
1857
+ if (cell.isDisposed) {
1858
+ return false;
1846
1859
  }
1847
- return true;
1848
- }
1849
- else {
1850
- throw new KernelError(reply.content);
1851
- }
1852
- })
1853
- .catch(reason => {
1860
+ if (!reply) {
1861
+ return true;
1862
+ }
1863
+ if (reply.content.status === 'ok') {
1864
+ const content = reply.content;
1865
+ if (content.payload && content.payload.length) {
1866
+ handlePayload(content, notebook, cell);
1867
+ }
1868
+ return true;
1869
+ }
1870
+ else {
1871
+ throw new KernelError(reply.content);
1872
+ }
1873
+ })();
1874
+ }
1875
+ catch (reason) {
1854
1876
  if (cell.isDisposed || reason.message.startsWith('Canceled')) {
1855
- return false;
1877
+ ran = false;
1856
1878
  }
1857
- Private.executed.emit({ notebook, cell, success: false, error: reason });
1858
- throw reason;
1859
- })
1860
- .then(ran => {
1861
- if (ran) {
1862
- Private.executed.emit({ notebook, cell, success: true });
1879
+ else {
1880
+ Private.executed.emit({
1881
+ notebook,
1882
+ cell,
1883
+ success: false,
1884
+ error: reason
1885
+ });
1886
+ throw reason;
1863
1887
  }
1864
- return ran;
1865
- });
1888
+ }
1889
+ if (ran) {
1890
+ Private.executed.emit({ notebook, cell, success: true });
1891
+ }
1892
+ return ran;
1866
1893
  }
1867
1894
  cell.model.sharedModel.transact(() => {
1868
1895
  cell.model.clearExecution();