forked from grzeimann/DetectWebpage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateWebpage.py
More file actions
142 lines (125 loc) · 5.02 KB
/
CreateWebpage.py
File metadata and controls
142 lines (125 loc) · 5.02 KB
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
""" Create Webpage Class
1) Create a webpage based on
.. moduleauthor:: Greg Zeimann <gregz@astro.as.utexas.edu>
"""
class CreateWebpage(object):
@classmethod
def writeHeader(cls, f, title):
"""Write the header to webpage file ``f``
Parameters
----------
f : file-like object
where to write to; must have a ``write`` method
"""
s = []
s.append('<!DOCTYPE HTML PUBLIC "-//W3C//DD HTML 4.01 Frameset//EN"')
s.append('"http://www.w3.org/TR/html4/frameset.dtd">')
s.append('<html>')
s.append('<title> {:s}</title>'.format(title))
s.append('<head>')
s.append('<style type="text/css">')
s.append('<!--')
s.append('@import url(webpage_default.css);')
s.append('-->')
s.append('</style>')
s.append('</head>')
s.append('<h1 align=center> {:s} </h1>'.format(title))
s.append('<script type="text/javascript" src="jquery.min.js"></script>')
s.append(('<script type="text/javascript" '
'src="jquery.tablesorter.min.js"></script>'))
f.write('\n'.join(s) + "\n")
@classmethod
def writeColumnNames(cls, f, columnnames, non_sortable_columns=None):
"""Write the column names to webpage file ``f``
Parameters
----------
f : file-like object
where to write to; must have a ``write`` method
"""
# Check if columnnames is a list
if not isinstance(columnnames,list):
columnnames = list(columnnames)
s = []
s.append(''' <script type="text/javascript" id="js">
// Add ability to sort the table
$(document).ready(function() {
$.tablesorter.defaults.sortList = [[0,0]];
$("table").tablesorter({
// pass the headers argument and assing a object
headers: {
// assign the secound column (we start counting zero)''')
if non_sortable_columns:
for ns in non_sortable_columns:
s.append('''
%d: {
sorter: false
},''' %(ns))
s.append(''' }
});
});
</script>''')
s.append(''' <table id="myTable" cellspacing="1" class="tablesorter">
<thead>''')
for col in columnnames:
s.append('<th class="axe" height="35"> {:s} </th>'.format(col))
s.append('</thead>')
s.append('<tbody>')
f.write('\n'.join(s) + "\n")
f.flush()
@classmethod
def writeColumn(cls, f, dictionary):
"""Write rows to webpage file ``f``
Parameters
----------
f : file-like object
where to write to; must have a ``write`` method
"""
s = []
key_list = ["number","link","table","image"]
s.append('<tr class="axe">')
for key, value in dictionary.iteritems():
if key.lower().split('_')[0] not in key_list:
print("[ERROR] Key, {:s}, not in key_list.")
return None
if key.lower().split('_')[0] == "number":
if isinstance(value,int):
s.append(('<td class="axe" '
'height="240">%d</td>' %(value)))
if isinstance(value,float):
s.append(('<td class="axe" '
'height="240">%0.5f</td>' %(value)))
if key.lower().split('_')[0] == "link":
if len(value)!=2:
print(('[ERROR] Value in Dictionary should include two '
'items: a string and a number.'))
return None
s.append(('<td class="axe" height="240"><a '
'href=%s>%d</a></td>' %(value[0],value[1])))
if key.lower().split('_')[0] =="table":
t=[]
t.append('<td class="axe">')
if isinstance(value,list):
for i in xrange(len(value)-1):
t.append('%s<br>' %(value[i]))
t.append('%s<p></td>' %(value[-1]))
else:
t.append('%s<p></td>' %(value))
s.append(''.join(t))
if key.lower().split('_')[0]=="image":
s.append('<td class="axe" ><img align=middle '
'src=%s alt=%s></td>' %(value,value))
s.append('</tr>')
f.write('\n'.join(s) + "\n")
f.flush()
@classmethod
def writeEnding(cls, f):
"""Write ending to webpage file ``f``
Parameters
----------
f : file-like object
where to write to; must have a ``write`` method
"""
s = []
s.append('</tbody></table>')
f.write('\n'.join(s) + "\n")
f.flush()