From e1e262f9d7fad229ca4dd3d3b3c3f38d003f62f6 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 20 Dec 2023 15:53:25 +0700 Subject: [PATCH] add: support for embedding images --- htmlgenerator/htmltags.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/htmlgenerator/htmltags.py b/htmlgenerator/htmltags.py index cc8bcf5..2276da1 100644 --- a/htmlgenerator/htmltags.py +++ b/htmlgenerator/htmltags.py @@ -1,3 +1,4 @@ +import base64 import typing import warnings @@ -381,6 +382,17 @@ class IMAGE(HTMLElement): class IMG(VoidElement): tag = "img" + def __init__(self, *args, embed_as=None, **kwargs): + if embed_as is not None: + if not isinstance(kwargs.get("src", None), bytes): + raise Exception( + "An embeded image must have a bytes value set for the 'src' attribute" + ) + kwargs[ + "src" + ] = f"data:image/{embed_as};base64,{base64.b64encode(kwargs['src']).decode()}" + super().__init__(*args, **kwargs) + class INPUT(VoidElement): tag = "input"