##// END OF EJS Templates
Merge pull request #10539 from Carreau/fix-repr-mime...
Min RK -
r23616:9e7fbf0b merge
parent child Browse files
Show More
@@ -91,7 +91,7 class DisplayFormatter(Configurable):
91 91
92 92 By default all format types will be computed.
93 93
94 The following MIME types are currently implemented:
94 The following MIME types are usually implemented:
95 95
96 96 * text/plain
97 97 * text/html
@@ -108,14 +108,15 class DisplayFormatter(Configurable):
108 108 ----------
109 109 obj : object
110 110 The Python object whose format data will be computed.
111 include : list or tuple, optional
111 include : list, tuple or set; optional
112 112 A list of format type strings (MIME types) to include in the
113 113 format data dict. If this is set *only* the format types included
114 114 in this list will be computed.
115 exclude : list or tuple, optional
115 exclude : list, tuple or set; optional
116 116 A list of format type string (MIME types) to exclude in the format
117 117 data dict. If this is set all format types will be computed,
118 118 except for those included in this argument.
119 Mimetypes present in exclude will take precedence over the ones in include
119 120
120 121 Returns
121 122 -------
@@ -129,6 +130,15 class DisplayFormatter(Configurable):
129 130
130 131 metadata_dict is a dictionary of metadata about each mime-type output.
131 132 Its keys will be a strict subset of the keys in format_dict.
133
134 Notes
135 -----
136
137 If an object implement `_repr_mimebundle_` as well as various
138 `_repr_*_`, the data returned by `_repr_mimebundle_` will take
139 precedence and the corresponding `_repr_*_` for this mimetype will
140 not be called.
141
132 142 """
133 143 format_dict = {}
134 144 md_dict = {}
@@ -139,6 +149,14 class DisplayFormatter(Configurable):
139 149
140 150 format_dict, md_dict = self.mimebundle_formatter(obj)
141 151
152 if format_dict or md_dict:
153 if include:
154 format_dict = {k:v for k,v in format_dict.items() if k in include}
155 md_dict = {k:v for k,v in md_dict.items() if k in include}
156 if exclude:
157 format_dict = {k:v for k,v in format_dict.items() if k not in exclude}
158 md_dict = {k:v for k,v in md_dict.items() if k not in exclude}
159
142 160 for format_type, formatter in self.formatters.items():
143 161 if format_type in format_dict:
144 162 # already got it from mimebundle, don't render again
@@ -163,7 +181,6 class DisplayFormatter(Configurable):
163 181 format_dict[format_type] = data
164 182 if md is not None:
165 183 md_dict[format_type] = md
166
167 184 return format_dict, md_dict
168 185
169 186 @property
@@ -983,4 +1000,3 def format_display_data(obj, include=None, exclude=None):
983 1000 include,
984 1001 exclude
985 1002 )
986
@@ -444,9 +444,13 def test_repr_mime():
444 444 return {
445 445 'application/json+test.v2': {
446 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 454 def _repr_html_(self):
451 455 return '<b>hi!</b>'
452 456
@@ -458,9 +462,18 def test_repr_mime():
458 462 d, md = f.format(obj)
459 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 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 478 def test_repr_mime_meta():
466 479 class HasReprMimeMeta(object):
General Comments 0
You need to be logged in to leave comments. Login now