Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

ZMQ displayhook #527

Merged
merged 3 commits into from

2 participants

@takluyver
Owner

Move displayhook for ZMQ shell to zmq.displayhook, and rename to make the difference clearer.

Straightforward tidying up. Because I went looking for the displayhook used by ZMQInteractiveShell, and spent a while changing the wrong one before I worked out that it was in zmqshell.py. This puts them both in zmq.displayhook, and attempts to clarify the difference.

See #512

@minrk
Owner

This makes sense.

But you did move _encode_png, and it's still used in zmqshell but no longer available:
https://github.com/takluyver/ipython/blob/48c2330913f40fea5b69d278ac949149ff50da8f/IPython/zmq/zmqshell.py#L68

@takluyver
Owner

Thanks, Min. I've added an import there.

@takluyver
Owner

Rebased now that newapp is merged.

@minrk
Owner

This makes sense, and I don't see a problem. Go ahead and merge, then.

@takluyver takluyver merged commit c598e48 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
4 IPython/parallel/engine/engine.py
@@ -41,9 +41,9 @@ class EngineFactory(RegistrationFactory):
out_stream_factory=Type('IPython.zmq.iostream.OutStream', config=True,
help="""The OutStream for handling stdout/err.
Typically 'IPython.zmq.iostream.OutStream'""")
- display_hook_factory=Type('IPython.zmq.displayhook.DisplayHook', config=True,
+ display_hook_factory=Type('IPython.zmq.displayhook.ZMQDisplayHook', config=True,
help="""The class for handling displayhook.
- Typically 'IPython.zmq.displayhook.DisplayHook'""")
+ Typically 'IPython.zmq.displayhook.ZMQDisplayHook'""")
location=Unicode(config=True,
help="""The location (an IP address) of the controller. This is
used for disambiguating URLs, to determine whether
View
49 IPython/zmq/displayhook.py
@@ -1,9 +1,13 @@
import __builtin__
+from base64 import encodestring
-from session import extract_header
-
-class DisplayHook(object):
+from IPython.core.displayhook import DisplayHook
+from IPython.utils.traitlets import Instance, Dict
+from session import extract_header, Session
+class ZMQDisplayHook(object):
+ """A simple displayhook that publishes the object's repr over a ZeroMQ
+ socket."""
topic=None
def __init__(self, session, pub_socket):
@@ -20,4 +24,41 @@ def __call__(self, obj):
parent=self.parent_header, ident=self.topic)
def set_parent(self, parent):
- self.parent_header = extract_header(parent)
+ self.parent_header = extract_header(parent)
+
+
+def _encode_png(data):
+ pngdata = data.get('image/png')
+ if pngdata is not None:
+ data['image/png'] = encodestring(pngdata)
+
+class ZMQShellDisplayHook(DisplayHook):
+ """A displayhook subclass that publishes data using ZeroMQ. This is intended
+ to work with an InteractiveShell instance. It sends a dict of different
+ representations of the object."""
+
+ session = Instance(Session)
+ pub_socket = Instance('zmq.Socket')
+ parent_header = Dict({})
+
+ def set_parent(self, parent):
+ """Set the parent for outbound messages."""
+ self.parent_header = extract_header(parent)
+
+ def start_displayhook(self):
+ self.msg = self.session.msg(u'pyout', {}, parent=self.parent_header)
+
+ def write_output_prompt(self):
+ """Write the output prompt."""
+ if self.do_full_cache:
+ self.msg['content']['execution_count'] = self.prompt_count
+
+ def write_format_data(self, format_dict):
+ pngdata = format_dict.get('image/png')
+ _encode_png(format_dict)
+ self.msg['content']['data'] = format_dict
+
+ def finish_displayhook(self):
+ """Finish up all displayhook activities."""
+ self.session.send(self.pub_socket, self.msg)
+ self.msg = None
View
1  IPython/zmq/entry_point.py
@@ -13,7 +13,6 @@
from parentpoller import ParentPollerWindows
-
def base_launch_kernel(code, shell_port=0, iopub_port=0, stdin_port=0, hb_port=0,
ip=None, stdin=None, stdout=None, stderr=None,
executable=None, independent=False, extra_arguments=[]):
View
2  IPython/zmq/kernelapp.py
@@ -95,7 +95,7 @@ class KernelApp(BaseIPythonApplication):
no_stderr = Bool(False, config=True, help="redirect stderr to the null device")
outstream_class = Unicode('IPython.zmq.iostream.OutStream', config=True,
help="The importstring for the OutStream factory")
- displayhook_class = Unicode('IPython.zmq.displayhook.DisplayHook', config=True,
+ displayhook_class = Unicode('IPython.zmq.displayhook.ZMQDisplayHook', config=True,
help="The importstring for the DisplayHook factory")
# polling
View
41 IPython/zmq/zmqshell.py
@@ -16,7 +16,6 @@
from __future__ import print_function
# Stdlib
-from base64 import encodestring
import inspect
import os
@@ -26,7 +25,6 @@
)
from IPython.core import page
from IPython.core.autocall import ZMQExitAutocall
-from IPython.core.displayhook import DisplayHook
from IPython.core.displaypub import DisplayPublisher
from IPython.core.macro import Macro
from IPython.core.magic import MacroToEdit
@@ -35,6 +33,7 @@
from IPython.utils.path import get_py_filename
from IPython.utils.traitlets import Instance, Type, Dict
from IPython.utils.warn import warn
+from IPython.zmq.displayhook import ZMQShellDisplayHook, _encode_png
from IPython.zmq.session import extract_header
from session import Session
@@ -49,42 +48,6 @@
# Functions and classes
#-----------------------------------------------------------------------------
-def _encode_png(data):
- pngdata = data.get('image/png')
- if pngdata is not None:
- data['image/png'] = encodestring(pngdata)
-
-
-class ZMQDisplayHook(DisplayHook):
- """A displayhook subclass that publishes data using ZeroMQ."""
-
- session = Instance(Session)
- pub_socket = Instance('zmq.Socket')
- parent_header = Dict({})
-
- def set_parent(self, parent):
- """Set the parent for outbound messages."""
- self.parent_header = extract_header(parent)
-
- def start_displayhook(self):
- self.msg = self.session.msg(u'pyout', {}, parent=self.parent_header)
-
- def write_output_prompt(self):
- """Write the output prompt."""
- if self.do_full_cache:
- self.msg['content']['execution_count'] = self.prompt_count
-
- def write_format_data(self, format_dict):
- pngdata = format_dict.get('image/png')
- _encode_png(format_dict)
- self.msg['content']['data'] = format_dict
-
- def finish_displayhook(self):
- """Finish up all displayhook activities."""
- self.session.send(self.pub_socket, self.msg)
- self.msg = None
-
-
class ZMQDisplayPublisher(DisplayPublisher):
"""A display publisher that publishes data using a ZeroMQ PUB socket."""
@@ -114,7 +77,7 @@ def publish(self, source, data, metadata=None):
class ZMQInteractiveShell(InteractiveShell):
"""A subclass of InteractiveShell for ZMQ."""
- displayhook_class = Type(ZMQDisplayHook)
+ displayhook_class = Type(ZMQShellDisplayHook)
display_pub_class = Type(ZMQDisplayPublisher)
exiter = Instance(ZMQExitAutocall)
Something went wrong with that request. Please try again.