Skip to content

Commit 0b6fe08

Browse files
committed
Various mime rendering fixes
1 parent b5bfffe commit 0b6fe08

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

pyscriptjs/src/interpreter.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ MIME_METHODS = {
2929
}
3030
3131
def render_image(mime, value, meta):
32-
data = f'{mime};base64,{value}'
32+
data = f'data:{mime};charset=utf-8;base64,{value}'
3333
attrs = ' '.join(['{k}="{v}"' for k, v in meta.items()])
3434
return f'<img src="{data}" {attrs}</img>'
3535
@@ -68,7 +68,13 @@ def format_mime(obj):
6868
"""
6969
Formats object using _repr_x_ methods.
7070
"""
71-
format_dict, md_dict = eval_formatter(obj, '_repr_mimebundle_')
71+
72+
mimebundle = eval_formatter(obj, '_repr_mimebundle_')
73+
if isinstance(mimebundle, tuple):
74+
format_dict, md_dict = mimebundle
75+
else:
76+
format_dict = mimebundle
77+
md_dict = {}
7278
7379
output, not_available = None, []
7480
for method, mime_type in reversed(MIME_METHODS.items()):
@@ -113,9 +119,9 @@ class PyScript:
113119
114120
element = document.getElementById(element_id)
115121
html, mime_type = format_mime(value)
116-
if mime_type == 'application/javascript':
122+
if mime_type in ('application/javascript', 'text/html'):
117123
scriptEl = document.createRange().createContextualFragment(html)
118-
element.children = [scriptEl]
124+
element.appendChild(scriptEl)
119125
else:
120126
element.innerHTML = html
121127

pyscriptjs/src/pyscript.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
}
2121

2222
def render_image(mime, value, meta):
23-
data = f'{mime};base64,{value}'
23+
data = f'data:{mime};charset=utf-8;base64,{value}'
2424
attrs = ' '.join(['{k}="{v}"' for k, v in meta.items()])
2525
return f'<img src="{data}" {attrs}</img>'
2626

@@ -59,7 +59,13 @@ def format_mime(obj):
5959
"""
6060
Formats object using _repr_x_ methods.
6161
"""
62-
format_dict, md_dict = eval_formatter(obj, '_repr_mimebundle_')
62+
63+
mimebundle = eval_formatter(obj, '_repr_mimebundle_')
64+
if isinstance(mimebundle, tuple):
65+
format_dict, md_dict = mimebundle
66+
else:
67+
format_dict = mimebundle
68+
md_dict = {}
6369

6470
output, not_available = None, []
6571
for method, mime_type in reversed(MIME_METHODS.items()):
@@ -104,9 +110,9 @@ def write(element_id, value, append=False, exec_id=0):
104110

105111
element = document.getElementById(element_id)
106112
html, mime_type = format_mime(value)
107-
if mime_type == 'application/javascript':
113+
if mime_type in ('application/javascript', 'text/html'):
108114
scriptEl = document.createRange().createContextualFragment(html)
109-
element.children = [scriptEl]
115+
element.appendChild(scriptEl)
110116
else:
111117
element.innerHTML = html
112118

0 commit comments

Comments
 (0)