From c92edbc2465fac2de943614224431f5943d77a89 2014-01-17 17:35:04
From: Jonathan Frederic <jdfreder@calpoly.edu>
Date: 2014-01-17 17:35:04
Subject: [PATCH] Better implementation of widget _repr_ style display logic.

---

diff --git a/IPython/core/displayhook.py b/IPython/core/displayhook.py
index 0ecfb3b..8e873ed 100644
--- a/IPython/core/displayhook.py
+++ b/IPython/core/displayhook.py
@@ -241,13 +241,16 @@ class DisplayHook(Configurable):
         """
         self.check_for_underscore()
         if result is not None and not self.quiet():
-            self.start_displayhook()
-            self.write_output_prompt()
-            format_dict, md_dict = self.compute_format_data(result)
-            self.write_format_data(format_dict, md_dict)
-            self.update_user_ns(result)
-            self.log_output(format_dict)
-            self.finish_displayhook()
+            # If _ipython_display_ is defined, use that to display this object.  If
+            # it returns NotImplemented, use the _repr_ logic (default).
+            if not hasattr(result, '_ipython_display_') or result._ipython_display_() == NotImplemented:
+                self.start_displayhook()
+                self.write_output_prompt()
+                format_dict, md_dict = self.compute_format_data(result)
+                self.write_format_data(format_dict, md_dict)
+                self.update_user_ns(result)
+                self.log_output(format_dict)
+                self.finish_displayhook()
 
     def flush(self):
         if not self.do_full_cache:
diff --git a/IPython/core/formatters.py b/IPython/core/formatters.py
index f9614b2..b4ccf03 100644
--- a/IPython/core/formatters.py
+++ b/IPython/core/formatters.py
@@ -149,30 +149,27 @@ class DisplayFormatter(Configurable):
         format_dict = {}
         md_dict = {}
 
-        # If _ipython_display_ is defined, use that to display this object.  If
-        # it returns NotImplemented, use the _repr_ logic (default).
-        if not hasattr(obj, '_ipython_display_') or obj._ipython_display_(**kwargs) == NotImplemented:
-            for format_type, formatter in self.formatters.items():
-                if include and format_type not in include:
-                    continue
-                if exclude and format_type in exclude:
-                    continue
-                
-                md = None
-                try:
-                    data = formatter(obj)
-                except:
-                    # FIXME: log the exception
-                    raise
-                
-                # formatters can return raw data or (data, metadata)
-                if isinstance(data, tuple) and len(data) == 2:
-                    data, md = data
-                
-                if data is not None:
-                    format_dict[format_type] = data
-                if md is not None:
-                    md_dict[format_type] = md
+        for format_type, formatter in self.formatters.items():
+            if include and format_type not in include:
+                continue
+            if exclude and format_type in exclude:
+                continue
+            
+            md = None
+            try:
+                data = formatter(obj)
+            except:
+                # FIXME: log the exception
+                raise
+            
+            # formatters can return raw data or (data, metadata)
+            if isinstance(data, tuple) and len(data) == 2:
+                data, md = data
+            
+            if data is not None:
+                format_dict[format_type] = data
+            if md is not None:
+                md_dict[format_type] = md
             
         return format_dict, md_dict