notebook-utils.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. export
  31. // tslint:disable-next-line
  32. const DEFAULT_CONTENT: nbformat.INotebookContent = require('../examples/notebook/test.ipynb') as nbformat.INotebookContent;
  33. DEFAULT_CONTENT.metadata = { orig_nbformat: 1 };
  34. export
  35. const defaultEditorConfig = { ...StaticNotebook.defaultEditorConfig };
  36. export
  37. const editorFactory = editorServices.factoryService.newInlineEditor.bind(
  38. editorServices.factoryService);
  39. export
  40. const mimeTypeService = editorServices.mimeTypeService;
  41. export
  42. const rendermime = defaultRenderMime();
  43. export
  44. const clipboard = Clipboard.getInstance();
  45. /**
  46. * Create a base cell content factory.
  47. */
  48. export
  49. function createBaseCellFactory(): Cell.IContentFactory {
  50. return new Cell.ContentFactory({ editorFactory });
  51. }
  52. /**
  53. * Create a new code cell content factory.
  54. */
  55. export
  56. function createCodeCellFactory(): Cell.IContentFactory {
  57. return new Cell.ContentFactory({ editorFactory });
  58. }
  59. /**
  60. * Create a cell editor widget.
  61. */
  62. export
  63. function createCellEditor(model?: CodeCellModel): CodeEditorWrapper {
  64. return new CodeEditorWrapper({
  65. model: model || new CodeCellModel({}),
  66. factory: editorFactory
  67. });
  68. }
  69. /**
  70. * Create a default notebook content factory.
  71. */
  72. export
  73. function createNotebookFactory(): Notebook.IContentFactory {
  74. return new Notebook.ContentFactory({ editorFactory });
  75. }
  76. /**
  77. * Create a default notebook panel content factory.
  78. */
  79. export
  80. function createNotebookPanelFactory(): NotebookPanel.IContentFactory {
  81. return new NotebookPanel.ContentFactory({ editorFactory });
  82. }
  83. /**
  84. * Create a notebook widget.
  85. */
  86. export
  87. function createNotebook(): Notebook {
  88. return new Notebook({
  89. rendermime: defaultRenderMime(),
  90. contentFactory: createNotebookFactory(),
  91. mimeTypeService
  92. });
  93. }
  94. /**
  95. * Create a notebook panel widget.
  96. */
  97. export
  98. function createNotebookPanel(context: Context<INotebookModel>): NotebookPanel {
  99. return new NotebookPanel({
  100. content: createNotebook(),
  101. context
  102. });
  103. }
  104. /**
  105. * Populate a notebook with default content.
  106. */
  107. export
  108. function populateNotebook(notebook: Notebook): void {
  109. let model = new NotebookModel();
  110. model.fromJSON(DEFAULT_CONTENT);
  111. notebook.model = model;
  112. }