CoolFace
Apppublic

MLBench/ReaLens

sourceHugging Facemitupdated 8mo agoView on Hugging Face
0likes
html.py85 linesDownload Raw Back to util
1import dominate2from dominate.tags import meta, h3, table, tr, td, p, a, img, br3from pathlib import Path4 5 6class HTML:7    """This HTML class allows us to save images and write texts into a single HTML file.8 9    It consists of functions such as <add_header> (add a text header to the HTML file),10    <add_images> (add a row of images to the HTML file), and <save> (save the HTML to the disk).11    It is based on Python library 'dominate', a Python library for creating and manipulating HTML documents using a DOM API.12    """13 14    def __init__(self, web_dir, title, refresh=0):15        """Initialize the HTML classes16 17        Parameters:18            web_dir (str) -- a directory that stores the webpage. HTML file will be created at <web_dir>/index.html; images will be saved at <web_dir/images/19            title (str)   -- the webpage name20            refresh (int) -- how often the website refresh itself; if 0; no refreshing21        """22        self.title = title23        self.web_dir = Path(web_dir)24        self.img_dir = self.web_dir / "images"25 26        self.web_dir.mkdir(parents=True, exist_ok=True)27        self.img_dir.mkdir(parents=True, exist_ok=True)28 29        self.doc = dominate.document(title=title)30        if refresh > 0:31            with self.doc.head:32                meta(http_equiv="refresh", content=str(refresh))33 34    def get_image_dir(self):35        """Return the directory that stores images"""36        return self.img_dir37 38    def add_header(self, text):39        """Insert a header to the HTML file40 41        Parameters:42            text (str) -- the header text43        """44        with self.doc:45            h3(text)46 47    def add_images(self, ims, txts, links, width=400):48        """add images to the HTML file49 50        Parameters:51            ims (str list)   -- a list of image paths52            txts (str list)  -- a list of image names shown on the website53            links (str list) --  a list of hyperref links; when you click an image, it will redirect you to a new page54        """55        self.t = table(border=1, style="table-layout: fixed;")  # Insert a table56        self.doc.add(self.t)57        with self.t:58            with tr():59                for im, txt, link in zip(ims, txts, links):60                    with td(style="word-wrap: break-word;", halign="center", valign="top"):61                        with p():62                            with a(href=Path("images") / link):63                                img(style=f"width:{width}px", src=Path("images") / im)64                            br()65                            p(txt)66 67    def save(self):68        """save the current content to the HMTL file"""69        html_file = self.web_dir / "index.html"70        with open(html_file, "wt") as f:71            f.write(self.doc.render())72 73 74if __name__ == "__main__":  # we show an example usage here.75    html = HTML("web/", "test_html")76    html.add_header("hello world")77 78    ims, txts, links = [], [], []79    for n in range(4):80        ims.append(f"image_{n}.png")81        txts.append(f"text_{n}")82        links.append(f"image_{n}.png")83    html.add_images(ims, txts, links)84    html.save()85