notebook-utils.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. editorServices
  5. } from '@jupyterlab/codemirror';
  6. import {
  7. CodeEditorWrapper
  8. } from '@jupyterlab/codeeditor';
  9. import {
  10. Clipboard
  11. } from '@jupyterlab/apputils';
  12. import {
  13. nbformat
  14. } from '@jupyterlab/coreutils';
  15. import {
  16. Context
  17. } from '@jupyterlab/docregistry';
  18. import {
  19. INotebookModel, NotebookPanel, Notebook, NotebookModel, StaticNotebook
  20. } from '@jupyterlab/notebook';
  21. import {
  22. Cell, CodeCellModel
  23. } from '@jupyterlab/cells';
  24. import {
  25. defaultRenderMime
  26. } from './utils';
  27. /**
  28. * The default notebook content.
  29. */
  30. // tslint:disable-next-line
  31. export const DEFAULT_CONTENT: nbformat.INotebookContent = require('../examples/notebook/test.ipynb') as nbformat.INotebookContent;
  32. DEFAULT_CONTENT.metadata = { orig_nbformat: 1 };
  33. export
  34. const defaultEditorConfig = { ...StaticNotebook.defaultEditorConfig };
  35. export
  36. const editorFactory = editorServices.factoryService.newInlineEditor.bind(
  37. editorServices.factoryService);
  38. export
  39. const mimeTypeService = editorServices.mimeTypeService;
  40. export
  41. const rendermime = defaultRenderMime();
  42. export
  43. const clipboard = Clipboard.getInstance();
  44. /**
  45. * Create a base cell content factory.
  46. */
  47. export
  48. function createBaseCellFactory(): Cell.IContentFactory {
  49. return new Cell.ContentFactory({ editorFactory });
  50. }
  51. /**
  52. * Create a new code cell content factory.
  53. */
  54. export
  55. function createCodeCellFactory(): Cell.IContentFactory {
  56. return new Cell.ContentFactory({ editorFactory });
  57. }
  58. /**
  59. * Create a cell editor widget.
  60. */
  61. export
  62. function createCellEditor(model?: CodeCellModel): CodeEditorWrapper {
  63. return new CodeEditorWrapper({
  64. model: model || new CodeCellModel({}),
  65. factory: editorFactory
  66. });
  67. }
  68. /**
  69. * Create a default notebook content factory.
  70. */
  71. export
  72. function createNotebookFactory(): Notebook.IContentFactory {
  73. return new Notebook.ContentFactory({ editorFactory });
  74. }
  75. /**
  76. * Create a default notebook panel content factory.
  77. */
  78. export
  79. function createNotebookPanelFactory(): NotebookPanel.IContentFactory {
  80. return new NotebookPanel.ContentFactory({ editorFactory });
  81. }
  82. /**
  83. * Create a notebook widget.
  84. */
  85. export
  86. function createNotebook(): Notebook {
  87. return new Notebook({
  88. rendermime: defaultRenderMime(),
  89. contentFactory: createNotebookFactory(),
  90. mimeTypeService
  91. });
  92. }
  93. /**
  94. * Create a notebook panel widget.
  95. */
  96. export
  97. function createNotebookPanel(context: Context<INotebookModel>): NotebookPanel {
  98. return new NotebookPanel({
  99. content: createNotebook(),
  100. context
  101. });
  102. }
  103. /**
  104. * Populate a notebook with default content.
  105. */
  106. export
  107. function populateNotebook(notebook: Notebook): void {
  108. let model = new NotebookModel();
  109. model.fromJSON(DEFAULT_CONTENT);
  110. notebook.model = model;
  111. }