1 # HTML.py tutorial - P. Lagadec
\r
3 # see also http://www.decalage.info/en/python/html for more details and
\r
9 # open an HTML file to show output in a browser
\r
10 HTMLFILE = 'HTML_tutorial_output.html'
\r
11 f = open(HTMLFILE, 'w')
\r
14 #=== TABLES ===================================================================
\r
16 # 1) a simple HTML table may be built from a list of lists:
\r
19 ['Last name', 'First name', 'Age'],
\r
20 ['Smith', 'John', 30],
\r
21 ['Carpenter', 'Jack', 47],
\r
22 ['Johnson', 'Paul', 62],
\r
25 htmlcode = HTML.table(table_data)
\r
31 #-------------------------------------------------------------------------------
\r
33 # 2) a header row may be specified: it will appear in bold in browsers
\r
36 ['Smith', 'John', 30],
\r
37 ['Carpenter', 'Jack', 47],
\r
38 ['Johnson', 'Paul', 62],
\r
41 htmlcode = HTML.table(table_data,
\r
42 header_row=['Last name', 'First name', 'Age'])
\r
49 #-------------------------------------------------------------------------------
\r
51 # 3) you may also create a Table object and add rows one by one:
\r
53 t = HTML.Table(header_row=['x', 'square(x)', 'cube(x)'])
\r
54 for x in range(1,10):
\r
55 t.rows.append([x, x*x, x*x*x])
\r
63 #-------------------------------------------------------------------------------
\r
65 # 4) rows may be any iterable (list, tuple, ...) including a generator:
\r
66 # (this is useful to save memory when generating a large table)
\r
70 for x in range(1,i):
\r
71 yield [x, x*x, x*x*x]
\r
73 htmlcode = HTML.table(gen_rows(10), header_row=['x', 'square(x)', 'cube(x)'])
\r
80 #-------------------------------------------------------------------------------
\r
82 # 5) to choose a specific background color for a cell, use a TableCell
\r
85 HTML_COLORS = ['Black', 'Green', 'Silver', 'Lime', 'Gray', 'Olive', 'White',
\r
86 'Maroon', 'Navy', 'Red', 'Blue', 'Purple', 'Teal', 'Fuchsia', 'Aqua']
\r
88 t = HTML.Table(header_row=['Name', 'Color'])
\r
89 for colorname in HTML_COLORS:
\r
90 colored_cell = HTML.TableCell(' ', bgcolor=colorname)
\r
91 t.rows.append([colorname, colored_cell])
\r
99 #-------------------------------------------------------------------------------
\r
101 # 6) A simple way to generate a test report:
\r
103 # dictionary of test results, indexed by test id:
\r
105 'test 1': 'success',
\r
106 'test 2': 'failure',
\r
107 'test 3': 'success',
\r
111 # dict of colors for each result:
\r
118 t = HTML.Table(header_row=['Test', 'Result'])
\r
119 for test_id in sorted(test_results):
\r
120 # create the colored cell:
\r
121 color = result_colors[test_results[test_id]]
\r
122 colored_result = HTML.TableCell(test_results[test_id], bgcolor=color)
\r
123 # append the row with two cells:
\r
124 t.rows.append([test_id, colored_result])
\r
131 #-------------------------------------------------------------------------------
\r
133 # 7) sample table with column attributes and styles:
\r
135 ['Smith', 'John', 30, 4.5],
\r
136 ['Carpenter', 'Jack', 47, 7],
\r
137 ['Johnson', 'Paul', 62, 10.55],
\r
139 htmlcode = HTML.table(table_data,
\r
140 header_row = ['Last name', 'First name', 'Age', 'Score'],
\r
141 col_width=['', '20%', '10%', '10%'],
\r
142 col_align=['left', 'center', 'right', 'char'],
\r
143 col_styles=['font-size: large', '', 'font-size: small', 'background-color:yellow'])
\r
144 f.write(htmlcode + '<p>\n')
\r
150 #=== LISTS ===================================================================
\r
152 # 1) a HTML list (with bullets) may be built from a Python list of strings:
\r
154 a_list = ['john', 'paul', 'jack']
\r
155 htmlcode = HTML.list(a_list)
\r
162 # 2) it is easy to change it into a numbered (ordered) list:
\r
164 htmlcode = HTML.list(a_list, ordered=True)
\r
171 # 3) Lines of a list may also be added one by one, when using the List class:
\r
173 html_list = HTML.List()
\r
174 for i in range(1,10):
\r
175 html_list.lines.append('square(%d) = %d' % (i, i*i))
\r
176 htmlcode = str(html_list)
\r
183 # 4) To save memory, a large list may be built from a generator:
\r
187 for x in range(1,i):
\r
188 yield 'square(%d) = %d' % (x, x*x)
\r
189 htmlcode = HTML.list(gen_lines(10))
\r
196 #=== LINKS ===================================================================
\r
198 # How to create a link:
\r
200 htmlcode = HTML.link('Decalage website', 'http://www.decalage.info')
\r
208 print '\nOpen the file %s in a browser to see the result.' % HTMLFILE
\r