Team Ai
Apppublic

ReflectionEraser/ReflectionEraserApp

sourceHugging Faceotherupdated 2y agoView on Hugging Face
0likes
html.py67 linesDownload Raw Back to util
1import dominate #used for creating and manipulating HTML documents in a programmatic way.
2from dominate.tags import * #easier to create HTML tags directly.
3import os
4
5#ALL reflesh should be refresh
6class HTML:
7    def __init__(self, web_dir, title, reflesh=0): #reflesh: refresh_rate
8        self.title = title #title of web page
9        self.web_dir = web_dir#parent directory of all web content e.g images
10        self.img_dir = os.path.join(self.web_dir, 'images')
11        if not os.path.exists(self.web_dir):
12            os.makedirs(self.web_dir)
13        if not os.path.exists(self.img_dir):
14            os.makedirs(self.img_dir)
15        #(mingcv) print(self.img_dir)
16
17        self.doc = dominate.document(title=title)
18        if reflesh > 0:
19            with self.doc.head:
20                meta(http_equiv="refresh", content=str(reflesh)) #was "reflesh" in mingcv
21        #The http-equiv attribute provides an HTTP header for the information/value of the content attribute.In <head></head>
22    def get_image_dir(self):
23        return self.img_dir
24
25    def add_header(self, str):
26        with self.doc:
27            h3(str)
28
29    def add_table(self, border=1):
30        self.t = table(border=border, style="table-layout: fixed;")
31        self.doc.add(self.t)
32
33    def add_images(self, ims, txts, links, height=400):# add images to the HTML document within a table. Each cell (td) in the table contains an image on one line and its descriptive text on the next line.
34        self.add_table()
35        with self.t:#opens the table context for adding rows
36            with tr():#open table row context
37                for im, txt, link in zip(ims, txts, links):#This iterates over the images, texts, and links
38                    with td(style="word-wrap: break-word;", halign="center", valign="top"):
39                        with p():
40                            with a(href=os.path.join('images', link)):
41                                img(style="height:%dpx" % height, src=os.path.join('images', im))
42                            br()
43                            p(txt)
44
45    def save(self):# save the generated HTML document to a file.
46        html_file = '%s/index.html' % self.web_dir  # self.web_dir value will be placed in %s like sprintf(html_file, “%s/index.html”, web_dir);
47        f = open(html_file, 'wt')#write text mode 
48        f.write(self.doc.render())# writes the rendered HTML content to the file. The render method of the doc object (which is an instance of the dominate.document class) converts the HTML document into a string.
49        f.close()
50
51
52if __name__ == '__main__':#if the script is being run as the main module. This block will only execute if the script is run directly (not imported as a module).
53
54    html = HTML('web/', 'test_html')#'web/'=web_dir, 'test_html'=title
55    html.add_header('hello world')
56
57    ims = []#ims for image filenames, txts for image descriptions, and links for image links.
58    txts = []
59    links = []
60    for n in range(4):#adds images, texts and links to the list so that they can be added to html. 
61        '''??? why 4'''
62        ims.append('image_%d.png' % n)
63        txts.append('text_%d' % n)
64        links.append('image_%d.png' % n)
65    html.add_images(ims, txts, links)
66    html.save()
67