MCPcopy Index your code
hub / github.com/pyscript/pyscript / _render_image

Function _render_image

core/src/stdlib/pyscript/display.py:44–56  ·  view source on GitHub ↗

Render image (`mime`) data (`value`) as an HTML img element with data URL. Any `meta` attributes are added to the img tag. Accepts both raw bytes and base64-encoded strings for flexibility. This only handles PNG and JPEG images. SVG images are handled separately as their raw XM

(mime, value, meta)

Source from the content-addressed store, hash-verified

42
43
44def _render_image(mime, value, meta):
45 """
46 Render image (`mime`) data (`value`) as an HTML img element with data URL.
47 Any `meta` attributes are added to the img tag.
48
49 Accepts both raw bytes and base64-encoded strings for flexibility. This
50 only handles PNG and JPEG images. SVG images are handled separately as
51 their raw XML content (which the browser can render directly).
52 """
53 if isinstance(value, bytes):
54 value = base64.b64encode(value).decode("utf-8")
55 attrs = "".join([f' {k}="{v}"' for k, v in meta.items()])
56 return f'<img src="data:{mime};base64,{value}"{attrs}>'
57
58
59# Maps MIME types to rendering functions.

Callers 1

display.pyFile · 0.85

Calls 1

decodeMethod · 0.80

Tested by

no test coverage detected