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)
| 42 | |
| 43 | |
| 44 | def _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. |