An escape-hatch Formatter for objects that know how to display themselves. To define the callables that compute the representation of your objects, define a :meth:`_ipython_display_` method or use the :meth:`for_type` or :meth:`for_type_by_name` methods to register functions that ha
| 879 | _return_type = (bytes, str) |
| 880 | |
| 881 | class IPythonDisplayFormatter(BaseFormatter): |
| 882 | """An escape-hatch Formatter for objects that know how to display themselves. |
| 883 | |
| 884 | To define the callables that compute the representation of your |
| 885 | objects, define a :meth:`_ipython_display_` method or use the :meth:`for_type` |
| 886 | or :meth:`for_type_by_name` methods to register functions that handle |
| 887 | this. Unlike mime-type displays, this method should not return anything, |
| 888 | instead calling any appropriate display methods itself. |
| 889 | |
| 890 | This display formatter has highest priority. |
| 891 | If it fires, no other display formatter will be called. |
| 892 | |
| 893 | Prior to IPython 6.1, `_ipython_display_` was the only way to display custom mime-types |
| 894 | without registering a new Formatter. |
| 895 | |
| 896 | IPython 6.1 introduces `_repr_mimebundle_` for displaying custom mime-types, |
| 897 | so `_ipython_display_` should only be used for objects that require unusual |
| 898 | display patterns, such as multiple display calls. |
| 899 | """ |
| 900 | print_method = ObjectName('_ipython_display_') |
| 901 | _return_type = (type(None), bool) |
| 902 | |
| 903 | @catch_format_error |
| 904 | def __call__(self, obj): |
| 905 | """Compute the format for an object.""" |
| 906 | if self.enabled: |
| 907 | # lookup registered printer |
| 908 | try: |
| 909 | printer = self.lookup(obj) |
| 910 | except KeyError: |
| 911 | pass |
| 912 | else: |
| 913 | printer(obj) |
| 914 | return True |
| 915 | # Finally look for special method names |
| 916 | method = get_real_method(obj, self.print_method) |
| 917 | if method is not None: |
| 918 | method() |
| 919 | return True |
| 920 | |
| 921 | |
| 922 | class MimeBundleFormatter(BaseFormatter): |