We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Displaying a widget using displayhook produces misaligned Out[N] prompt
![screenshot from 2015-01-22 16 56 44](https://cloud.githubusercontent.com/assets/327925/5867960/bd98d2d8-a257-11e4-8599-e0e331664f9b.png)

This doesn't look right. @jdfreder, can you investigate?


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `IPython/kernel/zmq/displayhook.py`
Content:
```
1 """Replacements for sys.displayhook that publish over ZMQ."""
2 
3 # Copyright (c) IPython Development Team.
4 # Distributed under the terms of the Modified BSD License.
5 
6 import sys
7 
8 from IPython.core.displayhook import DisplayHook
9 from IPython.kernel.inprocess.socket import SocketABC
10 from IPython.utils.jsonutil import encode_images
11 from IPython.utils.py3compat import builtin_mod
12 from IPython.utils.traitlets import Instance, Dict
13 from .session import extract_header, Session
14 
15 class ZMQDisplayHook(object):
16     """A simple displayhook that publishes the object's repr over a ZeroMQ
17     socket."""
18     topic=b'execute_result'
19 
20     def __init__(self, session, pub_socket):
21         self.session = session
22         self.pub_socket = pub_socket
23         self.parent_header = {}
24 
25     def __call__(self, obj):
26         if obj is None:
27             return
28 
29         builtin_mod._ = obj
30         sys.stdout.flush()
31         sys.stderr.flush()
32         msg = self.session.send(self.pub_socket, u'execute_result', {u'data':repr(obj)},
33                                parent=self.parent_header, ident=self.topic)
34 
35     def set_parent(self, parent):
36         self.parent_header = extract_header(parent)
37 
38 
39 class ZMQShellDisplayHook(DisplayHook):
40     """A displayhook subclass that publishes data using ZeroMQ. This is intended
41     to work with an InteractiveShell instance. It sends a dict of different
42     representations of the object."""
43     topic=None
44 
45     session = Instance(Session)
46     pub_socket = Instance(SocketABC)
47     parent_header = Dict({})
48 
49     def set_parent(self, parent):
50         """Set the parent for outbound messages."""
51         self.parent_header = extract_header(parent)
52 
53     def start_displayhook(self):
54         self.msg = self.session.msg(u'execute_result', {
55             'data': {},
56             'metadata': {},
57         }, parent=self.parent_header)
58 
59     def write_output_prompt(self):
60         """Write the output prompt."""
61         self.msg['content']['execution_count'] = self.prompt_count
62 
63     def write_format_data(self, format_dict, md_dict=None):
64         self.msg['content']['data'] = encode_images(format_dict)
65         self.msg['content']['metadata'] = md_dict
66 
67     def finish_displayhook(self):
68         """Finish up all displayhook activities."""
69         sys.stdout.flush()
70         sys.stderr.flush()
71         self.session.send(self.pub_socket, self.msg, ident=self.topic)
72         self.msg = None
73 
74
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/IPython/kernel/zmq/displayhook.py b/IPython/kernel/zmq/displayhook.py
--- a/IPython/kernel/zmq/displayhook.py
+++ b/IPython/kernel/zmq/displayhook.py
@@ -68,6 +68,7 @@
         """Finish up all displayhook activities."""
         sys.stdout.flush()
         sys.stderr.flush()
-        self.session.send(self.pub_socket, self.msg, ident=self.topic)
+        if self.msg['content']['data']:
+            self.session.send(self.pub_socket, self.msg, ident=self.topic)
         self.msg = None

{"golden_diff": "diff --git a/IPython/kernel/zmq/displayhook.py b/IPython/kernel/zmq/displayhook.py\n--- a/IPython/kernel/zmq/displayhook.py\n+++ b/IPython/kernel/zmq/displayhook.py\n@@ -68,6 +68,7 @@\n         \"\"\"Finish up all displayhook activities.\"\"\"\n         sys.stdout.flush()\n         sys.stderr.flush()\n-        self.session.send(self.pub_socket, self.msg, ident=self.topic)\n+        if self.msg['content']['data']:\n+            self.session.send(self.pub_socket, self.msg, ident=self.topic)\n         self.msg = None\n", "issue": "Displaying a widget using displayhook produces misaligned Out[N] prompt\n![screenshot from 2015-01-22 16 56 44](https://cloud.githubusercontent.com/assets/327925/5867960/bd98d2d8-a257-11e4-8599-e0e331664f9b.png)\n\nThis doesn't look right. @jdfreder, can you investigate?\n\n", "before_files": [{"content": "\"\"\"Replacements for sys.displayhook that publish over ZMQ.\"\"\"\n\n# Copyright (c) IPython Development Team.\n# Distributed under the terms of the Modified BSD License.\n\nimport sys\n\nfrom IPython.core.displayhook import DisplayHook\nfrom IPython.kernel.inprocess.socket import SocketABC\nfrom IPython.utils.jsonutil import encode_images\nfrom IPython.utils.py3compat import builtin_mod\nfrom IPython.utils.traitlets import Instance, Dict\nfrom .session import extract_header, Session\n\nclass ZMQDisplayHook(object):\n    \"\"\"A simple displayhook that publishes the object's repr over a ZeroMQ\n    socket.\"\"\"\n    topic=b'execute_result'\n\n    def __init__(self, session, pub_socket):\n        self.session = session\n        self.pub_socket = pub_socket\n        self.parent_header = {}\n\n    def __call__(self, obj):\n        if obj is None:\n            return\n\n        builtin_mod._ = obj\n        sys.stdout.flush()\n        sys.stderr.flush()\n        msg = self.session.send(self.pub_socket, u'execute_result', {u'data':repr(obj)},\n                               parent=self.parent_header, ident=self.topic)\n\n    def set_parent(self, parent):\n        self.parent_header = extract_header(parent)\n\n\nclass ZMQShellDisplayHook(DisplayHook):\n    \"\"\"A displayhook subclass that publishes data using ZeroMQ. This is intended\n    to work with an InteractiveShell instance. It sends a dict of different\n    representations of the object.\"\"\"\n    topic=None\n\n    session = Instance(Session)\n    pub_socket = Instance(SocketABC)\n    parent_header = Dict({})\n\n    def set_parent(self, parent):\n        \"\"\"Set the parent for outbound messages.\"\"\"\n        self.parent_header = extract_header(parent)\n\n    def start_displayhook(self):\n        self.msg = self.session.msg(u'execute_result', {\n            'data': {},\n            'metadata': {},\n        }, parent=self.parent_header)\n\n    def write_output_prompt(self):\n        \"\"\"Write the output prompt.\"\"\"\n        self.msg['content']['execution_count'] = self.prompt_count\n\n    def write_format_data(self, format_dict, md_dict=None):\n        self.msg['content']['data'] = encode_images(format_dict)\n        self.msg['content']['metadata'] = md_dict\n\n    def finish_displayhook(self):\n        \"\"\"Finish up all displayhook activities.\"\"\"\n        sys.stdout.flush()\n        sys.stderr.flush()\n        self.session.send(self.pub_socket, self.msg, ident=self.topic)\n        self.msg = None\n\n", "path": "IPython/kernel/zmq/displayhook.py"}], "after_files": [{"content": "\"\"\"Replacements for sys.displayhook that publish over ZMQ.\"\"\"\n\n# Copyright (c) IPython Development Team.\n# Distributed under the terms of the Modified BSD License.\n\nimport sys\n\nfrom IPython.core.displayhook import DisplayHook\nfrom IPython.kernel.inprocess.socket import SocketABC\nfrom IPython.utils.jsonutil import encode_images\nfrom IPython.utils.py3compat import builtin_mod\nfrom IPython.utils.traitlets import Instance, Dict\nfrom .session import extract_header, Session\n\nclass ZMQDisplayHook(object):\n    \"\"\"A simple displayhook that publishes the object's repr over a ZeroMQ\n    socket.\"\"\"\n    topic=b'execute_result'\n\n    def __init__(self, session, pub_socket):\n        self.session = session\n        self.pub_socket = pub_socket\n        self.parent_header = {}\n\n    def __call__(self, obj):\n        if obj is None:\n            return\n\n        builtin_mod._ = obj\n        sys.stdout.flush()\n        sys.stderr.flush()\n        msg = self.session.send(self.pub_socket, u'execute_result', {u'data':repr(obj)},\n                               parent=self.parent_header, ident=self.topic)\n\n    def set_parent(self, parent):\n        self.parent_header = extract_header(parent)\n\n\nclass ZMQShellDisplayHook(DisplayHook):\n    \"\"\"A displayhook subclass that publishes data using ZeroMQ. This is intended\n    to work with an InteractiveShell instance. It sends a dict of different\n    representations of the object.\"\"\"\n    topic=None\n\n    session = Instance(Session)\n    pub_socket = Instance(SocketABC)\n    parent_header = Dict({})\n\n    def set_parent(self, parent):\n        \"\"\"Set the parent for outbound messages.\"\"\"\n        self.parent_header = extract_header(parent)\n\n    def start_displayhook(self):\n        self.msg = self.session.msg(u'execute_result', {\n            'data': {},\n            'metadata': {},\n        }, parent=self.parent_header)\n\n    def write_output_prompt(self):\n        \"\"\"Write the output prompt.\"\"\"\n        self.msg['content']['execution_count'] = self.prompt_count\n\n    def write_format_data(self, format_dict, md_dict=None):\n        self.msg['content']['data'] = encode_images(format_dict)\n        self.msg['content']['metadata'] = md_dict\n\n    def finish_displayhook(self):\n        \"\"\"Finish up all displayhook activities.\"\"\"\n        sys.stdout.flush()\n        sys.stderr.flush()\n        if self.msg['content']['data']:\n            self.session.send(self.pub_socket, self.msg, ident=self.topic)\n        self.msg = None\n\n", "path": "IPython/kernel/zmq/displayhook.py"}]}