diff --git a/ipykernel/displayhook.py b/ipykernel/displayhook.py index c41ddaa93..6e263abf2 100644 --- a/ipykernel/displayhook.py +++ b/ipykernel/displayhook.py @@ -22,6 +22,10 @@ def __init__(self, session, pub_socket): self.pub_socket = pub_socket self.parent_header = {} + def get_execution_count(self): + """This method is replaced in kernelapp""" + return 0 + def __call__(self, obj): if obj is None: return @@ -29,7 +33,10 @@ def __call__(self, obj): builtin_mod._ = obj sys.stdout.flush() sys.stderr.flush() - self.session.send(self.pub_socket, u'execute_result', {u'data':repr(obj)}, + contents = {u'execution_count': self.get_execution_count(), + u'data': {'text/plain': repr(obj)}, + u'metadata': {}} + self.session.send(self.pub_socket, u'execute_result', contents, parent=self.parent_header, ident=self.topic) def set_parent(self, parent): diff --git a/ipykernel/kernelapp.py b/ipykernel/kernelapp.py index 729aa5f4c..fcbe5e479 100644 --- a/ipykernel/kernelapp.py +++ b/ipykernel/kernelapp.py @@ -314,7 +314,8 @@ def init_io(self): sys.stderr = outstream_factory(self.session, self.iopub_thread, u'stderr') if self.displayhook_class: displayhook_factory = import_item(str(self.displayhook_class)) - sys.displayhook = displayhook_factory(self.session, self.iopub_socket) + self.displayhook = displayhook_factory(self.session, self.iopub_socket) + sys.displayhook = self.displayhook self.patch_io() @@ -365,6 +366,9 @@ def init_kernel(self): kernel.record_ports(self.ports) self.kernel = kernel + # Allow the displayhook to get the execution count + self.displayhook.get_execution_count = lambda: kernel.execution_count + def init_gui_pylab(self): """Enable GUI event loop integration, taking pylab into account."""