notebook-utils.ts 2.7 KB

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