ref: https://github.com/prateekvjoshi/Python-HTML-Generator
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from string import Template
TAB = " "
class T(object):
"""
A template object has a name, attributes and content.
The contents may contain sub template objects.
Attributes are kept in order.
The only things one has to remember:
* Attributes clashing with python keywords must be suffixed with a '_'.
* Attribute names should not start with an underscore '_'
* use the '<' operator to add content to a template object.
ref: https://github.com/prateekvjoshi/Python-HTML-Generator
"""
def __init__(self, name = None):
self.__name = name
self.__multi_line = False
self.__contents = []
self.__attributes = []
def __open(self, level = -1, **namespace):
out = ["{0}<{1}".format(TAB * level, self.__name)]
for (name, value) in self.__attributes:
out.append(' {0}="{1}"'.format(name, value))
out.append(">")
if self.__multi_line:
out.append("\n")
templ = ''.join(out)
txt = Template(templ).substitute(namespace)
return txt
def __close(self, level = -1, **namespace):
if self.__multi_line:
txt = "\n{0}</{1}>\n".format(TAB * level, self.__name)
else:
txt = "</{0}>\n".format(self.__name)
return txt
# public API
def _render(self, level = -1, **namespace):
out = []
out_contents = []
contents = self.__contents
for item in contents:
if item is None:
continue
if type(item) is T:
self.__multi_line = True
out_contents.append(item._render(level = level + 1, **namespace))
else:
out_contents.append("{0}{1}".format(TAB * level, Template(item).substitute(namespace)))
txt_contents = ''.join(out_contents)
if not self.__multi_line:
txt_contents = txt_contents.strip()
else:
txt_contents = txt_contents.rstrip()
if self.__name:
out.append(self.__open(level, **namespace))
out.append(txt_contents)
out.append(self.__close(level, **namespace))
else:
out.append(txt_contents)
return ''.join(out)
def __getattr__(self, name):
t = self.__class__(name)
self < t
return t
def __setattr__(self, name, value):
if name.startswith('_'):
self.__dict__[name] = value
else:
## everything else is an element attribute
## strip trailing underscores
self.__attributes.append((name.rstrip('_'), value))
def _set(self, name, value):
""" settings of attributes when attribure name is not a valid python
identifier.
"""
self.__attributes.append((name.rstrip('_'), value))
def __lt__(self, other):
self.__contents.append(other)
return self
def __call__(self, _class = None, _id = None):
if _class:
self.__attributes.append(('class', _class))
if _id:
self.id = _id
return self
## with interface
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, exc_traceback):
return False
def example():
doc = T()
with doc.html as html:
with html.head as head:
head.title = 'Redis Test Result Report'
with html.body as body:
body.h1("main") < "Redis Test Result Report"
body.p("main") < "Result repo: https://github.intel.com/changqi1/redis-4.0.0-volatile-benchmark-reports"
body.h2("Condition") < "Condition"
body.p("Condition") < "background: purpose of the test"
body.p("Condition") < "benchmark version: test script version"
body.p("Condition") < "redis target: the redis version being tested"
body.h2("Summary") < "Summary"
body.p("Summary") < "A paragraph of text summarizes the conclusion. "
body.p("Summary") < "The results are presented in the form of a graph."
import base64
with open("1.png", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
with body.img as img:
img.src_ = "data:image/png;base64, " + encoded_string
body.h2("Details") < "Details"
body.p("Details") < "Test process: Simplified execution of commands"
with body.ul as ul:
for i in range(10):
ul.li < "taskset -c 0,56 /root/cq/redis-4.0.0-volatile/src/redis-server --port 9000 --dbfilename 0.rdb --save 2 2 --dir /mnt/nvme0n1/redis &"
body.p("Details") < "Details data: a table of each instance data"
body.pre("table") < """+----------------------+----------+
| animal | ferocity |
+----------------------+----------+
| Rabbit of Caerbannog | 110 |
| wolverine | 100 |
| grizzly | 87 |
| dolphin | 63 |
| albatross | 44 |
| platypus | 23 |
| cat | -1 |
+----------------------+----------+"""
body.p("Details") < "Note: Explain the unknown"
body.h2('Reference') < "Reference"
with body.ul as ul:
ul.li < "CPU model: Intel(R) Xeon(R) Platinum 8180M CPU @ 2.50GHz * 112 "
ul.li < "Memory: "
ul.li < "SSD: "
ul.li < "Kernel version: Linux version 4.17.14-102.fc27.x86_64 "
ul.li < "OS Version: Red Hat 7.3.1-6"
ul.li < "benchmark script url: https://github.intel.com/changqi1/redis-4.0.0-volatile-benchmark"
ul.li < "redis url: https://github.intel.com/DEG-CCE-SE/redis-4.0.0-volatile"
body.p("Details") < "Best Regards"
body.p("Details") < "IT Flex CAS Team"
return doc
if __name__ == "__main__":
doc = example()
html = doc._render(name = 'redis')
with open('index.html', 'w') as f:
f.write(html)