Browse Source

Clean up cell execution and run actions

Steven Silvester 8 years ago
parent
commit
73db8b2458
3 changed files with 51 additions and 52 deletions
  1. 6 4
      src/notebook/cells/widget.ts
  2. 12 13
      src/notebook/notebook/actions.ts
  3. 33 35
      src/notebook/output-area/model.ts

+ 6 - 4
src/notebook/cells/widget.ts

@@ -34,7 +34,7 @@ import {
 } from 'phosphor-widget';
 
 import {
-  OutputAreaWidget, executeCode
+  OutputAreaWidget
 } from '../output-area';
 
 import {
@@ -447,10 +447,12 @@ class CodeCellWidget extends BaseCellWidget {
     this.setPrompt('*');
     this.trusted = true;
     let outputs = model.outputs;
-    return executeCode(code, kernel, outputs).then((reply: any) => {
+    return outputs.execute(code, kernel).then((reply: any) => {
       model.executionCount = reply.content.execution_count;
-      // TODO: check the return status and clear the execution state
-      // if necessary.
+      if (reply.content.status !== 'ok') {
+        model.executionCount = null;
+        return false;
+      }
       return true;
     });
   }

+ 12 - 13
src/notebook/notebook/actions.ts

@@ -315,6 +315,8 @@ namespace NotebookActions {
    * #### Notes
    * The last selected cell will be activated.
    * The existing selection will be cleared.
+   * An execution error will prevent the remaining code cells from executing.
+   * All markdown cells will be rendered.
    */
   export
   function run(widget: Notebook, kernel?: IKernel): Promise<boolean> {
@@ -358,6 +360,8 @@ namespace NotebookActions {
    * #### Notes
    * The existing selection will be cleared.
    * The cell after the last selected cell will be activated.
+   * An execution error will prevent the remaining code cells from executing.
+   * All markdown cells will be rendered.
    * If the last selected cell is the last cell, a new code cell
    * will be created in `'edit'` mode.  The new cell creation can be undone.
    */
@@ -367,9 +371,6 @@ namespace NotebookActions {
       return Promise.resolve(false);
     }
     return run(widget, kernel).then(result => {
-      if (!result) {
-        return false;
-      }
       let model = widget.model;
       if (widget.activeCellIndex === widget.childCount() - 1) {
         let cell = model.factory.createCodeCell();
@@ -377,7 +378,7 @@ namespace NotebookActions {
         widget.mode = 'edit';
       }
       widget.activeCellIndex++;
-      return true;
+      return result;
     });
   }
 
@@ -389,6 +390,8 @@ namespace NotebookActions {
    * @param kernel - An optional kernel object.
    *
    * #### Notes
+   * An execution error will prevent the remaining code cells from executing.
+   * All markdown cells will be rendered.
    * The widget mode will be set to `'edit'` after running.
    * The existing selection will be cleared.
    * The cell insert can be undone.
@@ -399,15 +402,12 @@ namespace NotebookActions {
       return Promise.resolve(false);
     }
     return run(widget, kernel).then(result => {
-      if (!result) {
-        return false;
-      }
       let model = widget.model;
       let cell = model.factory.createCodeCell();
       model.cells.insert(widget.activeCellIndex + 1, cell);
       widget.activeCellIndex++;
       widget.mode = 'edit';
-      return true;
+      return result;
     });
   }
 
@@ -419,6 +419,9 @@ namespace NotebookActions {
    * @param kernel - An optional kernel object.
    * #### Notes
    * The existing selection will be cleared.
+   * An execution error will prevent the remaining code cells from executing.
+   * All markdown cells will be rendered.
+   * The last cell in the notebook will be activated.
    */
   export
   function runAll(widget: Notebook, kernel?: IKernel): Promise<boolean> {
@@ -831,11 +834,7 @@ namespace Private {
       break;
     case 'code':
       if (kernel) {
-        return (widget as CodeCellWidget).execute(kernel).then(reply => {
-          // TODO: let status = (reply as any).content.status;
-          // return status === 'ok';
-          return true;
-        });
+        return (widget as CodeCellWidget).execute(kernel);
       }
       (widget.model as CodeCellModel).executionCount = null;
       break;

+ 33 - 35
src/notebook/output-area/model.ts

@@ -145,6 +145,39 @@ class OutputAreaModel implements IDisposable {
     return this._list.clear();
   }
 
+  /**
+   * Execute code on a kernel and send outputs to the model.
+   */
+  execute(code: string, kernel: IKernel): Promise<KernelMessage.IExecuteReplyMsg> {
+    // Override the default for `stop_on_error`.
+    let content: KernelMessage.IExecuteRequest = {
+      code,
+      stop_on_error: true
+    };
+    this.clear();
+    return new Promise<KernelMessage.IExecuteReplyMsg>((resolve, reject) => {
+      let future = kernel.execute(content);
+      future.onIOPub = ((msg: KernelMessage.IIOPubMessage) => {
+        let msgType = msg.header.msg_type as nbformat.OutputType;
+        switch (msgType) {
+        case 'execute_result':
+        case 'display_data':
+        case 'stream':
+        case 'error':
+          let model = msg.content as nbformat.IOutput;
+          model.output_type = msgType;
+          this.add(model);
+          break;
+        default:
+          break;
+        }
+      });
+      future.onReply = (msg: KernelMessage.IExecuteReplyMsg) => {
+        resolve(msg);
+      };
+    });
+  }
+
   /**
    * Handle a change to the list.
    */
@@ -157,41 +190,6 @@ class OutputAreaModel implements IDisposable {
 }
 
 
-/**
- * Execute code on a kernel and send outputs to an output area model.
- */
-export
-function executeCode(code: string, kernel: IKernel, outputs: OutputAreaModel): Promise<KernelMessage.IExecuteReplyMsg> {
-  // Override the default for `stop_on_error`.
-  let content: KernelMessage.IExecuteRequest = {
-    code,
-    stop_on_error: true
-  };
-  outputs.clear();
-  return new Promise<KernelMessage.IExecuteReplyMsg>((resolve, reject) => {
-    let future = kernel.execute(content);
-    future.onIOPub = ((msg: KernelMessage.IIOPubMessage) => {
-      let msgType = msg.header.msg_type as nbformat.OutputType;
-      switch (msgType) {
-      case 'execute_result':
-      case 'display_data':
-      case 'stream':
-      case 'error':
-        let model = msg.content as nbformat.IOutput;
-        model.output_type = msgType;
-        outputs.add(model);
-        break;
-      default:
-        break;
-      }
-    });
-    future.onReply = (msg: KernelMessage.IExecuteReplyMsg) => {
-      resolve(msg);
-    };
-  });
-}
-
-
 /**
  * A namespace for private data.
  */