Browse Source

Clean up the use of nbformat

Steven Silvester 8 years ago
parent
commit
12fda0725e

+ 3 - 7
src/cells/model.ts

@@ -2,11 +2,7 @@
 // Distributed under the terms of the Modified BSD License.
 
 import {
-  nbformat, utils
-} from '@jupyterlab/services';
-
-import {
-  JSONValue
+  JSONExt, JSONValue
 } from '@phosphor/coreutils';
 
 import {
@@ -18,7 +14,7 @@ import {
 } from '../codeeditor';
 
 import {
-  IChangedArgs
+  IChangedArgs, nbformat
 } from '../coreutils';
 
 import {
@@ -139,7 +135,7 @@ class CellModel extends CodeEditor.Model implements ICellModel {
     } else {
       this.value.text = cell.source as string;
     }
-    let metadata = utils.copy(cell.metadata);
+    let metadata = JSONExt.deepCopy(cell.metadata);
     if (this.type !== 'raw') {
       delete metadata['format'];
     }

+ 4 - 4
src/codemirror/mimetype.ts

@@ -4,14 +4,14 @@
 import * as CodeMirror
   from 'codemirror';
 
-import {
-  nbformat
-} from '@jupyterlab/services';
-
 import {
   IEditorMimeTypeService
 } from '../codeeditor';
 
+import {
+  nbformat
+} from '../coreutils';
+
 import {
   findMode
 } from './index';

+ 5 - 1
src/console/foreign.ts

@@ -2,7 +2,7 @@
 // Distributed under the terms of the Modified BSD License.
 
 import {
-  Kernel, KernelMessage, nbformat
+  Kernel, KernelMessage
 } from '@jupyterlab/services';
 
 import {
@@ -13,6 +13,10 @@ import {
   BaseCellWidget, CodeCellWidget
 } from '../cells';
 
+import {
+  nbformat
+} from '../coreutils';
+
 
 /**
  * A handler for capturing API messages from other sessions that should be

+ 5 - 1
src/console/widget.ts

@@ -2,7 +2,7 @@
 // Distributed under the terms of the Modified BSD License.
 
 import {
-  Kernel, KernelMessage, Session, nbformat
+  Kernel, KernelMessage, Session
 } from '@jupyterlab/services';
 
 import {
@@ -35,6 +35,10 @@ import {
   RawCellModel, CodeCellModel
 } from '../cells';
 
+import {
+  nbformat
+} from '../coreutils';
+
 import {
   OutputAreaWidget
 } from '../outputarea';

+ 56 - 1
src/coreutils/nbformat.ts

@@ -301,12 +301,36 @@ namespace nbformat {
   export
   interface IUnrecognizedCell extends IBaseCell { }
 
+
   /**
    * A cell union type.
    */
   export
   type ICell = IRawCell | IMarkdownCell | ICodeCell | IUnrecognizedCell;
 
+  /**
+   * Test whether a cell is a raw cell.
+   */
+  export
+  function isRaw(cell: ICell): cell is IRawCell {
+    return cell.cell_type === 'raw';
+  }
+
+  /**
+   * Test whether a cell is a markdown cell.
+   */
+  export
+  function isMarkdown(cell: ICell): cell is IMarkdownCell {
+    return cell.cell_type === 'markdown';
+  }
+
+  /**
+   * Test whether a cell is a code cell.
+   */
+  export
+  function isCode(cell: ICell): cell is ICodeCell {
+    return cell.cell_type === 'code';
+  }
 
   /**
    * A union metadata type.
@@ -437,10 +461,41 @@ namespace nbformat {
   export
   interface IUnrecognizedOutput extends IBaseOutput { }
 
+  /**
+   * Test whether an output is an execute result.
+   */
+  export
+  function isExecuteResult(output: IOutput): output is IExecuteResult {
+    return output.output_type === 'execute_result';
+  }
+
+  /**
+   * Test whether an output is from display data.
+   */
+  export
+  function isDisplayData(output: IOutput): output is IDisplayData {
+    return output.output_type === 'display_data';
+  }
+
+  /**
+   * Test whether an output is from a stream.
+   */
+  export
+  function isStream(output: IOutput): output is IStream {
+    return output.output_type === 'stream';
+  }
+
+  /**
+   * Test whether an output is from a stream.
+   */
+  export
+  function isError(output: IOutput): output is IError {
+    return output.output_type === 'error';
+  }
 
   /**
    * An output union type.
    */
   export
-  type IOutput = IExecuteResult | IDisplayData | IStream | IError | IUnrecognizedOutput;
+  type IOutput = IUnrecognizedOutput | IExecuteResult | IDisplayData | IStream | IError;
 }

+ 5 - 1
src/notebook/actions.ts

@@ -2,7 +2,7 @@
 // Distributed under the terms of the Modified BSD License.
 
 import {
-  Kernel, KernelMessage, nbformat
+  Kernel, KernelMessage
 } from '@jupyterlab/services';
 
 import {
@@ -13,6 +13,10 @@ import {
   Clipboard
 } from '../apputils';
 
+import {
+  nbformat
+} from '../coreutils';
+
 import {
   ICellModel, ICodeCellModel,
   CodeCellWidget, BaseCellWidget, MarkdownCellWidget

+ 1 - 9
src/notebook/celltools.ts

@@ -1,10 +1,6 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
-import {
-  nbformat
-} from '@jupyterlab/services';
-
 import {
   ArrayExt, each
 } from '@phosphor/algorithm';
@@ -42,11 +38,7 @@ import {
 } from '../codeeditor';
 
 import {
-  IChangedArgs
-} from '../coreutils';
-
-import {
-  ObservableJSON
+  IChangedArgs, ObservableJSON, nbformat
 } from '../coreutils';
 
 import {

+ 4 - 4
src/notebook/default-toolbar.ts

@@ -1,10 +1,6 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
-import {
-  nbformat
-} from '@jupyterlab/services';
-
 import {
   Message
 } from '@phosphor/messaging';
@@ -21,6 +17,10 @@ import {
   Styling
 } from '../apputils';
 
+import {
+  nbformat
+} from '../coreutils';
+
 import {
   NotebookPanel
 } from './panel';

+ 2 - 17
src/notebook/model.ts

@@ -1,18 +1,10 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
-import {
-  nbformat
-} from '@jupyterlab/services';
-
 import {
   each
 } from '@phosphor/algorithm';
 
-import {
-  IObservableVector, ObservableVector
-} from '../coreutils';
-
 import {
   DocumentModel, DocumentRegistry
 } from '../docregistry';
@@ -23,15 +15,8 @@ import {
 } from '../cells/model';
 
 import {
-  IChangedArgs
-} from '../coreutils';
-
-import {
-  IObservableJSON, ObservableJSON
-} from '../coreutils';
-
-import {
-  IObservableUndoableVector, ObservableUndoableVector
+  IChangedArgs, IObservableJSON, ObservableJSON, IObservableUndoableVector,
+  ObservableUndoableVector, IObservableVector, ObservableVector, nbformat
 } from '../coreutils';
 
 

+ 3 - 14
src/notebook/widget.ts

@@ -2,11 +2,7 @@
 // Distributed under the terms of the Modified BSD License.
 
 import {
-  nbformat
-} from '@jupyterlab/services';
-
-import {
-  ArrayExt, each, find, toArray
+  ArrayExt, each, toArray
 } from '@phosphor/algorithm';
 
 import {
@@ -56,15 +52,8 @@ import {
 } from '../codeeditor';
 
 import {
-  IChangedArgs
-} from '../coreutils';
-
-import {
-  IObservableMap, ObservableMap
-} from '../coreutils';
-
-import {
-  IObservableVector, ObservableVector
+  IChangedArgs, IObservableMap, ObservableMap, IObservableVector,
+  ObservableVector, nbformat
 } from '../coreutils';
 
 import {

+ 4 - 8
src/outputarea/model.ts

@@ -1,10 +1,6 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
-import {
-  nbformat
-} from '@jupyterlab/services';
-
 import {
   each, map, toArray
 } from '@phosphor/algorithm';
@@ -14,7 +10,7 @@ import {
 } from '@phosphor/signaling';
 
 import {
-  IObservableVector, ObservableVector
+  IObservableVector, ObservableVector, nbformat
 } from '../coreutils';
 
 import {
@@ -182,14 +178,14 @@ class OutputAreaModel implements IOutputAreaModel {
     let trusted = this._trusted;
 
     // Normalize stream data.
-    if (value.output_type === 'stream') {
+    if (nbformat.isStream(value)) {
       if (Array.isArray(value.text)) {
         value.text = (value.text as string[]).join('\n');
       }
     }
 
     // Consolidate outputs if they are stream outputs of the same kind.
-    if (value.output_type === 'stream' && this._lastStream &&
+    if (nbformat.isStream(value) && this._lastStream &&
         value.name === this._lastName) {
       // In order to get a list change event, we add the previous
       // text to the current item and replace the previous item.
@@ -208,7 +204,7 @@ class OutputAreaModel implements IOutputAreaModel {
     let item = this._createItem({ value, trusted });
 
     // Update the stream information.
-    if (value.output_type === 'stream') {
+    if (nbformat.isStream(value)) {
       this._lastStream = value.text as string;
       this._lastName = value.name;
     } else {

+ 2 - 2
src/outputarea/widget.ts

@@ -2,7 +2,7 @@
 // Distributed under the terms of the Modified BSD License.
 
 import {
-  Kernel, KernelMessage, nbformat
+  Kernel, KernelMessage
 } from '@jupyterlab/services';
 
 import {
@@ -34,7 +34,7 @@ import {
 } from '@phosphor/widgets';
 
 import {
-  ObservableVector
+  ObservableVector, nbformat
 } from '../coreutils';
 
 import {

+ 12 - 24
src/rendermime/outputmodel.ts

@@ -1,14 +1,14 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
-import {
-  nbformat
-} from '@jupyterlab/services';
-
 import {
   JSONExt, JSONObject, JSONValue
 } from '@phosphor/coreutils';
 
+import {
+  nbformat
+} from '../coreutils';
+
 import {
   MimeModel
 } from './mimemodel';
@@ -26,7 +26,7 @@ interface IOutputModel extends RenderMime.IMimeModel {
   /**
    * The output type.
    */
-  readonly type: nbformat.OutputType;
+  readonly type: string;
 
   /**
    * The execution count of the model.
@@ -86,7 +86,7 @@ class OutputModel extends MimeModel implements IOutputModel {
       }
     }
     this.type = value.output_type;
-    if (value.output_type === 'execute_result') {
+    if (nbformat.isExecuteResult(value)) {
       this.executionCount = value.execution_count;
     } else {
       this.executionCount = null;
@@ -96,7 +96,7 @@ class OutputModel extends MimeModel implements IOutputModel {
   /**
    * The output type.
    */
-  readonly type: nbformat.OutputType;
+  readonly type: string;
 
   /**
    * The execution count.
@@ -170,26 +170,19 @@ namespace OutputModel {
   export
   function getData(output: nbformat.IOutput): JSONObject {
     let bundle: nbformat.IMimeBundle = {};
-    switch (output.output_type) {
-    case 'execute_result':
-    case 'display_data':
-      bundle = output.data;
-      break;
-    case 'stream':
+    if (nbformat.isExecuteResult(output) || nbformat.isDisplayData(output)) {
+      bundle = (output as nbformat.IExecuteResult).data;
+    } else if (nbformat.isStream(output)) {
       if (output.name === 'stderr') {
         bundle['application/vnd.jupyter.stderr'] = output.text;
       } else {
         bundle['application/vnd.jupyter.stdout'] = output.text;
       }
-      break;
-    case 'error':
+    } else if (nbformat.isError(output)) {
       let traceback = output.traceback.join('\n');
       bundle['application/vnd.jupyter.stderr'] = (
         traceback || `${output.ename}: ${output.evalue}`
       );
-      break;
-    default:
-      break;
     }
     return convertBundle(bundle);
   }
@@ -200,15 +193,10 @@ namespace OutputModel {
   export
   function getMetadata(output: nbformat.IOutput): JSONObject {
     let value: JSONObject = Object.create(null);
-    switch (output.output_type) {
-    case 'execute_result':
-    case 'display_data':
+    if (nbformat.isExecuteResult(output) || nbformat.isDisplayData(output)) {
       for (let key in output.metadata) {
         value[key] = extract(output.metadata, key);
       }
-      break;
-    default:
-      break;
     }
     return value;
   }

+ 1 - 5
test/src/cells/model.spec.ts

@@ -3,16 +3,12 @@
 
 import expect = require('expect.js');
 
-import {
-  nbformat
-} from '@jupyterlab/services';
-
 import {
   toArray
 } from '@phosphor/algorithm';
 
 import {
-  IChangedArgs
+  IChangedArgs, nbformat
 } from '../../../lib/coreutils';
 
 import {

+ 4 - 4
test/src/notebook/model.spec.ts

@@ -3,10 +3,6 @@
 
 import expect = require('expect.js');
 
-import {
-  nbformat
-} from '@jupyterlab/services';
-
 import {
   ArrayExt, toArray
 } from '@phosphor/algorithm';
@@ -15,6 +11,10 @@ import {
   CodeCellModel
 } from '../../../lib/cells/model';
 
+import {
+  nbformat
+} from '../../../lib/coreutils';
+
 import {
   NotebookModel
 } from '../../../lib/notebook/model';

+ 4 - 4
test/src/notebook/utils.ts

@@ -1,10 +1,6 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
-import {
-  nbformat
-} from '@jupyterlab/services';
-
 import {
   editorServices
 } from '../../../lib/codemirror';
@@ -17,6 +13,10 @@ import {
   Clipboard
 } from '../../../lib/apputils';
 
+import {
+  nbformat
+} from '../../../lib/coreutils';
+
 import {
   NotebookPanel, Notebook, NotebookModel
 } from '../../../lib/notebook';

+ 1 - 1
test/src/rendermime/outputmodel.spec.ts

@@ -5,7 +5,7 @@ import expect = require('expect.js');
 
 import {
   nbformat
-} from '@jupyterlab/services';
+} from '../../../lib/coreutils';
 
 import {
   OutputModel

+ 7 - 3
test/src/utils.ts

@@ -8,13 +8,17 @@ import {
 } from 'simulate-event';
 
 import {
-  ServiceManager, nbformat, utils
+  ServiceManager
 } from '@jupyterlab/services';
 
 import {
   Widget
 } from '@phosphor/widgets';
 
+import {
+  nbformat, uuid
+} from '../../lib/coreutils';
+
 import {
   TextModelFactory, DocumentRegistry, Context
 } from '../../lib/docregistry';
@@ -52,7 +56,7 @@ export
 function createFileContext(path?: string, manager?: ServiceManager.IManager): Context<DocumentRegistry.IModel> {
   manager = manager || Private.manager;
   let factory = Private.textFactory;
-  path = path || utils.uuid() + '.txt';
+  path = path || uuid() + '.txt';
   return new Context({ manager, factory, path });
 }
 
@@ -64,7 +68,7 @@ export
 function createNotebookContext(path?: string, manager?: ServiceManager.IManager): Context<INotebookModel> {
   manager = manager || Private.manager;
   let factory = Private.notebookFactory;
-  path = path || utils.uuid() + '.ipynb';
+  path = path || uuid() + '.ipynb';
   return new Context({ manager, factory, path });
 }