Browse Source

Update outputarea, cells, and console

Steven Silvester 8 years ago
parent
commit
c87dd460f6

+ 10 - 5
packages/cells/src/widget.ts

@@ -2,7 +2,7 @@
 // Distributed under the terms of the Modified BSD License.
 
 import {
-  Kernel, KernelMessage
+  KernelMessage
 } from '@jupyterlab/services';
 
 import {
@@ -21,6 +21,10 @@ import {
   Widget
 } from '@phosphor/widgets';
 
+import {
+  ClientSession
+} from '@jupyterlab/apputils';
+
 import {
   IChangedArgs
 } from '@jupyterlab/coreutils';
@@ -395,12 +399,12 @@ class CodeCellWidget extends BaseCellWidget {
   }
 
   /**
-   * Execute the cell given a kernel.
+   * Execute the cell given a client session.
    */
-  execute(kernel: Kernel.IKernel): Promise<KernelMessage.IExecuteReplyMsg> {
+  execute(session: ClientSession): Promise<KernelMessage.IExecuteReplyMsg> {
     let model = this.model;
     let code = model.value.text;
-    if (!code.trim()) {
+    if (!code.trim() || !session.kernel) {
       model.executionCount = null;
       model.outputs.clear();
       return Promise.resolve(null);
@@ -408,7 +412,8 @@ class CodeCellWidget extends BaseCellWidget {
     model.executionCount = null;
     this.setPrompt('*');
     this.model.trusted = true;
-    return this._output.execute(code, kernel).then(reply => {
+
+    return this._output.execute(code, session).then(reply => {
       let status = reply.content.status;
       if (status === 'abort') {
         model.executionCount = null;

+ 23 - 28
packages/console/src/foreign.ts

@@ -2,13 +2,21 @@
 // Distributed under the terms of the Modified BSD License.
 
 import {
-  Kernel, KernelMessage
+  KernelMessage
 } from '@jupyterlab/services';
 
 import {
   IDisposable
 } from '@phosphor/disposable';
 
+import {
+  Signal
+} from '@phosphor/signaling';
+
+import {
+  ClientSession
+} from '@jupyterlab/apputils';
+
 import {
   BaseCellWidget, CodeCellWidget
 } from '@jupyterlab/cells';
@@ -28,7 +36,8 @@ class ForeignHandler implements IDisposable {
    * Construct a new foreign message handler.
    */
   constructor(options: ForeignHandler.IOptions) {
-    this.kernel = options.kernel;
+    this.session = options.session;
+    this.session.iopubMessage.connect(this.onIOPubMessage, this);
     this._factory = options.cellFactory;
     this._parent = options.parent;
   }
@@ -44,27 +53,9 @@ class ForeignHandler implements IDisposable {
   }
 
   /**
-   * The kernel used by the foreign handler.
+   * The client session used by the foreign handler.
    */
-  get kernel(): Kernel.IKernel {
-    return this._kernel;
-  }
-  set kernel(value: Kernel.IKernel) {
-    if (this._kernel === value) {
-      return;
-    }
-
-    // Disconnect previously connected kernel.
-    if (this._kernel) {
-      this._cells.clear();
-      this._kernel.iopubMessage.disconnect(this.onIOPubMessage, this);
-    }
-
-    this._kernel = value;
-    if (this._kernel) {
-      this._kernel.iopubMessage.connect(this.onIOPubMessage, this);
-    }
-  }
+  readonly session: ClientSession;
 
   /**
    * The foreign handler's parent receiver.
@@ -89,8 +80,8 @@ class ForeignHandler implements IDisposable {
     }
     let cells = this._cells;
     this._cells = null;
-    this._kernel = null;
     cells.clear();
+    Signal.clearData(this);
   }
 
   /**
@@ -99,15 +90,20 @@ class ForeignHandler implements IDisposable {
    * @returns `true` if the message resulted in a new cell injection or a
    * previously injected cell being updated and `false` for all other messages.
    */
-  protected onIOPubMessage(sender: Kernel.IKernel, msg: KernelMessage.IIOPubMessage): boolean {
+  protected onIOPubMessage(sender: ClientSession, msg: KernelMessage.IIOPubMessage): boolean {
     // Only process messages if foreign cell injection is enabled.
     if (!this._enabled) {
       return false;
     }
+    let kernel = this.session.kernel;
+    if (!kernel) {
+      return;
+    }
+
     // Check whether this message came from an external session.
     let parent = this._parent;
     let session = (msg.parent_header as KernelMessage.IHeader).session;
-    if (session === this._kernel.clientId) {
+    if (session === kernel.clientId) {
       return false;
     }
     let msgType = msg.header.msg_type;
@@ -162,7 +158,6 @@ class ForeignHandler implements IDisposable {
 
   private _cells = new Map<string, CodeCellWidget>();
   private _enabled = true;
-  private _kernel: Kernel.IKernel = null;
   private _parent: ForeignHandler.IReceiver = null;
   private _factory: () => CodeCellWidget = null;
 }
@@ -179,9 +174,9 @@ namespace ForeignHandler {
   export
   interface IOptions {
     /**
-     * The kernel that the handler will listen to.
+     * The client session used by the foreign handler.
      */
-    kernel: Kernel.IKernel;
+    session: ClientSession;
 
     /**
      * The parent into which the handler will inject code cells.

+ 30 - 30
packages/console/src/history.ts

@@ -2,7 +2,7 @@
 // Distributed under the terms of the Modified BSD License.
 
 import {
-  Kernel, KernelMessage
+  KernelMessage
 } from '@jupyterlab/services';
 
 import {
@@ -13,6 +13,10 @@ import {
   Signal
 } from '@phosphor/signaling';
 
+import {
+  ClientSession
+} from '@jupyterlab/apputils';
+
 import {
   CodeEditor
 } from '@jupyterlab/codeeditor';
@@ -24,9 +28,9 @@ import {
 export
 interface IConsoleHistory extends IDisposable {
   /**
-   * The current kernel supplying navigation history.
+   * The client session used by the foreign handler.
    */
-  kernel: Kernel.IKernel;
+  readonly session: ClientSession;
 
   /**
    * The current editor used by the history widget.
@@ -87,34 +91,16 @@ class ConsoleHistory implements IConsoleHistory {
   /**
    * Construct a new console history object.
    */
-  constructor(options?: ConsoleHistory.IOptions) {
-    if (options && options.kernel) {
-      this.kernel = options.kernel;
-    }
+  constructor(options: ConsoleHistory.IOptions) {
+    this.session = options.session;
+    this._handleKernel();
+    this.session.kernelChanged.connect(this._handleKernel, this);
   }
 
   /**
-   * The current kernel supplying navigation history.
+   * The client session used by the foreign handler.
    */
-  get kernel(): Kernel.IKernel {
-    return this._kernel;
-  }
-  set kernel(newValue: Kernel.IKernel) {
-    if (newValue === this._kernel) {
-      return;
-    }
-
-    this._kernel = newValue;
-
-    if (!this._kernel) {
-      this._history.length = 0;
-      return;
-    }
-
-    this._kernel.requestHistory(Private.initialRequest).then(v => {
-      this.onHistory(v);
-    });
-  }
+  readonly session: ClientSession;
 
   /**
    * The current editor used by the history manager.
@@ -297,10 +283,24 @@ class ConsoleHistory implements IConsoleHistory {
     }
   }
 
+  /**
+   * Handle the current kernel changing.
+   */
+  private _handleKernel(): void {
+    let kernel = this.session.kernel;
+    if (!kernel) {
+      this._history.length = 0;
+      return;
+    }
+
+    kernel.requestHistory(Private.initialRequest).then(v => {
+      this.onHistory(v);
+    });
+  }
+
   private _cursor = 0;
   private _hasSession = false;
   private _history: string[] = [];
-  private _kernel: Kernel.IKernel = null;
   private _placeholder: string = '';
   private _setByHistory = false;
   private _isDisposed = false;
@@ -319,9 +319,9 @@ namespace ConsoleHistory {
   export
   interface IOptions {
     /**
-     * The kernel instance to query for history.
+     * The client session used by the foreign handler.
      */
-    kernel?: Kernel.IKernel;
+    session: ClientSession;
   }
 }
 

+ 4 - 4
packages/console/src/panel.ts

@@ -2,8 +2,8 @@
 // Distributed under the terms of the Modified BSD License.
 
 import {
-  Session
-} from '@jupyterlab/services';
+  ClientSession
+} from '@jupyterlab/apputils';
 
 import {
   Token
@@ -117,9 +117,9 @@ namespace ConsolePanel {
     contentFactory: IContentFactory;
 
     /**
-     * The session for the console widget.
+     * The client session for the console widget.
      */
-    session: Session.ISession;
+    session: ClientSession;
 
     /**
      * The model factory for the console widget.

+ 14 - 15
packages/console/src/widget.ts

@@ -2,7 +2,7 @@
 // Distributed under the terms of the Modified BSD License.
 
 import {
-  Kernel, KernelMessage, Session
+  KernelMessage
 } from '@jupyterlab/services';
 
 import {
@@ -25,6 +25,10 @@ import {
   Widget
 } from '@phosphor/widgets';
 
+import {
+  ClientSession
+} from '@jupyterlab/apputils';
+
 import {
   IEditorMimeTypeService, CodeEditor
 } from '@jupyterlab/codeeditor';
@@ -36,7 +40,7 @@ import {
 } from '@jupyterlab/cells';
 
 import {
-  nbformat
+  nbformat, IObservableVector, ObservableVector
 } from '@jupyterlab/coreutils';
 
 import {
@@ -55,9 +59,6 @@ import {
   ConsoleHistory, IConsoleHistory
 } from './history';
 
-import {
-  IObservableVector, ObservableVector
-} from '@jupyterlab/coreutils';
 
 /**
  * The class name added to console widgets.
@@ -149,13 +150,13 @@ class CodeConsole extends Widget {
 
     // Set up the foreign iopub handler.
     this._foreignHandler = factory.createForeignHandler({
-      kernel: this.session.kernel,
+      session: this.session,
       parent: this,
       cellFactory: () => this._createForeignCell(),
     });
 
     this._history = factory.createConsoleHistory({
-      kernel: this.session.kernel
+      session: this.session
     });
 
     this.session.kernelChanged.connect(this._onKernelChanged, this);
@@ -191,9 +192,9 @@ class CodeConsole extends Widget {
   readonly rendermime: IRenderMime;
 
   /**
-   * The session used by the console.
+   * The client session used by the console.
    */
-  readonly session: Session.ISession;
+  readonly session: ClientSession;
 
   /**
    * The console banner widget.
@@ -505,7 +506,7 @@ class CodeConsole extends Widget {
       cell.model.contentChanged.disconnect(this.update, this);
       this.update();
     };
-    return cell.execute(this.session.kernel).then(onSuccess, onFailure);
+    return cell.execute(this.session).then(onSuccess, onFailure);
   }
 
   /**
@@ -594,11 +595,9 @@ class CodeConsole extends Widget {
   /**
    * Handle a change to the kernel.
    */
-  private _onKernelChanged(sender: Session.ISession, kernel: Kernel.IKernel): void {
+  private _onKernelChanged(): void {
     this.clear();
     this._initialize();
-    this._history.kernel = kernel;
-    this._foreignHandler.kernel = kernel;
     this.newPrompt();
   }
 
@@ -640,9 +639,9 @@ namespace CodeConsole {
     rendermime: IRenderMime;
 
     /**
-     * The session for the console widget.
+     * The client session for the console widget.
      */
-    session: Session.ISession;
+    session: ClientSession;
 
     /**
      * The service used to look up mime types.

+ 20 - 6
packages/outputarea/src/widget.ts

@@ -33,6 +33,10 @@ import {
   Widget
 } from '@phosphor/widgets';
 
+import {
+  ClientSession
+} from '@jupyterlab/apputils';
+
 import {
   ObservableVector, nbformat
 } from '@jupyterlab/coreutils';
@@ -208,23 +212,32 @@ class OutputAreaWidget extends Widget {
   }
 
   /**
-   * Execute code on a kernel and handle response messages.
+   * Execute code on a client session and handle response messages.
    */
-  execute(code: string, kernel: Kernel.IKernel): Promise<KernelMessage.IExecuteReplyMsg> {
+  execute(code: string, session: ClientSession): Promise<KernelMessage.IExecuteReplyMsg> {
     // Bail if the model is disposed.
     if (this.model.isDisposed) {
       return Promise.reject('Model is disposed');
     }
+
+    // Bail if there is no kernel.
+    let kernel = session.kernel;
+    if (!kernel) {
+      return Promise.reject('No kernel exists on the session');
+    }
+
     // Override the default for `stop_on_error`.
     let content: KernelMessage.IExecuteRequest = {
       code,
       stop_on_error: true
     };
     this.model.clear();
+
     // Make sure there were no input widgets.
     if (this.widgets.length) {
       this._clear();
     }
+
     return new Promise<KernelMessage.IExecuteReplyMsg>((resolve, reject) => {
       // Bail if the model is disposed.
       if (this.model.isDisposed) {
@@ -243,7 +256,7 @@ class OutputAreaWidget extends Widget {
       // Handle stdin.
       future.onStdin = (msg: KernelMessage.IStdinMessage) => {
         if (KernelMessage.isInputRequestMsg(msg)) {
-          this._onInputRequest(msg, kernel);
+          this._onInputRequest(msg, session);
         }
       };
     });
@@ -367,7 +380,7 @@ class OutputAreaWidget extends Widget {
   /**
    * Handle an input request from a kernel.
    */
-  private _onInputRequest(msg: KernelMessage.IInputRequestMsg, kernel: Kernel.IKernel): void {
+  private _onInputRequest(msg: KernelMessage.IInputRequestMsg, session: ClientSession): void {
     // Add an output widget to the end.
     let factory = this.contentFactory;
     let prompt = msg.content.prompt;
@@ -381,6 +394,7 @@ class OutputAreaWidget extends Widget {
     gutter.addClass(GUTTER_CLASS);
     panel.addWidget(gutter);
 
+    let kernel = session.kernel;
     let input = factory.createStdin({ prompt, password, kernel });
     input.addClass(STDIN_CLASS);
     panel.addWidget(input);
@@ -499,7 +513,7 @@ namespace OutputAreaWidget {
     /**
      * The kernel associated with the request.
      */
-    kernel: Kernel.IKernel;
+    kernel: Kernel.IKernelConnection;
   }
 
   /**
@@ -615,7 +629,7 @@ namespace OutputAreaWidget {
       this._input.removeEventListener('keydown', this);
     }
 
-    private _kernel: Kernel.IKernel = null;
+    private _kernel: Kernel.IKernelConnection = null;
     private _input: HTMLInputElement = null;
   }