##// END OF EJS Templates
Merge pull request #10550 from ipython/auto-backport-of-pr-10539...
Min RK -
r23637:7b4c71ff merge
parent child Browse files
Show More
@@ -93,7 +93,7 b' class DisplayFormatter(Configurable):'
93
93
94 By default all format types will be computed.
94 By default all format types will be computed.
95
95
96 The following MIME types are currently implemented:
96 The following MIME types are usually implemented:
97
97
98 * text/plain
98 * text/plain
99 * text/html
99 * text/html
@@ -110,14 +110,15 b' class DisplayFormatter(Configurable):'
110 ----------
110 ----------
111 obj : object
111 obj : object
112 The Python object whose format data will be computed.
112 The Python object whose format data will be computed.
113 include : list or tuple, optional
113 include : list, tuple or set; optional
114 A list of format type strings (MIME types) to include in the
114 A list of format type strings (MIME types) to include in the
115 format data dict. If this is set *only* the format types included
115 format data dict. If this is set *only* the format types included
116 in this list will be computed.
116 in this list will be computed.
117 exclude : list or tuple, optional
117 exclude : list, tuple or set; optional
118 A list of format type string (MIME types) to exclude in the format
118 A list of format type string (MIME types) to exclude in the format
119 data dict. If this is set all format types will be computed,
119 data dict. If this is set all format types will be computed,
120 except for those included in this argument.
120 except for those included in this argument.
121 Mimetypes present in exclude will take precedence over the ones in include
121
122
122 Returns
123 Returns
123 -------
124 -------
@@ -131,6 +132,15 b' class DisplayFormatter(Configurable):'
131
132
132 metadata_dict is a dictionary of metadata about each mime-type output.
133 metadata_dict is a dictionary of metadata about each mime-type output.
133 Its keys will be a strict subset of the keys in format_dict.
134 Its keys will be a strict subset of the keys in format_dict.
135
136 Notes
137 -----
138
139 If an object implement `_repr_mimebundle_` as well as various
140 `_repr_*_`, the data returned by `_repr_mimebundle_` will take
141 precedence and the corresponding `_repr_*_` for this mimetype will
142 not be called.
143
134 """
144 """
135 format_dict = {}
145 format_dict = {}
136 md_dict = {}
146 md_dict = {}
@@ -141,6 +151,14 b' class DisplayFormatter(Configurable):'
141
151
142 format_dict, md_dict = self.mimebundle_formatter(obj)
152 format_dict, md_dict = self.mimebundle_formatter(obj)
143
153
154 if format_dict or md_dict:
155 if include:
156 format_dict = {k:v for k,v in format_dict.items() if k in include}
157 md_dict = {k:v for k,v in md_dict.items() if k in include}
158 if exclude:
159 format_dict = {k:v for k,v in format_dict.items() if k not in exclude}
160 md_dict = {k:v for k,v in md_dict.items() if k not in exclude}
161
144 for format_type, formatter in self.formatters.items():
162 for format_type, formatter in self.formatters.items():
145 if format_type in format_dict:
163 if format_type in format_dict:
146 # already got it from mimebundle, don't render again
164 # already got it from mimebundle, don't render again
@@ -165,7 +183,6 b' class DisplayFormatter(Configurable):'
165 format_dict[format_type] = data
183 format_dict[format_type] = data
166 if md is not None:
184 if md is not None:
167 md_dict[format_type] = md
185 md_dict[format_type] = md
168
169 return format_dict, md_dict
186 return format_dict, md_dict
170
187
171 @property
188 @property
@@ -989,4 +1006,3 b' def format_display_data(obj, include=None, exclude=None):'
989 include,
1006 include,
990 exclude
1007 exclude
991 )
1008 )
992
@@ -444,9 +444,13 b' def test_repr_mime():'
444 return {
444 return {
445 'application/json+test.v2': {
445 'application/json+test.v2': {
446 'x': 'y'
446 'x': 'y'
447 }
447 },
448 'plain/text' : '<HasReprMime>',
449 'image/png' : 'i-overwrite'
448 }
450 }
449
451
452 def _repr_png_(self):
453 return 'should-be-overwritten'
450 def _repr_html_(self):
454 def _repr_html_(self):
451 return '<b>hi!</b>'
455 return '<b>hi!</b>'
452
456
@@ -458,9 +462,18 b' def test_repr_mime():'
458 d, md = f.format(obj)
462 d, md = f.format(obj)
459 html_f.enabled = save_enabled
463 html_f.enabled = save_enabled
460
464
461 nt.assert_equal(sorted(d), ['application/json+test.v2', 'text/html', 'text/plain'])
465 nt.assert_equal(sorted(d), ['application/json+test.v2',
466 'image/png',
467 'plain/text',
468 'text/html',
469 'text/plain'])
462 nt.assert_equal(md, {})
470 nt.assert_equal(md, {})
463
471
472 d, md = f.format(obj, include={'image/png'})
473 nt.assert_equal(list(d.keys()), ['image/png'],
474 'Include should filter out even things from repr_mimebundle')
475 nt.assert_equal(d['image/png'], 'i-overwrite', '_repr_mimebundle_ take precedence')
476
464
477
465 def test_repr_mime_meta():
478 def test_repr_mime_meta():
466 class HasReprMimeMeta(object):
479 class HasReprMimeMeta(object):
General Comments 0
You need to be logged in to leave comments. Login now