From 2b04cb38d98a54778777133fdb77adfb42b8ee69 Mon Sep 17 00:00:00 2001
From: Javi Merino <javi.merino@arm.com>
Date: Fri, 17 Apr 2015 16:18:23 +0100
Subject: [PATCH] Don't break prematurely when running a cell on an ipython
 kernel

The kernel may go idle before it processes the next input, which break
the while=True loop in run_cell() early.  Wait for an acknowledgement
of the input we've sent to the kernel before considering an idle
message to mean that the cell has been parsed.
---
 wlauto/utils/ipython.py | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/wlauto/utils/ipython.py b/wlauto/utils/ipython.py
index 6ac4d323..68f5ab84 100644
--- a/wlauto/utils/ipython.py
+++ b/wlauto/utils/ipython.py
@@ -52,6 +52,7 @@ def run_cell(kernel_client, cell):
     """Run a cell of a notebook in an ipython kernel and return its output"""
     kernel_client.execute(cell.input)
 
+    input_acknowledged = False
     outs = []
     while True:
         msg = kernel_client.get_iopub_msg()
@@ -61,11 +62,12 @@ def run_cell(kernel_client, cell):
         out = NotebookNode(output_type=msg_type)
 
         if msg_type == "status":
-            if content["execution_state"] == "idle":
+            if content["execution_state"] == "idle" and input_acknowledged:
                 break
             else:
                 continue
         elif msg_type == "pyin":
+            input_acknowledged = True
             continue
         elif msg_type == "stream":
             out.stream = content["name"]