forked from mraygalaxy/HTML.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTML_tutorial.py
208 lines (153 loc) · 5.26 KB
/
HTML_tutorial.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
# HTML.py tutorial - P. Lagadec
# see also http://www.decalage.info/en/python/html for more details and
# updates.
import HTML
# open an HTML file to show output in a browser
HTMLFILE = 'HTML_tutorial_output.html'
f = open(HTMLFILE, 'w')
#=== TABLES ===================================================================
# 1) a simple HTML table may be built from a list of lists:
table_data = [
['Last name', 'First name', 'Age'],
['Smith', 'John', 30],
['Carpenter', 'Jack', 47],
['Johnson', 'Paul', 62],
]
htmlcode = HTML.table(table_data)
print(htmlcode)
f.write(htmlcode)
f.write('<p>')
print('-'*79)
#-------------------------------------------------------------------------------
# 2) a header row may be specified: it will appear in bold in browsers
table_data = [
['Smith', 'John', 30],
['Carpenter', 'Jack', 47],
['Johnson', 'Paul', 62],
]
htmlcode = HTML.table(table_data,
header_row=['Last name', 'First name', 'Age'])
print(htmlcode)
f.write(htmlcode)
f.write('<p>')
print('-'*79)
#-------------------------------------------------------------------------------
# 3) you may also create a Table object and add rows one by one:
t = HTML.Table(header_row=['x', 'square(x)', 'cube(x)'])
for x in range(1,10):
t.rows.append([x, x*x, x*x*x])
htmlcode = str(t)
print(htmlcode)
f.write(htmlcode)
f.write('<p>')
print('-'*79)
#-------------------------------------------------------------------------------
# 4) rows may be any iterable (list, tuple, ...) including a generator:
# (this is useful to save memory when generating a large table)
def gen_rows(i):
'rows generator'
for x in range(1,i):
yield [x, x*x, x*x*x]
htmlcode = HTML.table(gen_rows(10), header_row=['x', 'square(x)', 'cube(x)'])
print(htmlcode)
f.write(htmlcode)
f.write('<p>')
print('-'*79)
#-------------------------------------------------------------------------------
# 5) to choose a specific background color for a cell, use a TableCell
# object:
HTML_COLORS = ['Black', 'Green', 'Silver', 'Lime', 'Gray', 'Olive', 'White',
'Maroon', 'Navy', 'Red', 'Blue', 'Purple', 'Teal', 'Fuchsia', 'Aqua']
t = HTML.Table(header_row=['Name', 'Color'])
for colorname in HTML_COLORS:
colored_cell = HTML.TableCell(' ', bgcolor=colorname)
t.rows.append([colorname, colored_cell])
htmlcode = str(t)
print(htmlcode)
f.write(htmlcode)
f.write('<p>')
print('-'*79)
#-------------------------------------------------------------------------------
# 6) A simple way to generate a test report:
# dictionary of test results, indexed by test id:
test_results = {
'test 1': 'success',
'test 2': 'failure',
'test 3': 'success',
'test 4': 'error',
}
# dict of colors for each result:
result_colors = {
'success': 'lime',
'failure': 'red',
'error': 'yellow',
}
t = HTML.Table(header_row=['Test', 'Result'])
for test_id in sorted(test_results):
# create the colored cell:
color = result_colors[test_results[test_id]]
colored_result = HTML.TableCell(test_results[test_id], bgcolor=color)
# append the row with two cells:
t.rows.append([test_id, colored_result])
htmlcode = str(t)
print(htmlcode)
f.write(htmlcode)
f.write('<p>')
print('-'*79)
#-------------------------------------------------------------------------------
# 7) sample table with column attributes and styles:
table_data = [
['Smith', 'John', 30, 4.5],
['Carpenter', 'Jack', 47, 7],
['Johnson', 'Paul', 62, 10.55],
]
htmlcode = HTML.table(table_data,
header_row = ['Last name', 'First name', 'Age', 'Score'],
col_width=['', '20%', '10%', '10%'],
col_align=['left', 'center', 'right', 'char'],
col_styles=['font-size: large', '', 'font-size: small', 'background-color:yellow'])
f.write(htmlcode + '<p>\n')
print(htmlcode)
print('-'*79)
#=== LISTS ===================================================================
# 1) a HTML list (with bullets) may be built from a Python list of strings:
a_list = ['john', 'paul', 'jack']
htmlcode = HTML.list(a_list)
print(htmlcode)
f.write(htmlcode)
f.write('<p>')
print('-'*79)
# 2) it is easy to change it into a numbered (ordered) list:
htmlcode = HTML.list(a_list, ordered=True)
print(htmlcode)
f.write(htmlcode)
f.write('<p>')
print('-'*79)
# 3) Lines of a list may also be added one by one, when using the List class:
html_list = HTML.List()
for i in range(1,10):
html_list.lines.append('square(%d) = %d' % (i, i*i))
htmlcode = str(html_list)
print(htmlcode)
f.write(htmlcode)
f.write('<p>')
print('-'*79)
# 4) To save memory, a large list may be built from a generator:
def gen_lines(i):
'lines generator'
for x in range(1,i):
yield 'square(%d) = %d' % (x, x*x)
htmlcode = HTML.list(gen_lines(10))
print(htmlcode)
f.write(htmlcode)
f.write('<p>')
print('-'*79)
#=== LINKS ===================================================================
# How to create a link:
htmlcode = HTML.link('Decalage website', 'http://www.decalage.info')
print(htmlcode)
f.write(htmlcode)
f.write('<p>')
print('-'*79)
f.close()
print('\nOpen the file %s in a browser to see the result.' % HTMLFILE)