← back to Handbag Authentication

handbag_data/github_datasets/iGAN/lib/html.py

58 lines

import dominate
from dominate.tags import *
import os


class HTML:
    def __init__(self, web_fold, t):
        self.title = t
        self.web_fold = web_fold
        self.img_fold = os.path.join(self.web_fold, 'images')
        if not os.path.exists(self.web_fold):
            os.makedirs(self.web_fold)
        if not os.path.exists(self.img_fold):
            os.makedirs(self.img_fold)
        print(self.img_fold)
        self.doc = dominate.document(title=t)

    def add_header(self, str):
        with self.doc:
            h3(str)

    def add_table(self, border=1):
        self.t = table(border=border, style="table-layout: fixed;")
        self.doc.add(self.t)

    def add_images(self, ims, txts, links, width=400):
        self.add_table()
        with self.t:
            with tr():
                for im, txt, link in zip(ims, txts, links):
                    with td(style="word-wrap: break-word;", halign="center", valign="top"):
                        with p():
                            with a(href=os.path.join('images', link)):
                                img(style="width:%dpx" % width, src=os.path.join('images', im))
                            br()
                            p(txt)

    def save(self):
        html_file = '%s/index.html' % self.web_fold
        f = open(html_file, 'wt')
        f.write(self.doc.render())
        f.close()


if __name__ == '__main__':
    html = HTML('web/', 'test_html')
    html.add_header('hello world')

    ims = []
    txts = []
    links = []
    for n in range(4):
        ims.append('image_%d.png' % n)
        txts.append('text_%d' % n)
        links.append('image_%d.png' % n)
    html.add_images(ims, txts, links)
    html.save()