Show More
@@ -164,20 +164,13 b' def display(*objs, **kwargs):' | |||||
164 | format = InteractiveShell.instance().display_formatter.format |
|
164 | format = InteractiveShell.instance().display_formatter.format | |
165 |
|
165 | |||
166 | for obj in objs: |
|
166 | for obj in objs: | |
167 |
|
||||
168 | # If _ipython_display_ is defined, use that to display this object. |
|
|||
169 | display_method = _safe_get_formatter_method(obj, '_ipython_display_') |
|
|||
170 | if display_method is not None: |
|
|||
171 | try: |
|
|||
172 | display_method(**kwargs) |
|
|||
173 | except NotImplementedError: |
|
|||
174 | pass |
|
|||
175 | else: |
|
|||
176 | continue |
|
|||
177 | if raw: |
|
167 | if raw: | |
178 | publish_display_data(data=obj, metadata=metadata) |
|
168 | publish_display_data(data=obj, metadata=metadata) | |
179 | else: |
|
169 | else: | |
180 | format_dict, md_dict = format(obj, include=include, exclude=exclude) |
|
170 | format_dict, md_dict = format(obj, include=include, exclude=exclude) | |
|
171 | if not format_dict: | |||
|
172 | # nothing to display (e.g. _ipython_display_ took over) | |||
|
173 | continue | |||
181 | if metadata: |
|
174 | if metadata: | |
182 | # kwarg-specified metadata gets precedence |
|
175 | # kwarg-specified metadata gets precedence | |
183 | _merge(md_dict, metadata) |
|
176 | _merge(md_dict, metadata) |
@@ -221,20 +221,13 b' class DisplayHook(Configurable):' | |||||
221 | """ |
|
221 | """ | |
222 | self.check_for_underscore() |
|
222 | self.check_for_underscore() | |
223 | if result is not None and not self.quiet(): |
|
223 | if result is not None and not self.quiet(): | |
224 | # If _ipython_display_ is defined, use that to display this object. |
|
|||
225 | display_method = _safe_get_formatter_method(result, '_ipython_display_') |
|
|||
226 | if display_method is not None: |
|
|||
227 | try: |
|
|||
228 | return display_method() |
|
|||
229 | except NotImplementedError: |
|
|||
230 | pass |
|
|||
231 |
|
||||
232 | self.start_displayhook() |
|
224 | self.start_displayhook() | |
233 | self.write_output_prompt() |
|
225 | self.write_output_prompt() | |
234 | format_dict, md_dict = self.compute_format_data(result) |
|
226 | format_dict, md_dict = self.compute_format_data(result) | |
235 | self.write_format_data(format_dict, md_dict) |
|
|||
236 | self.update_user_ns(result) |
|
227 | self.update_user_ns(result) | |
237 |
|
|
228 | if format_dict: | |
|
229 | self.write_format_data(format_dict, md_dict) | |||
|
230 | self.log_output(format_dict) | |||
238 | self.finish_displayhook() |
|
231 | self.finish_displayhook() | |
239 |
|
232 | |||
240 | def cull_cache(self): |
|
233 | def cull_cache(self): |
@@ -24,6 +24,7 b' from IPython.core.getipython import get_ipython' | |||||
24 | from IPython.lib import pretty |
|
24 | from IPython.lib import pretty | |
25 | from IPython.utils.traitlets import ( |
|
25 | from IPython.utils.traitlets import ( | |
26 | Bool, Dict, Integer, Unicode, CUnicode, ObjectName, List, |
|
26 | Bool, Dict, Integer, Unicode, CUnicode, ObjectName, List, | |
|
27 | Instance, | |||
27 | ) |
|
28 | ) | |
28 | from IPython.utils.py3compat import ( |
|
29 | from IPython.utils.py3compat import ( | |
29 | unicode_to_str, with_metaclass, PY3, string_types, unicode_type, |
|
30 | unicode_to_str, with_metaclass, PY3, string_types, unicode_type, | |
@@ -89,6 +90,10 b' class DisplayFormatter(Configurable):' | |||||
89 | else: |
|
90 | else: | |
90 | formatter.enabled = False |
|
91 | formatter.enabled = False | |
91 |
|
92 | |||
|
93 | self_formatter = Instance(__name__ +'.SelfDisplayingFormatter') | |||
|
94 | def _self_formatter_default(self): | |||
|
95 | return SelfDisplayingFormatter(parent=self) | |||
|
96 | ||||
92 | # A dict of formatter whose keys are format types (MIME types) and whose |
|
97 | # A dict of formatter whose keys are format types (MIME types) and whose | |
93 | # values are subclasses of BaseFormatter. |
|
98 | # values are subclasses of BaseFormatter. | |
94 | formatters = Dict() |
|
99 | formatters = Dict() | |
@@ -158,7 +163,11 b' class DisplayFormatter(Configurable):' | |||||
158 | """ |
|
163 | """ | |
159 | format_dict = {} |
|
164 | format_dict = {} | |
160 | md_dict = {} |
|
165 | md_dict = {} | |
161 |
|
166 | |||
|
167 | if self.self_formatter(obj): | |||
|
168 | # object handled itself, don't proceed | |||
|
169 | return {}, {} | |||
|
170 | ||||
162 | for format_type, formatter in self.formatters.items(): |
|
171 | for format_type, formatter in self.formatters.items(): | |
163 | if include and format_type not in include: |
|
172 | if include and format_type not in include: | |
164 | continue |
|
173 | continue | |
@@ -831,6 +840,40 b' class PDFFormatter(BaseFormatter):' | |||||
831 |
|
840 | |||
832 | _return_type = (bytes, unicode_type) |
|
841 | _return_type = (bytes, unicode_type) | |
833 |
|
842 | |||
|
843 | class SelfDisplayingFormatter(BaseFormatter): | |||
|
844 | """A Formatter for objects that know how to display themselves. | |||
|
845 | ||||
|
846 | To define the callables that compute the representation of your | |||
|
847 | objects, define a :meth:`_ipython_display_` method or use the :meth:`for_type` | |||
|
848 | or :meth:`for_type_by_name` methods to register functions that handle | |||
|
849 | this. Unlike mime-type displays, this method should not return anything, | |||
|
850 | instead calling any appropriate display methods itself. | |||
|
851 | ||||
|
852 | This display formatter has highest priority. | |||
|
853 | If it fires, no other display formatter will be called. | |||
|
854 | """ | |||
|
855 | print_method = ObjectName('_ipython_display_') | |||
|
856 | _return_type = (type(None), bool) | |||
|
857 | ||||
|
858 | ||||
|
859 | @warn_format_error | |||
|
860 | def __call__(self, obj): | |||
|
861 | """Compute the format for an object.""" | |||
|
862 | if self.enabled: | |||
|
863 | # lookup registered printer | |||
|
864 | try: | |||
|
865 | printer = self.lookup(obj) | |||
|
866 | except KeyError: | |||
|
867 | pass | |||
|
868 | else: | |||
|
869 | printer(obj) | |||
|
870 | return True | |||
|
871 | # Finally look for special method names | |||
|
872 | method = _safe_get_formatter_method(obj, self.print_method) | |||
|
873 | if method is not None: | |||
|
874 | method() | |||
|
875 | return True | |||
|
876 | ||||
834 |
|
877 | |||
835 | FormatterABC.register(BaseFormatter) |
|
878 | FormatterABC.register(BaseFormatter) | |
836 | FormatterABC.register(PlainTextFormatter) |
|
879 | FormatterABC.register(PlainTextFormatter) | |
@@ -843,6 +886,7 b' FormatterABC.register(JPEGFormatter)' | |||||
843 | FormatterABC.register(LatexFormatter) |
|
886 | FormatterABC.register(LatexFormatter) | |
844 | FormatterABC.register(JSONFormatter) |
|
887 | FormatterABC.register(JSONFormatter) | |
845 | FormatterABC.register(JavascriptFormatter) |
|
888 | FormatterABC.register(JavascriptFormatter) | |
|
889 | FormatterABC.register(SelfDisplayingFormatter) | |||
846 |
|
890 | |||
847 |
|
891 | |||
848 | def format_display_data(obj, include=None, exclude=None): |
|
892 | def format_display_data(obj, include=None, exclude=None): |
General Comments 0
You need to be logged in to leave comments.
Login now