Browse Source

Create a readonly notebook plugin

Steven Silvester 9 years ago
parent
commit
39799b1d29

+ 2 - 2
package.json

@@ -8,7 +8,7 @@
     "codemirror": "^5.10.0",
     "jquery": "^2.2.0",
     "jquery-ui": "^1.10.5",
-    "jupyter-js-notebook": "^0.7.2",
+    "jupyter-js-notebook": "^0.9.0",
     "jupyter-js-services": "^0.5.0",
     "jupyter-js-ui": "^0.0.3",
     "jupyter-js-utils": "^0.3.0",
@@ -40,7 +40,7 @@
     "rimraf": "^2.5.0",
     "style-loader": "^0.13.0",
     "typedoc": "^0.3.12",
-    "typescript": "^1.7.5",
+    "typescript": "^1.8.0",
     "url-loader": "^0.5.7",
     "watch": "^0.17.1",
     "webpack": "^1.12.11"

+ 106 - 0
src/readonly-notebook/plugin.ts

@@ -0,0 +1,106 @@
+// Copyright (c) Jupyter Development Team.
+// Distributed under the terms of the Modified BSD License.
+'use strict';
+
+import {
+  NotebookWidget, NotebookModel, populateNotebookModel, buildOutputModel, Output, INotebookModel, getNotebookContent
+} from 'jupyter-js-notebook';
+
+import {
+  isCodeCellModel, isMarkdownCellModel
+} from 'jupyter-js-notebook/lib/cells';
+
+import {
+  IContentsModel, IContentsManager, IContentsOpts
+} from 'jupyter-js-services';
+
+import {
+  AbstractFileHandler, DocumentManager
+} from 'jupyter-js-ui/lib/docmanager';
+
+import {
+  Application
+} from 'phosphide/lib/core/application';
+
+import {
+  JupyterServices
+} from '../services/plugin';
+
+
+
+/**
+ * The notebook file handler provider.
+ */
+export
+const notebookHandlerExtension = {
+  id: 'jupyter.extensions.notebookHandler',
+  requires: [DocumentManager, JupyterServices],
+  activate: activateNotebookHandler
+}
+
+
+/**
+ * Activate the notebook handler extension.
+ */
+function activateNotebookHandler(app: Application, manager: DocumentManager, services: JupyterServices): Promise<void> {
+  let handler = new NotebookFileHandler(
+    services.contentsManager
+  );
+  manager.register(handler);
+  return Promise.resolve(void 0);
+}
+
+
+/**
+ * An implementation of a file handler.
+ */
+class NotebookFileHandler extends AbstractFileHandler<NotebookWidget> {
+
+  constructor(contents: IContentsManager) {
+    super(contents);
+  }
+
+  /**
+   * Get the list of file extensions supported by the handler.
+   */
+  get fileExtensions(): string[] {
+    return ['.ipynb']
+  }
+
+  /**
+   * Get options use to fetch the model contents from disk.
+   */
+  protected getFetchOptions(model: IContentsModel): IContentsOpts {
+    return { type: 'notebook' };
+  }
+
+  /**
+   * Get the options used to save the widget content.
+   */
+  protected getSaveOptions(widget: NotebookWidget, model: IContentsModel): Promise<IContentsOpts> {
+      let content = getNotebookContent(widget.model);
+      return Promise.resolve({ type: 'notebook', content });
+  }
+
+  /**
+   * Create the widget from an `IContentsModel`.
+   */
+  protected createWidget(contents: IContentsModel): NotebookWidget {
+    let model = new NotebookModel();
+    model.readOnly = true;
+    return new NotebookWidget(model);
+  }
+
+  /**
+   * Populate the notebook widget with the contents of the notebook.
+   */
+  protected populateWidget(widget: NotebookWidget, model: IContentsModel): Promise<IContentsModel> {
+    populateNotebookModel(widget.model, model.content);
+    if (widget.model.cells.length === 0) {
+      let cell = widget.model.createCodeCell();
+      widget.model.cells.add(cell);
+    }
+
+    return Promise.resolve(model);
+  }
+}

+ 1 - 18
src/tsconfig.json

@@ -7,22 +7,5 @@
     "moduleResolution": "node",
     "target": "ES5",
     "outDir": "../lib"
-  },
-  "files": [
-    "../typings/es6-promise.d.ts",
-    "../typings/requirejs/requirejs.d.ts",
-    "../typings/codemirror/codemirror.d.ts",
-    "../typings/widgets.d.ts",
-    "index.ts",
-    "filebrowser/plugin.ts",
-    "filehandler/plugin.ts",
-    "documentmanager/plugin.ts",
-    "imagehandler/plugin.ts",
-    "help/plugin.ts",
-    "notebook/plugin.ts",
-    "notebook/widgetmanager.ts",
-    "services/plugin.ts",
-    "shortcuts/plugin.ts",
-    "terminal/plugin.ts"
-  ]
+  }
 }

+ 0 - 0
typings/codemirror/codemirror.d.ts → src/typings/codemirror/codemirror.d.ts


+ 0 - 0
typings/es6-promise.d.ts → src/typings/es6-promise.d.ts


+ 0 - 0
typings/expect.js/expect.js.d.ts → src/typings/expect.js/expect.js.d.ts


+ 0 - 0
typings/mocha/mocha.d.ts → src/typings/mocha/mocha.d.ts


+ 0 - 0
typings/moment/moment.d.ts → src/typings/moment/moment.d.ts


+ 0 - 0
typings/requirejs/requirejs.d.ts → src/typings/requirejs/requirejs.d.ts


+ 0 - 0
typings/widgets.d.ts → src/typings/widgets.d.ts


+ 2 - 2
test/src/tsconfig.json

@@ -8,8 +8,8 @@
     "outDir": "../build"
   },
   "files": [
-    "../../typings/expect.js/expect.js.d.ts",
-    "../../typings/mocha/mocha.d.ts",
+    "../../src/typings/expect.js/expect.js.d.ts",
+    "../../src/typings/mocha/mocha.d.ts",
     "index.ts"
   ]
 }