##// END OF EJS Templates
ENH: Extend the DisplayHook.compute_result_repr() and write_result_repr() methods to produce and consume the lists of extra formats. Use this capability in the ZMQ shell. Document the extension to the messaging format.
Robert Kern -
Show More
@@ -208,9 +208,19 b' class DisplayHook(Configurable):'
208 # But avoid extraneous empty lines.
208 # But avoid extraneous empty lines.
209 result_repr = '\n' + result_repr
209 result_repr = '\n' + result_repr
210
210
211 return result_repr
211 extra_formats = []
212 for f in self.extra_formatters:
213 try:
214 data = f(result)
215 except Exception:
216 # FIXME: log the exception.
217 continue
218 if data is not None:
219 extra_formats.append((f.id, f.format, data))
220
221 return result_repr, extra_formats
212
222
213 def write_result_repr(self, result_repr):
223 def write_result_repr(self, result_repr, extra_formats):
214 # We want to print because we want to always make sure we have a
224 # We want to print because we want to always make sure we have a
215 # newline, even if all the prompt separators are ''. This is the
225 # newline, even if all the prompt separators are ''. This is the
216 # standard IPython behavior.
226 # standard IPython behavior.
@@ -265,8 +275,8 b' class DisplayHook(Configurable):'
265 if result is not None and not self.quiet():
275 if result is not None and not self.quiet():
266 self.start_displayhook()
276 self.start_displayhook()
267 self.write_output_prompt()
277 self.write_output_prompt()
268 result_repr = self.compute_result_repr(result)
278 result_repr, extra_formats = self.compute_result_repr(result)
269 self.write_result_repr(result_repr)
279 self.write_result_repr(result_repr, extra_formats)
270 self.update_user_ns(result)
280 self.update_user_ns(result)
271 self.log_output(result)
281 self.log_output(result)
272 self.finish_displayhook()
282 self.finish_displayhook()
@@ -65,8 +65,9 b' class ZMQDisplayHook(DisplayHook):'
65 if self.do_full_cache:
65 if self.do_full_cache:
66 self.msg['content']['execution_count'] = self.prompt_count
66 self.msg['content']['execution_count'] = self.prompt_count
67
67
68 def write_result_repr(self, result_repr):
68 def write_result_repr(self, result_repr, extra_formats):
69 self.msg['content']['data'] = result_repr
69 self.msg['content']['data'] = result_repr
70 self.msg['content']['extra_formats'] = extra_formats
70
71
71 def finish_displayhook(self):
72 def finish_displayhook(self):
72 """Finish up all displayhook activities."""
73 """Finish up all displayhook activities."""
@@ -725,16 +725,35 b' case, the kernel instantiates as ``sys.displayhook`` an object which has'
725 similar behavior, but which instead of printing to stdout, broadcasts these
725 similar behavior, but which instead of printing to stdout, broadcasts these
726 values as ``pyout`` messages for clients to display appropriately.
726 values as ``pyout`` messages for clients to display appropriately.
727
727
728 IPython's displayhook can handle multiple simultaneous formats depending on its
729 configuration. The default pretty-printed repr text is always given with the
730 ``data`` entry in this message. Any other formats are provided in the
731 ``extra_formats`` list. Frontends are free to display any or all of these
732 according to its capabilities. ``extra_formats`` list contains 3-tuples of an ID
733 string, a type string, and the data. The ID is unique to the formatter
734 implementation that created the data. Frontends will typically ignore the ID
735 unless if it has requested a particular formatter. The type string tells the
736 frontend how to interpret the data. It is often, but not always a MIME type.
737 Frontends should ignore types that it does not understand. The data itself is
738 any JSON object and depends on the format. It is often, but not always a string.
739
728 Message type: ``pyout``::
740 Message type: ``pyout``::
729
741
730 content = {
742 content = {
731 # The data is typically the repr() of the object.
743 # The data is typically the repr() of the object. It should be displayed
732 'data' : str,
744 # as monospaced text.
745 'data' : str,
733
746
734 # The counter for this execution is also provided so that clients can
747 # The counter for this execution is also provided so that clients can
735 # display it, since IPython automatically creates variables called _N (for
748 # display it, since IPython automatically creates variables called _N
736 # prompt N).
749 # (for prompt N).
737 'execution_count' : int,
750 'execution_count' : int,
751
752 # Any extra formats.
753 # The tuples are of the form (ID, type, data).
754 'extra_formats' : [
755 [str, str, object]
756 ]
738 }
757 }
739
758
740 Python errors
759 Python errors
General Comments 0
You need to be logged in to leave comments. Login now