notebook-utils.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { editorServices } from '@jupyterlab/codemirror';
  4. import { CodeEditorWrapper } from '@jupyterlab/codeeditor';
  5. import { Clipboard } from '@jupyterlab/apputils';
  6. import { nbformat } from '@jupyterlab/coreutils';
  7. import { Context } from '@jupyterlab/docregistry';
  8. import {
  9. INotebookModel,
  10. NotebookPanel,
  11. Notebook,
  12. NotebookModel,
  13. StaticNotebook
  14. } from '@jupyterlab/notebook';
  15. import { RenderMimeRegistry } from '@jupyterlab/rendermime';
  16. import { Cell, CodeCellModel } from '@jupyterlab/cells';
  17. import { defaultRenderMime as localRendermime } from './rendermime';
  18. /**
  19. * Stub for the require() function.
  20. */
  21. declare var require: any;
  22. /**
  23. * The default notebook content.
  24. */
  25. // tslint:disable-next-line
  26. export namespace NBTestUtils {
  27. /**
  28. * The default outputs used for testing.
  29. */
  30. export const DEFAULT_OUTPUTS: nbformat.IOutput[] = [
  31. {
  32. name: 'stdout',
  33. output_type: 'stream',
  34. text: ['hello world\n', '0\n', '1\n', '2\n']
  35. },
  36. {
  37. name: 'stderr',
  38. output_type: 'stream',
  39. text: ['output to stderr\n']
  40. },
  41. {
  42. name: 'stderr',
  43. output_type: 'stream',
  44. text: ['output to stderr2\n']
  45. },
  46. {
  47. output_type: 'execute_result',
  48. execution_count: 1,
  49. data: { 'text/plain': 'foo' },
  50. metadata: {}
  51. },
  52. {
  53. output_type: 'display_data',
  54. data: { 'text/plain': 'hello, world' },
  55. metadata: {}
  56. },
  57. {
  58. output_type: 'error',
  59. ename: 'foo',
  60. evalue: 'bar',
  61. traceback: ['fizz', 'buzz']
  62. }
  63. ];
  64. export const DEFAULT_CONTENT: nbformat.INotebookContent = require('../default.json') as nbformat.INotebookContent;
  65. DEFAULT_CONTENT.metadata = { orig_nbformat: 1 };
  66. export const defaultEditorConfig = { ...StaticNotebook.defaultEditorConfig };
  67. export const editorFactory = editorServices.factoryService.newInlineEditor.bind(
  68. editorServices.factoryService
  69. );
  70. export const mimeTypeService = editorServices.mimeTypeService;
  71. /**
  72. * Get a copy of the default rendermime instance.
  73. */
  74. export function defaultRenderMime(): RenderMimeRegistry {
  75. return localRendermime();
  76. }
  77. export const clipboard = Clipboard.getInstance();
  78. /**
  79. * Create a base cell content factory.
  80. */
  81. export function createBaseCellFactory(): Cell.IContentFactory {
  82. return new Cell.ContentFactory({ editorFactory });
  83. }
  84. /**
  85. * Create a new code cell content factory.
  86. */
  87. export function createCodeCellFactory(): Cell.IContentFactory {
  88. return new Cell.ContentFactory({ editorFactory });
  89. }
  90. /**
  91. * Create a cell editor widget.
  92. */
  93. export function createCellEditor(model?: CodeCellModel): CodeEditorWrapper {
  94. return new CodeEditorWrapper({
  95. model: model || new CodeCellModel({}),
  96. factory: editorFactory
  97. });
  98. }
  99. /**
  100. * Create a default notebook content factory.
  101. */
  102. export function createNotebookFactory(): Notebook.IContentFactory {
  103. return new Notebook.ContentFactory({ editorFactory });
  104. }
  105. /**
  106. * Create a default notebook panel content factory.
  107. */
  108. export function createNotebookPanelFactory(): NotebookPanel.IContentFactory {
  109. return new NotebookPanel.ContentFactory({ editorFactory });
  110. }
  111. /**
  112. * Create a notebook widget.
  113. */
  114. export function createNotebook(): Notebook {
  115. return new Notebook({
  116. rendermime: defaultRenderMime(),
  117. contentFactory: createNotebookFactory(),
  118. mimeTypeService
  119. });
  120. }
  121. /**
  122. * Create a notebook panel widget.
  123. */
  124. export function createNotebookPanel(
  125. context: Context<INotebookModel>
  126. ): NotebookPanel {
  127. return new NotebookPanel({
  128. content: createNotebook(),
  129. context
  130. });
  131. }
  132. /**
  133. * Populate a notebook with default content.
  134. */
  135. export function populateNotebook(notebook: Notebook): void {
  136. let model = new NotebookModel();
  137. model.fromJSON(DEFAULT_CONTENT);
  138. notebook.model = model;
  139. }
  140. }