1
0
mirror of https://github.com/pyscript/pyscript.git synced 2022-05-01 19:47:48 +03:00

Various mime rendering fixes

This commit is contained in:
Philipp Rudiger
2022-04-21 00:21:43 +02:00
parent b5bfffe912
commit 0b6fe08663
2 changed files with 20 additions and 8 deletions

View File

@@ -29,7 +29,7 @@ MIME_METHODS = {
}
def render_image(mime, value, meta):
data = f'{mime};base64,{value}'
data = f'data:{mime};charset=utf-8;base64,{value}'
attrs = ' '.join(['{k}="{v}"' for k, v in meta.items()])
return f'<img src="{data}" {attrs}</img>'
@@ -68,7 +68,13 @@ def format_mime(obj):
"""
Formats object using _repr_x_ methods.
"""
format_dict, md_dict = eval_formatter(obj, '_repr_mimebundle_')
mimebundle = eval_formatter(obj, '_repr_mimebundle_')
if isinstance(mimebundle, tuple):
format_dict, md_dict = mimebundle
else:
format_dict = mimebundle
md_dict = {}
output, not_available = None, []
for method, mime_type in reversed(MIME_METHODS.items()):
@@ -113,9 +119,9 @@ class PyScript:
element = document.getElementById(element_id)
html, mime_type = format_mime(value)
if mime_type == 'application/javascript':
if mime_type in ('application/javascript', 'text/html'):
scriptEl = document.createRange().createContextualFragment(html)
element.children = [scriptEl]
element.appendChild(scriptEl)
else:
element.innerHTML = html

View File

@@ -20,7 +20,7 @@ MIME_METHODS = {
}
def render_image(mime, value, meta):
data = f'{mime};base64,{value}'
data = f'data:{mime};charset=utf-8;base64,{value}'
attrs = ' '.join(['{k}="{v}"' for k, v in meta.items()])
return f'<img src="{data}" {attrs}</img>'
@@ -59,7 +59,13 @@ def format_mime(obj):
"""
Formats object using _repr_x_ methods.
"""
format_dict, md_dict = eval_formatter(obj, '_repr_mimebundle_')
mimebundle = eval_formatter(obj, '_repr_mimebundle_')
if isinstance(mimebundle, tuple):
format_dict, md_dict = mimebundle
else:
format_dict = mimebundle
md_dict = {}
output, not_available = None, []
for method, mime_type in reversed(MIME_METHODS.items()):
@@ -104,9 +110,9 @@ class PyScript:
element = document.getElementById(element_id)
html, mime_type = format_mime(value)
if mime_type == 'application/javascript':
if mime_type in ('application/javascript', 'text/html'):
scriptEl = document.createRange().createContextualFragment(html)
element.children = [scriptEl]
element.appendChild(scriptEl)
else:
element.innerHTML = html