forked from OBOFoundry/OBOFoundry.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-html-grid.py
executable file
·82 lines (66 loc) · 2.05 KB
/
create-html-grid.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
#!/usr/bin/env python3
import csv
import sys
from argparse import ArgumentParser
headers = []
def main(args):
parser = ArgumentParser(description="Generate an HTML output of the metadata grid")
parser.add_argument('input_grid', type=str, help='File containing the grid (CSV, TSV, or TXT)')
parser.add_argument('html_grid', type=str, help='HTML file to write the output to')
args = parser.parse_args()
input_grid = args.input_grid
html_grid = args.html_grid
lines = get_html(parse_table(input_grid))
with open(html_grid, 'w') as f:
for line in lines:
f.write(line + '\n')
def parse_table(input_grid):
'''Given an input grid in TSV or CSV, get the data as a dictionary. Also set
the headers for the HTML grid.'''
global headers
data = {}
if '.tsv' in input_grid:
delim = '\t'
elif '.csv' in input_grid:
delim = ','
else:
print("Invalid file extension: %s" % input_grid, file=sys.stderr)
sys.exit(1)
reader = csv.reader(open(input_grid), delimiter=delim)
headers = next(reader)
for row in reader:
ont_id = row.pop(0)
fields = row
data[ont_id] = fields
return data
def get_html(data):
'''Given the data from the grid, return an array of lines to generate an
HTML table.'''
global headers
lines = []
lines.append('<table>')
lines.append('\t<tr>')
for h in headers:
lines.append('\t\t<td><b>%s</b></td>' % h)
lines.append('\t</tr>')
for ont, fields in data.items():
lines.append('\t<tr>')
bg_color = '#FFFFFF'
lines.append('\t\t<td bgcolor="%s">%s</td>' % (bg_color, ont))
for f in fields:
if f == 'PASS':
bg_color = '#97E595'
elif f == 'info' or f == 'inactive':
bg_color = '#DDDDDD'
elif f == 'warning' or f == 'WARN':
bg_color = '#F7F585'
elif f == 'error' or f == 'FAIL':
bg_color = '#F4A8A8'
else:
bg_color = '#FFFFFF'
lines.append('\t\t<td bgcolor="%s">%s</td>' % (bg_color, f))
lines.append('\t</tr>')
lines.append('</table>')
return lines
if __name__ == '__main__':
main(sys.argv)