瀏覽代碼

Merge pull request #671 from blink1073/load-magic

Add support for %load magic
Jason Grout 8 年之前
父節點
當前提交
887dea1005
共有 3 個文件被更改,包括 48 次插入1 次删除
  1. 11 0
      src/console/widget.ts
  2. 2 1
      src/notebook/cells/widget.ts
  3. 35 0
      src/notebook/notebook/actions.ts

+ 11 - 0
src/console/widget.ts

@@ -221,6 +221,17 @@ class ConsoleWidget extends Widget {
         if (value.content.status === 'ok') {
           let content = value.content as KernelMessage.IExecuteOkReply;
           this._inspectionHandler.handleExecuteReply(content);
+          // Use deprecated payloads for backwards compatibility.
+          if (content.payload && content.payload.length) {
+            let setNextInput = content.payload.filter(i => {
+              return (i as any).source === 'set_next_input';
+            })[0];
+            if (setNextInput) {
+              let text = (setNextInput as any).text;
+              // Ignore the `replace` value and always set the next prompt.
+              this.prompt.model.source = text;
+            }
+          }
         }
         Private.scrollToBottom(this.node);
       },

+ 2 - 1
src/notebook/cells/widget.ts

@@ -440,7 +440,8 @@ class CodeCellWidget extends BaseCellWidget {
     this.trusted = true;
     let outputs = model.outputs;
     return outputs.execute(code, kernel).then(reply => {
-      if (reply.content.status === 'aborted') {
+      let status = reply.content.status;
+      if (status === 'aborted') {
         model.executionCount = null;
         this.setPrompt(' ');
       } else {

+ 35 - 0
src/notebook/notebook/actions.ts

@@ -865,6 +865,9 @@ namespace Private {
           if (reply && reply.content.status === 'ok') {
             let content = reply.content as KernelMessage.IExecuteOkReply;
             parent.inspectionHandler.handleExecuteReply(content);
+            if (content.payload && content.payload.length) {
+              handlePayload(content, parent, child);
+            }
           } else {
             parent.inspectionHandler.handleExecuteReply(null);
           }
@@ -879,6 +882,38 @@ namespace Private {
     return Promise.resolve(true);
   }
 
+  /**
+   * Handle payloads from an execute reply.
+   *
+   * #### Notes
+   * Payloads are deprecated and there are no official interfaces for them in
+   * the kernel type definitions.
+   * See [Payloads (DEPRECATED)](http://jupyter-client.readthedocs.io/en/latest/messaging.html#payloads-deprecated).
+   */
+  function handlePayload(content: KernelMessage.IExecuteOkReply, parent: Notebook, child: BaseCellWidget) {
+    let setNextInput = content.payload.filter(i => {
+      return (i as any).source === 'set_next_input';
+    })[0];
+    if (setNextInput) {
+      let text = (setNextInput as any).text;
+      let replace = (setNextInput as any).replace;
+      if (replace) {
+        child.model.source = text;
+      } else {
+        // Create a new code cell and add as the next cell.
+        let cell = parent.model.factory.createCodeCell();
+        cell.source = text;
+        let cells = parent.model.cells;
+        let i = cells.indexOf(child.model);
+        if (i === -1) {
+          cells.add(cell);
+        } else {
+          cells.insert(i + 1, cell);
+        }
+      }
+    }
+  }
+
   /**
    * Set the markdown header level of a cell.
    */