Fix rendering for simple types

This commit is contained in:
Philipp Rudiger
2022-05-02 18:39:28 +02:00
parent 3457d4313a
commit 4df9a7e06d
2 changed files with 23 additions and 19 deletions

View File

@@ -50,14 +50,16 @@ def eval_formatter(obj, print_method):
"""
Evaluates a formatter method.
"""
if hasattr(obj, print_method):
if print_method == 'savefig':
buf = io.BytesIO()
obj.savefig(buf, format='png')
buf.seek(0)
return base64.b64encode(buf.read()).decode('utf-8')
if print_method == '__repr__':
return repr(obj)
elif print_method == 'savefig':
buf = io.BytesIO()
obj.savefig(buf, format='png')
buf.seek(0)
return base64.b64encode(buf.read()).decode('utf-8')
elif hasattr(obj, print_method):
return getattr(obj, print_method)()
if print_method == '_repr_mimebundle_':
elif print_method == '_repr_mimebundle_':
return {}, {}
return None
@@ -66,7 +68,7 @@ def format_mime(obj):
"""
Formats object using _repr_x_ methods.
"""
if isinstance(obj, (str, type)):
if isinstance(obj, str):
return obj, 'text/plain'
mimebundle = eval_formatter(obj, '_repr_mimebundle_')

View File

@@ -43,14 +43,16 @@ def eval_formatter(obj, print_method):
"""
Evaluates a formatter method.
"""
if hasattr(obj, print_method):
if print_method == 'savefig':
buf = io.BytesIO()
obj.savefig(buf, format='png')
buf.seek(0)
return base64.b64encode(buf.read()).decode('utf-8')
if print_method == '__repr__':
return repr(obj)
elif print_method == 'savefig':
buf = io.BytesIO()
obj.savefig(buf, format='png')
buf.seek(0)
return base64.b64encode(buf.read()).decode('utf-8')
elif hasattr(obj, print_method):
return getattr(obj, print_method)()
if print_method == '_repr_mimebundle_':
elif print_method == '_repr_mimebundle_':
return {}, {}
return None
@@ -59,8 +61,8 @@ def format_mime(obj):
"""
Formats object using _repr_x_ methods.
"""
if isinstance(obj, (str, type)):
return str(obj), 'text/plain'
if isinstance(obj, str):
return obj, 'text/plain'
mimebundle = eval_formatter(obj, '_repr_mimebundle_')
if isinstance(mimebundle, tuple):
@@ -75,7 +77,7 @@ def format_mime(obj):
output = format_dict[mime_type]
else:
output = eval_formatter(obj, method)
if output is None:
continue
elif mime_type not in MIME_RENDERERS:
@@ -84,7 +86,7 @@ def format_mime(obj):
break
if output is None:
if not_available:
console.warning(f'Rendered object requested unavailable MIME renderers: {not_available}')
console.warning(f'Rendered object requested unavailable MIME renderers: {not_available}')
output = repr(output)
mime_type = 'text/plain'
elif isinstance(output, tuple):