##// END OF EJS Templates
Define `_repr_mimebundle_`...
Min RK -
Show More
@@ -51,12 +51,17 b' class DisplayFormatter(Configurable):'
51 formatter.enabled = True
51 formatter.enabled = True
52 else:
52 else:
53 formatter.enabled = False
53 formatter.enabled = False
54
54
55 ipython_display_formatter = ForwardDeclaredInstance('FormatterABC')
55 ipython_display_formatter = ForwardDeclaredInstance('FormatterABC')
56 @default('ipython_display_formatter')
56 @default('ipython_display_formatter')
57 def _default_formatter(self):
57 def _default_formatter(self):
58 return IPythonDisplayFormatter(parent=self)
58 return IPythonDisplayFormatter(parent=self)
59
59
60 mimebundle_formatter = ForwardDeclaredInstance('FormatterABC')
61 @default('mimebundle_formatter')
62 def _default_mime_formatter(self):
63 return MimeBundleFormatter(parent=self)
64
60 # A dict of formatter whose keys are format types (MIME types) and whose
65 # A dict of formatter whose keys are format types (MIME types) and whose
61 # values are subclasses of BaseFormatter.
66 # values are subclasses of BaseFormatter.
62 formatters = Dict()
67 formatters = Dict()
@@ -131,8 +136,13 b' class DisplayFormatter(Configurable):'
131 if self.ipython_display_formatter(obj):
136 if self.ipython_display_formatter(obj):
132 # object handled itself, don't proceed
137 # object handled itself, don't proceed
133 return {}, {}
138 return {}, {}
134
139
140 format_dict, md_dict = self.mimebundle_formatter(obj)
141
135 for format_type, formatter in self.formatters.items():
142 for format_type, formatter in self.formatters.items():
143 if format_type in format_dict:
144 # already got it from mimebundle, don't render again
145 continue
136 if include and format_type not in include:
146 if include and format_type not in include:
137 continue
147 continue
138 if exclude and format_type in exclude:
148 if exclude and format_type in exclude:
@@ -843,7 +853,7 b' class PDFFormatter(BaseFormatter):'
843 _return_type = (bytes, str)
853 _return_type = (bytes, str)
844
854
845 class IPythonDisplayFormatter(BaseFormatter):
855 class IPythonDisplayFormatter(BaseFormatter):
846 """A Formatter for objects that know how to display themselves.
856 """An escape-hatch Formatter for objects that know how to display themselves.
847
857
848 To define the callables that compute the representation of your
858 To define the callables that compute the representation of your
849 objects, define a :meth:`_ipython_display_` method or use the :meth:`for_type`
859 objects, define a :meth:`_ipython_display_` method or use the :meth:`for_type`
@@ -853,10 +863,16 b' class IPythonDisplayFormatter(BaseFormatter):'
853
863
854 This display formatter has highest priority.
864 This display formatter has highest priority.
855 If it fires, no other display formatter will be called.
865 If it fires, no other display formatter will be called.
866
867 Prior to IPython 6.1, `_ipython_display_` was the only way to display custom mime-types
868 without registering a new Formatter.
869
870 IPython 6.1 introduces `_repr_mimebundle_` for displaying custom mime-types,
871 so `_ipython_display_` should only be used for objects that require unusual
872 display patterns, such as multiple display calls.
856 """
873 """
857 print_method = ObjectName('_ipython_display_')
874 print_method = ObjectName('_ipython_display_')
858 _return_type = (type(None), bool)
875 _return_type = (type(None), bool)
859
860
876
861 @catch_format_error
877 @catch_format_error
862 def __call__(self, obj):
878 def __call__(self, obj):
@@ -877,6 +893,34 b' class IPythonDisplayFormatter(BaseFormatter):'
877 return True
893 return True
878
894
879
895
896 class MimeBundleFormatter(BaseFormatter):
897 """A Formatter for arbitrary mime-types.
898
899 Unlike other `_repr_<mimetype>_` methods,
900 `_repr_mimebundle_` should return mime-bundle data,
901 either the mime-keyed `data` dictionary or the tuple `(data, metadata)`.
902 Any mime-type is valid.
903
904 To define the callables that compute the mime-bundle representation of your
905 objects, define a :meth:`_repr_mimebundle_` method or use the :meth:`for_type`
906 or :meth:`for_type_by_name` methods to register functions that handle
907 this.
908
909 .. versionadded:: 6.1
910 """
911 print_method = ObjectName('_repr_mimebundle_')
912 _return_type = dict
913
914 def _check_return(self, r, obj):
915 r = super(MimeBundleFormatter, self)._check_return(r, obj)
916 # always return (data, metadata):
917 if r is None:
918 return {}, {}
919 if not isinstance(r, tuple):
920 return r, {}
921 return r
922
923
880 FormatterABC.register(BaseFormatter)
924 FormatterABC.register(BaseFormatter)
881 FormatterABC.register(PlainTextFormatter)
925 FormatterABC.register(PlainTextFormatter)
882 FormatterABC.register(HTMLFormatter)
926 FormatterABC.register(HTMLFormatter)
@@ -889,6 +933,7 b' FormatterABC.register(LatexFormatter)'
889 FormatterABC.register(JSONFormatter)
933 FormatterABC.register(JSONFormatter)
890 FormatterABC.register(JavascriptFormatter)
934 FormatterABC.register(JavascriptFormatter)
891 FormatterABC.register(IPythonDisplayFormatter)
935 FormatterABC.register(IPythonDisplayFormatter)
936 FormatterABC.register(MimeBundleFormatter)
892
937
893
938
894 def format_display_data(obj, include=None, exclude=None):
939 def format_display_data(obj, include=None, exclude=None):
@@ -436,4 +436,55 b' def test_json_as_string_deprecated():'
436 d = f(JSONString())
436 d = f(JSONString())
437 nt.assert_equal(d, {})
437 nt.assert_equal(d, {})
438 nt.assert_equal(len(w), 1)
438 nt.assert_equal(len(w), 1)
439 No newline at end of file
439
440
441 def test_repr_mime():
442 class HasReprMime(object):
443 def _repr_mimebundle_(self):
444 return {
445 'application/json+test.v2': {
446 'x': 'y'
447 }
448 }
449
450 def _repr_html_(self):
451 return '<b>hi!</b>'
452
453 f = get_ipython().display_formatter
454 html_f = f.formatters['text/html']
455 save_enabled = html_f.enabled
456 html_f.enabled = True
457 obj = HasReprMime()
458 d, md = f.format(obj)
459 html_f.enabled = save_enabled
460
461 nt.assert_equal(sorted(d), ['application/json+test.v2', 'text/html', 'text/plain'])
462 nt.assert_equal(md, {})
463
464
465 def test_repr_mime_meta():
466 class HasReprMimeMeta(object):
467 def _repr_mimebundle_(self):
468 data = {
469 'image/png': 'base64-image-data',
470 }
471 metadata = {
472 'image/png': {
473 'width': 5,
474 'height': 10,
475 }
476 }
477 return (data, metadata)
478
479 f = get_ipython().display_formatter
480 obj = HasReprMimeMeta()
481 d, md = f.format(obj)
482 nt.assert_equal(sorted(d), ['image/png', 'text/plain'])
483 nt.assert_equal(md, {
484 'image/png': {
485 'width': 5,
486 'height': 10,
487 }
488 })
489
490
General Comments 0
You need to be logged in to leave comments. Login now