annotate templates/html_report.py @ 0:7db7ecc78ad6 draft

Uploaded
author damion
date Mon, 02 Mar 2015 20:46:00 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
1 import os.path
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
2 import time
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
3 import csv
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
4 import collections
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
5 import re
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
6
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
7 HTML_REPORT_HEADER_FILE = 'html_report_header.html'
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
8
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
9 class HTMLReport(object):
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
10
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
11 """ This receives and sets up the general meta-data fields available to the html rendering engine
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
12
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
13 """
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
14 def __init__(self, tagGroup, options, query_stats = []):
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
15
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
16 self.columns = tagGroup.columns
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
17 self.display_columns = [field for field in self.columns if field['group']=='column']
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
18 self.row_limit = options.row_limit
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
19 self.section_bins = {}
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
20 self.todo = collections.deque([]) # stack of things to do
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
21 self.query_stats = query_stats
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
22 self.empty_queries = [query['id'] for query in query_stats if query['filtered_rows'] == 0]
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
23 self.initialized = False
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
24 self.errorNotice = ''
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
25
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
26 # These items are available for display in html generation via dictionary string replacement: [string ... %(filters)s ...] % self.lookup
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
27 self.lookup = {
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
28 'depth': 0,
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
29 'filters': 'Filters: ' + options.filters_HTML if len(options.filters_HTML) else '',
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
30 'timestamp': time.strftime('%Y/%m/%d'),
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
31 'visible_limit': 20,
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
32 'column_count': str(len([field for field in self.columns if field['group']=='column'])),
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
33 'table_rows':0,
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
34 'select_row':0,
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
35 'row_limit': self.row_limit,
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
36 'label':'',
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
37 'value':'',
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
38 'link':'',
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
39 'cssClass':'',
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
40 'table_header': self._tableHeader(),
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
41 'section_bins':'',
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
42 'section_counter':1
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
43 }
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
44
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
45 self.initialized = True
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
46
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
47 #else:
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
48 # add error notice for non-initialized template:
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
49 # self.errorNotice = '<div style="width:400px;margin:auto"><h3>This HTML Report could not be initialized ...</h3></div>'
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
50
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
51
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
52 """
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
53 _processTagStack()
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
54 In the production of html, start tags and template bits are added to the outgoing html when designated section columns of data change value. the self.todo stack keeps track of all the tag closings that have to occur when a section or table section comes to an end (and new one begins or end of document occurs).
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
55
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
56 This dynamically executes any functions listed in stack that are greater than given tag depth.
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
57 Generally the functions return html closing content.
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
58
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
59 @param depth integer >= 0
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
60 @uses self.todo stack of [depth, function_name] items
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
61 """
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
62 def _processTagStack(self, depth = 0):
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
63 html = ''
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
64 while len(self.todo) and self.todo[0][0] >= depth:
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
65 html += getattr(self, self.todo.popleft()[1] )()
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
66 return html
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
67
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
68
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
69
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
70 ############################### HTML REPORT RENDERING ##############################
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
71 """ render() produces the html. Takes in tabular data + metainformation about that file, and iterates through rows. This approach depends on detecting changes in stated report section columns and table section columns, and triggers appropriate section start and end, and table / table section start and end tags.
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
72
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
73 @param in_file string Full file path
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
74 @param out_html_file string Full output html data file path to write to.
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
75 """
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
76 def render (self, in_file, out_html_file):
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
77
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
78 try:
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
79
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
80 fp_in = open(in_file, "rb")
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
81 fp_out = open(out_html_file, 'w')
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
82
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
83 fp_out.write( self._header(HTML_REPORT_HEADER_FILE) )
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
84
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
85 if self.initialized:
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
86
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
87 fp_out.write( self._bodyStart() )
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
88 self.todo.appendleft([0,'_bodyEnd'])
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
89
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
90
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
91 reader = csv.reader(fp_in, delimiter="\t")
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
92
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
93 for row in reader:
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
94
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
95 html = ''
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
96 self.rowdata = []
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
97 row_bins = []
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
98 section_reset = False
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
99
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
100 for (idx, field) in enumerate(self.columns):
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
101
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
102 value = field['value'] = row[idx]
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
103 depth = idx + 1
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
104
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
105 # If a bin is mentioned on this row, its put into self.selection_bins.
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
106 if field['type'] == 'bin' and value != '':
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
107 row_bins.append(value)
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
108 if not value in self.section_bins:
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
109 self.section_bins[value] = field['label']
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
110
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
111 grouping = field['group']
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
112 # Section or table grouping here:
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
113 if grouping == 'section' or grouping == 'table':
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
114
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
115 # Check to see if a new section or table section is triggered by change in field's value:
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
116 if section_reset or (not 'valueOld' in field) or value != field['valueOld']:
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
117
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
118 self.lookup['value'] = value
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
119 self.lookup['label'] = field['label']
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
120
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
121 html += self._processTagStack(depth)
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
122
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
123 if grouping == 'section':
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
124 section_reset = True
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
125 self.lookup['section_depth'] = depth
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
126 self.lookup['section_counter'] += 1
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
127 self.lookup['table_rows'] = 0
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
128 self.section_bins = {}
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
129
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
130 html += self._sectionStart()
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
131
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
132 html += self._sectionFormStart()
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
133 self.todo.appendleft([depth,'_sectionFormEnd'])
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
134
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
135 self.todo.appendleft([depth,'_sectionEnd'])
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
136
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
137
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
138 elif grouping == 'table':
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
139
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
140 lastToDo = self.todo[0]
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
141 if lastToDo[1] == '_sectionEnd':
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
142 html += self._tableStart()
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
143 self.todo.appendleft([lastToDo[0]+1,'_tableEnd'])
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
144
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
145 html += self._tbodyHeader() + self._tbodyStart()
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
146 self.todo.appendleft([lastToDo[0]+2,'_tbodyEnd'])
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
147
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
148 field['valueOld'] = value
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
149
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
150 else:
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
151
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
152 if grouping == 'column': self.rowdata.append(row[idx])
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
153
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
154 lastToDo = self.todo[0]
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
155 # No table level, instead going right from section to column field:
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
156 if lastToDo[1] == '_sectionEnd':
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
157 html += self._tableStart() + self._tbodyStart()
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
158 self.todo.appendleft([lastToDo[0]+1,'_tableEnd'])
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
159 self.todo.appendleft([lastToDo[0]+2,'_tbodyEnd'])
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
160
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
161 self.lookup['row_bins'] = ",".join(row_bins)
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
162 fp_out.write(html)
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
163 self.lookup['table_rows'] += 1
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
164 # Now output table row of data:
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
165 fp_out.write( self._tableRow() )
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
166
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
167
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
168 #Not initialized here, so just write created error notice.
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
169 else:
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
170
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
171 fp_out.write('<body><h3>' + self.errorNotice + '</h3></body></html>')
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
172
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
173 fp_out.write( self._processTagStack() )
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
174
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
175 except IOError as e:
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
176 print 'Operation failed: %s' % e.strerror
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
177
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
178 fp_in.close()
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
179 fp_out.close()
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
180
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
181
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
182 ############################### HTML REPORT PART TEMPLATES ##############################
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
183 def _header(self, filename):
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
184
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
185 with open(os.path.join(os.path.dirname(__file__), filename), "r") as fphtml:
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
186 data = fphtml.read()
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
187
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
188 return data
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
189
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
190
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
191 def _bodyStart(self):
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
192 # The form enables the creation of a dataset from selected entries. It passes selections (based on a certain column's value) to the "Select tabular rows" tool, which then creates a dataset from the selected rows.
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
193 html = """
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
194 """
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
195 if len(self.empty_queries):
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
196 qnames = ''
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
197 for name in self.empty_queries: qnames += '<li>' + name + '</li>\n'
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
198 html += """
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
199 <div class="headerMessage">The following queries yielded 0 results (check filters):
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
200 <ul>
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
201 %s
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
202 </ul>
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
203 </div>""" % qnames
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
204
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
205 return html % self.lookup
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
206
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
207 # Repeated for each grouped section table display
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
208 def _sectionStart(self):
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
209 self.lookup['select_row'] +=1
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
210 return """
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
211 <div class="section section_depth%(section_depth)s">
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
212 <div class="section_title">%(label)s: %(value)s</div>
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
213 """ % self.lookup
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
214
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
215
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
216
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
217 def _sectionFormStart (self):
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
218 # This sets up the selection form #../../../tool_runner/index
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
219 return ""
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
220
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
221 def _tableStart (self):
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
222
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
223 return """
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
224 <table class="report">
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
225 %(table_header)s
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
226 """ % self.lookup
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
227
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
228
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
229 def _tableHeader(self):
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
230
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
231 colTags = '' # Style numeric fields
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
232 thTags = ''
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
233
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
234 for field in self.columns:
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
235 if field['group'] == 'column':
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
236 colTags += ('<col />' if field['type'] == 'text' else '<col class="numeric" />')
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
237 thTags += '<th>' + field['label'] + '</th>'
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
238
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
239 return """
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
240 <colgroup>
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
241 %s
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
242 </colgroup>
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
243 <thead class="top">
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
244 <tr>%s</tr>
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
245 </thead>""" % (colTags, thTags)
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
246
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
247 def _tbodyHeader (self):
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
248 if self.lookup['value'] == '': self.lookup['value'] = '(no match)'
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
249 return """
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
250 <thead class="inside">
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
251 <tr>
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
252 <th colspan="%(column_count)s">%(label)s: %(value)s</th>
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
253 </tr>
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
254 </thead>""" % self.lookup
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
255
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
256 def _tbodyStart (self):
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
257 return """
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
258 <tbody>""" % self.lookup
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
259
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
260
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
261 def _tableRow(self):
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
262 self.lookup['select_row'] +=1
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
263
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
264 tdTags = ''
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
265 for (col, field) in enumerate(self.display_columns):
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
266 value = self.rowdata[col]
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
267 self.lookup['value'] = value
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
268 self.lookup['cssClass'] = ' class="numeric"' if field['type'] == 'numeric' else ''
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
269 accessionID = re.search(r'[a-z]+[0-9]+(.[0-9]+)*' ,value, re.I)
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
270 if (accessionID) :
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
271 self.lookup['link'] = '<a href="https://google.ca/#q=%s+gene" target="search">%s</a>' % (accessionID.group(), value)
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
272 else:
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
273 self.lookup['link'] = value
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
274 # First column optionally gets bin indicator as well as row checkbox selector
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
275 if (col == 0):
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
276 tdTags += '<td%(cssClass)s>%(link)s<span class="super">%(row_bins)s</span></td>' % self.lookup
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
277 else:
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
278 tdTags += '<td%(cssClass)s>%(value)s</td>' % self.lookup
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
279
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
280 return """\n\t\t\t<tr>%s</tr>""" % tdTags
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
281
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
282 def _tbodyEnd (self):
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
283 return """
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
284 </tbody>"""
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
285
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
286 def _tableEnd (self):
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
287 if len(self.section_bins):
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
288 bins = []
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
289 for key in sorted(self.section_bins):
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
290 bins.append( '<span class="super">(%s)</span>%s' % (key, self.section_bins[key]) )
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
291 self.lookup['section_bins'] = 'Bins: ' + ', '.join(bins)
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
292 else:
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
293 self.lookup['section_bins'] = ''
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
294
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
295 return """
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
296 <tfoot>
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
297 <tr>
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
298 <td colspan="%(column_count)s">
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
299 <div class="footerCenter">
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
300 %(filters)s.
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
301 </div>
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
302 <div class="footerLeft">
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
303 <span class="rowViewer0"></span> %(table_rows)s results.
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
304 <span class="rowViewer1 nonprintable"></span>
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
305 %(section_bins)s
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
306 </div>
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
307 <div class="footerRight">
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
308 Report produced on %(timestamp)s
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
309 </div>
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
310
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
311 </td>
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
312 </tr>
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
313 </tfoot>
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
314 </table>""" % self.lookup
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
315
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
316 def _sectionFormEnd (self):
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
317 return """
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
318
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
319 """
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
320
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
321 def _sectionEnd (self):
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
322 return """
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
323 </div>"""
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
324
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
325
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
326 def _bodyEnd (self):
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
327
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
328 return """\n\t</body>\n</html>"""
7db7ecc78ad6 Uploaded
damion
parents:
diff changeset
329