MCPcopy Create free account
hub / github.com/Relento/lego_release / HTML

Class HTML

util/html.py:7–74  ·  view source on GitHub ↗

This HTML class allows us to save images and write texts into a single HTML file. It consists of functions such as (add a text header to the HTML file), (add a row of images to the HTML file), and (save the HTML to the disk). It is based on Python lib

Source from the content-addressed store, hash-verified

5
6
7class HTML:
8 """This HTML class allows us to save images and write texts into a single HTML file.
9
10 It consists of functions such as <add_header> (add a text header to the HTML file),
11 <add_images> (add a row of images to the HTML file), and <save> (save the HTML to the disk).
12 It is based on Python library 'dominate', a Python library for creating and manipulating HTML documents using a DOM API.
13 """
14
15 def __init__(self, web_dir, title, refresh=0):
16 """Initialize the HTML classes
17
18 Parameters:
19 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/
20 title (str) -- the webpage name
21 refresh (int) -- how often the website refresh itself; if 0; no refreshing
22 """
23 self.title = title
24 self.web_dir = web_dir
25 self.img_dir = os.path.join(self.web_dir, 'images')
26 if not os.path.exists(self.web_dir):
27 os.makedirs(self.web_dir)
28 if not os.path.exists(self.img_dir):
29 os.makedirs(self.img_dir)
30
31 self.doc = dominate.document(title=title)
32 if refresh > 0:
33 with self.doc.head:
34 meta(http_equiv="refresh", content=str(refresh))
35
36 def get_image_dir(self):
37 """Return the directory that stores images"""
38 return self.img_dir
39
40 def add_header(self, text):
41 """Insert a header to the HTML file
42
43 Parameters:
44 text (str) -- the header text
45 """
46 with self.doc:
47 h3(text)
48
49 def add_images(self, ims, txts, links, width=400):
50 """add images to the HTML file
51
52 Parameters:
53 ims (str list) -- a list of image paths
54 txts (str list) -- a list of image names shown on the website
55 links (str list) -- a list of hyperref links; when you click an image, it will redirect you to a new page
56 """
57 self.t = table(border=1, style="table-layout: fixed;") # Insert a table
58 self.doc.add(self.t)
59 with self.t:
60 with tr():
61 for im, txt, link in zip(ims, txts, links):
62 with td(style="word-wrap: break-word;", halign="center", valign="top"):
63 with p():
64 with a(href=os.path.join('images', link)):

Callers 1

html.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected