matrice graph classe
[iramuteq] / HTML / HTML_tutorial.py
1 # HTML.py tutorial - P. Lagadec\r
2 \r
3 # see also http://www.decalage.info/en/python/html for more details and\r
4 # updates.\r
5 \r
6 \r
7 import HTML\r
8 \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
12 \r
13 \r
14 #=== TABLES ===================================================================\r
15 \r
16 # 1) a simple HTML table may be built from a list of lists:\r
17 \r
18 table_data = [\r
19         ['Last name',   'First name',   'Age'],\r
20         ['Smith',       'John',         30],\r
21         ['Carpenter',   'Jack',         47],\r
22         ['Johnson',     'Paul',         62],\r
23     ]\r
24 \r
25 htmlcode = HTML.table(table_data)\r
26 print htmlcode\r
27 f.write(htmlcode)\r
28 f.write('<p>')\r
29 print '-'*79\r
30 \r
31 #-------------------------------------------------------------------------------\r
32 \r
33 # 2) a header row may be specified: it will appear in bold in browsers\r
34 \r
35 table_data = [\r
36         ['Smith',       'John',         30],\r
37         ['Carpenter',   'Jack',         47],\r
38         ['Johnson',     'Paul',         62],\r
39     ]\r
40 \r
41 htmlcode = HTML.table(table_data,\r
42     header_row=['Last name',   'First name',   'Age'])\r
43 print htmlcode\r
44 f.write(htmlcode)\r
45 f.write('<p>')\r
46 print '-'*79\r
47 \r
48 \r
49 #-------------------------------------------------------------------------------\r
50 \r
51 # 3) you may also create a Table object and add rows one by one:\r
52 \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
56 htmlcode = str(t)\r
57 print htmlcode\r
58 f.write(htmlcode)\r
59 f.write('<p>')\r
60 print '-'*79\r
61 \r
62 \r
63 #-------------------------------------------------------------------------------\r
64 \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
67 \r
68 def gen_rows(i):\r
69     'rows generator'\r
70     for x in range(1,i):\r
71         yield [x, x*x, x*x*x]\r
72 \r
73 htmlcode = HTML.table(gen_rows(10), header_row=['x', 'square(x)', 'cube(x)'])\r
74 print htmlcode\r
75 f.write(htmlcode)\r
76 f.write('<p>')\r
77 print '-'*79\r
78 \r
79 \r
80 #-------------------------------------------------------------------------------\r
81 \r
82 # 5) to choose a specific background color for a cell, use a TableCell\r
83 #    object:\r
84 \r
85 HTML_COLORS = ['Black', 'Green', 'Silver', 'Lime', 'Gray', 'Olive', 'White',\r
86     'Maroon', 'Navy', 'Red', 'Blue', 'Purple', 'Teal', 'Fuchsia', 'Aqua']\r
87 \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
92 htmlcode = str(t)\r
93 print htmlcode\r
94 f.write(htmlcode)\r
95 f.write('<p>')\r
96 print '-'*79\r
97 \r
98 \r
99 #-------------------------------------------------------------------------------\r
100 \r
101 # 6) A simple way to generate a test report:\r
102 \r
103 # dictionary of test results, indexed by test id:\r
104 test_results = {\r
105         'test 1': 'success',\r
106         'test 2': 'failure',\r
107         'test 3': 'success',\r
108         'test 4': 'error',\r
109     }\r
110 \r
111 # dict of colors for each result:\r
112 result_colors = {\r
113         'success':      'lime',\r
114         'failure':      'red',\r
115         'error':        'yellow',\r
116     }\r
117 \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
125 htmlcode = str(t)\r
126 print htmlcode\r
127 f.write(htmlcode)\r
128 f.write('<p>')\r
129 print '-'*79\r
130 \r
131 #-------------------------------------------------------------------------------\r
132 \r
133 # 7) sample table with column attributes and styles:\r
134 table_data = [\r
135         ['Smith',       'John',         30,    4.5],\r
136         ['Carpenter',   'Jack',         47,    7],\r
137         ['Johnson',     'Paul',         62,    10.55],\r
138     ]\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
145 print htmlcode\r
146 print '-'*79\r
147 \r
148 \r
149 \r
150 #=== LISTS ===================================================================\r
151 \r
152 # 1) a HTML list (with bullets) may be built from a Python list of strings:\r
153 \r
154 a_list = ['john', 'paul', 'jack']\r
155 htmlcode = HTML.list(a_list)\r
156 print htmlcode\r
157 f.write(htmlcode)\r
158 f.write('<p>')\r
159 print '-'*79\r
160 \r
161 \r
162 # 2) it is easy to change it into a numbered (ordered) list:\r
163 \r
164 htmlcode = HTML.list(a_list, ordered=True)\r
165 print htmlcode\r
166 f.write(htmlcode)\r
167 f.write('<p>')\r
168 print '-'*79\r
169 \r
170 \r
171 # 3) Lines of a list may also be added one by one, when using the List class:\r
172 \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
177 print htmlcode\r
178 f.write(htmlcode)\r
179 f.write('<p>')\r
180 print '-'*79\r
181 \r
182 \r
183 # 4) To save memory, a large list may be built from a generator:\r
184 \r
185 def gen_lines(i):\r
186     'lines 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
190 print htmlcode\r
191 f.write(htmlcode)\r
192 f.write('<p>')\r
193 print '-'*79\r
194 \r
195 \r
196 #=== LINKS ===================================================================\r
197 \r
198 # How to create a link:\r
199 \r
200 htmlcode = HTML.link('Decalage website', 'http://www.decalage.info')\r
201 print htmlcode\r
202 f.write(htmlcode)\r
203 f.write('<p>')\r
204 print '-'*79\r
205 \r
206 \r
207 f.close()\r
208 print '\nOpen the file %s in a browser to see the result.' % HTMLFILE\r