-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhtml.py
89 lines (68 loc) · 1.83 KB
/
html.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
csvcount=0
def tag(name, s):
return "<%s>%s</%s>" % (name, s, name)
def style():
return tag("style", """
* { padding: 10px; margin: 10px; }
thead { background: #eee }
th { font-weight: bold; border: 1px solid black }
td { border: 1px solid black }
hr { padding: 0; margin: 0; border: 2px solid blue }
""")
def script():
return "<script type='text/javascript' src='dygraph-combined.js'></script>"
def b(s):
return tag("b", s)
def td(s):
return tag("td", s)
def tr(s):
return tag("tr", s)
def join(l):
return "\n".join(l)
def hr():
return "<hr/>"
def section(name, s):
return join([tag("h2", name), s, hr()])
def comments(l):
return tag("pre",'\n'.join(l))
def csv(csv,title,opts=[]):
global csvcount
csvcount += 1
opts.append("rightGap:250")
csv = map(lambda x: r"'%s\n'" % x, csv)
csvtxt = r"""
<div style='float:left;border:1px solid black;'>
%s
<div id="graphdiv%d" style="width:1200px;height:500px"></div>
<script type="text/javascript">
g = new Dygraph(
// containing div
document.getElementById("graphdiv%d"),
%s
, {%s}
);
</script>
</div>
""" % (tag("h3",title), csvcount, csvcount, "+".join(csv), ",".join(opts))
return csvtxt
def table(d):
"""
'd' is a dictionary of {rowname : { colname : value } }
Returns a string of the html form of the table
"""
if type(d) == type({}):
s = ""
cols = d[d.keys()[0]]
# first row, headings for cols
s += join([td("")] + [td(b(colname)) for colname in cols])
# every row
for rowname in d.keys():
s += tr(join( [ td(b(rowname)) ] + [td(d[rowname][colname]) for colname in cols]))
else:
s = ""
s += tr(join([td(x) for x in d]))
return tag("table", s)
def html(fname, doc):
f = open(fname, "w")
f.write(tag("html", style() + script() + doc))
f.close()