From 1b1d27e9652fcda20298f78589f364b5c529886f Mon Sep 17 00:00:00 2001 From: Jacob Niehus Date: Sat, 18 Feb 2017 10:36:57 -0700 Subject: [PATCH] Update execution_count before running cell Ensure the execution count displayed next to the code is correct by processing any IOPub messages between pressing enter and running the code. The execution count should always be updated after an execute_input message even if the message came from a session whose output wouldn't be included because it is still the execution count of this session's kernel. --- jupyter_console/ptshell.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/jupyter_console/ptshell.py b/jupyter_console/ptshell.py index 04edd529..691bcbfd 100644 --- a/jupyter_console/ptshell.py +++ b/jupyter_console/ptshell.py @@ -358,6 +358,10 @@ def _(event): b.newline() return + # Pressing enter flushes any pending display. This also ensures + # the displayed execution_count is correct. + self.handle_iopub() + more, indent = self.check_complete(d.text) if (not more) and b.accept_action.is_returnable: @@ -663,6 +667,10 @@ def handle_iopub(self, msg_id=''): msg_type = sub_msg['header']['msg_type'] parent = sub_msg["parent_header"] + # Update execution_count in case it changed in another session + if msg_type == "execute_input": + self.execution_count = int(sub_msg["content"]["execution_count"]) + 1 + if self.include_output(sub_msg): if msg_type == 'status': self._execution_state = sub_msg["content"]["execution_state"]