notebook-utils.ts 2.7 KB

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