-
Notifications
You must be signed in to change notification settings - Fork 0
/
html_report.py
234 lines (196 loc) · 8.03 KB
/
html_report.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/local/bin/python3
"""Support for library reports in HTML format."""
import math
# This specifies a white (low numbers) to greeen (high numbers) spectrum
RED_RANGE = [0x58, 0xff]
GREEN_RANGE = [0xc0, 0xff]
BLUE_RANGE = [0x58, 0xff]
RED_SPREAD = math.fabs(RED_RANGE[1] - RED_RANGE[0])
GREEN_SPREAD = math.fabs(GREEN_RANGE[1] - GREEN_RANGE[0])
BLUE_SPREAD = math.fabs(BLUE_RANGE[1] - BLUE_RANGE[0])
RED_MAX = max(RED_RANGE)
GREEN_MAX = max(GREEN_RANGE)
BLUE_MAX = max(BLUE_RANGE)
def gen_header():
"""Create an HTML header."""
return (
'<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html '
+ 'PUBLIC "-//W3C//DTD XHTML 1.1//EN" '
+ '"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'
+ '<html xmlns="http://www.w3.org/1999/xhtml"> '
+ '<head><meta '
+ 'http-equiv="Content-Type" content="text/html; charset=utf-8"/> '
+ '</head> <body>')
def gen_footer():
"""Create an HTML footr."""
return '</body></html>'
def gen_count_style(count, max_count):
"""Bigger counts get more color.
Color scheme is set above to go exponentially from white at low numbers
to maximum color at high numbers.
"""
if count == 0:
percent = 0
else:
percent = math.log(count+1, max_count+1)
red = int(round(RED_MAX - RED_SPREAD*percent))
green = int(round(GREEN_MAX - GREEN_SPREAD*percent))
blue = int(round(BLUE_MAX - BLUE_SPREAD*percent))
color = "{0:2x}{1:2x}{2:2x}".format(red, green, blue)
style = ' style="text-align: right; background-color: #' + color + ';" '
return style
def gen_year_report(
lib, years_ordered, actually_markdown=False):
"""Generate a year report, for the given years, in HTML format.
"""
report = []
if not actually_markdown:
# Generate header.
report.append(gen_header())
report.append('<table class="table">\n')
report.append(' <tr>\n')
report.append(' <th> Year </th>\n')
report.append(' <th> # </th>\n')
report.append(' </tr>\n')
# Years
n_papers_across_years = 0
n_papers_this_year = {}
for year in years_ordered:
n_papers_this_year[year] = len(lib.get_pubs(year=year))
n_papers_across_years += n_papers_this_year[year]
for year in years_ordered:
style = gen_count_style(
n_papers_this_year[year], n_papers_across_years)
report.append(' <tr>\n')
report.append(' <td> {0} </td>\n'.format(year))
report.append(' <td {0}> {1} </td>\n'.format(
style, n_papers_this_year[year]))
report.append(' </tr>\n')
# Total
style = gen_count_style(n_papers_across_years, n_papers_across_years)
report.append(' <tr>\n')
report.append(' <td class="text-right"> Total </td>\n')
report.append(
' <td {0}> {1} </td>\n'.format(
style, n_papers_across_years))
report.append(' </tr>\n')
if not actually_markdown:
report.append(gen_footer())
return report
def gen_journal_report(lib, actually_markdown=False):
"""Generate a report listing all journals, ranked by number of pubs,
in HTML format.
"""
report = []
if not actually_markdown:
# Generate header.
report.append(gen_header())
report.append('<table class="table">\n')
report.append(' <tr>\n')
report.append(' <th> Rank </th>\n')
report.append(' <th> Journal </th>\n')
report.append(' <th> # </th>\n')
report.append(' </tr>\n')
rank = 0
jrnl_idx = 0
n_prior_pubs = 0
for jrnl_pubs in lib.journal_pubs_rank:
jrnl_idx += 1
n_current_pubs = len(jrnl_pubs)
if n_prior_pubs != n_current_pubs:
rank = jrnl_idx
n_prior_pubs = n_current_pubs
report.append(' <tr>\n')
report.append(' <td> {0} </td>\n'.format(rank))
report.append(' <td> {0} </td>\n'.format(jrnl_pubs[0].journal_name))
report.append(' <td style="text-align: right;"> {0} </td>\n'.format(
n_current_pubs))
return report
def gen_tag_year_report(
lib, tags_ordered, n_papers_w_tag, years_ordered,
actually_markdown=False):
"""Generate a tagyear report in HTML format.
TODO: Combine tags_ordered and n_papers_w_tag into an ordered dictionary
"""
report = []
if not actually_markdown:
# Generate header.
report.append(gen_header())
report.append('<table class="table">\n')
report.append(' <tr style="height: 8rem;">\n')
report.append(' <th style="vertical-align: bottom;"> Year </th>\n')
report.append(' <th style="vertical-align: bottom;"> # Pubs </th>\n')
report.append(' <th> </th>\n') # blank column before starting tags.
for tag in tags_ordered:
report.append(
' <th style="max-width: 3rem; vertical-align: middle; transform: translate(0rem, 2.2rem) rotate(290deg);"> <a href="'
+ lib.gen_tag_url(tag)
+ '">' + tag + '</a> </th>\n')
report.append(' </tr>\n')
# generate numbers of papers with tag per year
all_papers_count = len(lib)
for year in years_ordered:
report.append(' <tr>\n')
n_papers_this_year = len(lib.get_pubs(year=year))
report.append(' <th> ' + year + ' </th>\n')
year_count_style = gen_count_style(
n_papers_this_year, all_papers_count)
year_url = lib.gen_year_url(year)
if year_url:
report.append(
' <td {0}> <a href="{1}"> {2} </a></td>\n'.format(
year_count_style, year_url, n_papers_this_year))
else:
report.append(
' <td {0}> {1} </td>\n'.format(
year_count_style, n_papers_this_year))
report.append(' <td> </td>\n') # blank column before starting tags.
for tag in tags_ordered:
papers_for_tag_year = lib.get_pubs(tag=tag, year=year)
if papers_for_tag_year:
n_papers_tag_year = len(papers_for_tag_year)
style = gen_count_style(
n_papers_tag_year, all_papers_count)
tag_year_url = lib.gen_tag_year_url(tag, year)
if tag_year_url:
count_html = (
'<a href="{0}"> {1} </a>'.format(
tag_year_url, n_papers_tag_year))
else:
count_html = "{0}".format(n_papers_tag_year)
else:
style = ""
count_html = ""
report.append(' <td ' + style + '> ' + count_html + ' </td>\n')
report.append(' </tr>\n')
# generate total line at bottom
report.append(' <tr>\n')
report.append(' <th> Total </th>\n')
all_papers_style = gen_count_style(all_papers_count, all_papers_count)
report.append(' <th ' + all_papers_style + '> '
+ str(all_papers_count) + ' </th>\n')
report.append(' <th> </th>\n') # blank column before starting tags.
for tag in tags_ordered:
tag_count_style = gen_count_style(
n_papers_w_tag[tag], all_papers_count)
report.append(
' <th ' + tag_count_style + '> '
+ '<a href="' + lib.gen_tag_url(tag) + '">'
+ str(n_papers_w_tag[tag]) + '</a> </th>\n')
report.append(' </tr>\n')
report.append('</table>\n')
if not actually_markdown:
report.append(gen_footer())
return report
def gen_tag_count_date_range_report(tags_in_count_order, n_total_papers,
lib, num_tag_column_groups,
start_date, end_date):
"""Generate a table with with each entry showing the tag name,
and the number of papers tagged with that tag during the given date range.
Each cell shows the number of papers a tag was attached to that generate.
However, HTML format is not yet supported for this report, so just throw
an error.
"""
raise NotImplementedError(
"gen_tag_count_date_range_report not yet implemented for report "
+ "format HTML")