forked from matheusportela/simpletable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpletable.py
306 lines (243 loc) · 8.54 KB
/
simpletable.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
simpletable.py - v0.1 2014-07-31 Matheus Vieira Portela
This module provides simple classes and interfaces to generate simple HTML
tables based on Python native types, such as lists.
Author's website: http://matheusvportela.wordpress.com/
"""
__version__ = '0.3'
__date__ = '2014-08-20'
__author__ = 'Matheus Vieira Portela'
### CHANGES ###
# 2014-07-31: v0.1 MVP:
# - First version
# 2014-08-05: v0.2 MVP:
# - Method for defining header rows
# - SimpleTable method to create a SimpleTable from lists
# - Method to create a table from a simple list of elements and a column size
# 2014-08-20: v0.3 MVP:
# - Enable SimplePage to accept a list of tables
# - Enable SimplePage to iterate over its tables
### TODO ###
#
### REFERENCES ###
# Decalage HTML.py module: http://www.decalage.info/python/html
import codecs
class SimpleTableCell(object):
"""A table class to create table cells.
Example:
cell = SimpleTableCell('Hello, world!')
"""
def __init__(self, text, header=False):
"""Table cell constructor.
Keyword arguments:
text -- text to be displayed
header -- flag to indicate this cell is a header cell.
"""
self.text = text
self.header = header
def __str__(self):
"""Return the HTML code for the table cell."""
if self.header:
return '<th>%s</th>' %(self.text)
else:
return '<td>%s</td>' %(self.text)
class SimpleTableRow(object):
"""A table class to create table rows, populated by table cells.
Example:
# Row from list
row = SimpleTableRow(['Hello,', 'world!'])
# Row from SimpleTableCell
cell1 = SimpleTableCell('Hello,')
cell2 = SimpleTableCell('world!')
row = SimpleTableRow([cell1, cell2])
"""
def __init__(self, cells=[], header=False):
"""Table row constructor.
Keyword arguments:
cells -- iterable of SimpleTableCell (default None)
header -- flag to indicate this row is a header row.
if the cells are SimpleTableCell, it is the programmer's
responsibility to verify whether it was created with the
header flag set to True.
"""
if isinstance(cells[0], SimpleTableCell):
self.cells = cells
else:
self.cells = [SimpleTableCell(cell, header=header) for cell in cells]
self.header = header
def __str__(self):
"""Return the HTML code for the table row and its cells as a string."""
row = []
row.append('<tr>')
for cell in self.cells:
row.append(str(cell))
row.append('</tr>')
return '\n'.join(row)
def __iter__(self):
"""Iterate through row cells"""
for cell in self.cells:
yield cell
def add_cell(self, cell):
"""Add a SimpleTableCell object to the list of cells."""
self.cells.append(cell)
def add_cells(self, cells):
"""Add a list of SimpleTableCell objects to the list of cells."""
for cell in cells:
self.cells.append(cell)
class SimpleTable(object):
"""A table class to create HTML tables, populated by HTML table rows.
Example:
# Table from lists
table = SimpleTable([['Hello,', 'world!'], ['How', 'are', 'you?']])
# Table with header row
table = SimpleTable([['Hello,', 'world!'], ['How', 'are', 'you?']],
header_row=['Header1', 'Header2', 'Header3'])
# Table from SimpleTableRow
rows = SimpleTableRow(['Hello,', 'world!'])
table = SimpleTable(rows)
"""
def __init__(self, rows=[], header_row=None, css_class=None):
"""Table constructor.
Keyword arguments:
rows -- iterable of SimpleTableRow
header_row -- row that will be displayed at the beginning of the table.
if this row is SimpleTableRow, it is the programmer's
responsibility to verify whether it was created with the
header flag set to True.
css_class -- table CSS class
"""
if isinstance(rows[0], SimpleTableRow):
self.rows = rows
else:
self.rows = [SimpleTableRow(row) for row in rows]
if header_row is None:
self.header_row = None
elif isinstance(header_row, SimpleTableRow):
self.header_row = header_row
else:
self.header_row = SimpleTableRow(header_row, header=True)
self.css_class = css_class
def __str__(self):
"""Return the HTML code for the table as a string."""
table = []
if self.css_class:
table.append('<table class=%s>' % self.css_class)
else:
table.append('<table>')
if self.header_row:
table.append(str(self.header_row))
for row in self.rows:
table.append(str(row))
table.append('</table>')
return '\n'.join(table)
def __iter__(self):
"""Iterate through table rows"""
for row in self.rows:
yield row
def add_row(self, row):
"""Add a SimpleTableRow object to the list of rows."""
self.rows.append(row)
def add_rows(self, rows):
"""Add a list of SimpleTableRow objects to the list of rows."""
for row in rows:
self.rows.append(row)
class HTMLPage(object):
"""A class to create HTML pages containing CSS and tables."""
def __init__(self, tables=[], css=None, encoding="utf-8"):
"""HTML page constructor.
Keyword arguments:
tables -- List of SimpleTable objects
css -- Cascading Style Sheet specification that is appended before the
table string
encoding -- Characters encoding. Default: UTF-8
"""
self.tables = tables
self.css = css
self.encoding = encoding
def __str__(self):
"""Return the HTML page as a string."""
page = []
if self.css:
page.append('<style type="text/css">\n%s\n</style>' % self.css)
# Set encoding
page.append('<meta http-equiv="Content-Type" content="text/html;'
'charset=%s">' % self.encoding)
for table in self.tables:
page.append(str(table))
page.append('<br />')
return '\n'.join(page)
def __iter__(self):
"""Iterate through tables"""
for table in self.tables:
yield table
def save(self, filename):
"""Save HTML page to a file using the proper encoding"""
with codecs.open(filename, 'w', self.encoding) as outfile:
for line in str(self):
outfile.write(line)
def add_table(self, table):
"""Add a SimpleTable to the page list of tables"""
self.tables.append(table)
def fit_data_to_columns(data, num_cols):
"""Format data into the configured number of columns in a proper format to
generate a SimpleTable.
Example:
test_data = [str(x) for x in range(20)]
fitted_data = fit_data_to_columns(test_data, 5)
table = SimpleTable(fitted_data)
"""
num_iterations = len(data)/num_cols
if len(data)%num_cols != 0:
num_iterations += 1
return [data[num_cols*i:num_cols*i + num_cols] for i in range(num_iterations)]
### Example usage ###
if __name__ == "__main__":
css = """
table.mytable {
font-family: times;
font-size:12px;
color:#000000;
border-width: 1px;
border-color: #eeeeee;
border-collapse: collapse;
background-color: #ffffff;
width=100%;
max-width:550px;
table-layout:fixed;
}
table.mytable th {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #eeeeee;
background-color: #e6eed6;
color:#000000;
}
table.mytable td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #eeeeee;
}
#code {
display:inline;
font-family: courier;
color: #3d9400;
}
#string {
display:inline;
font-weight: bold;
}
"""
table1 = SimpleTable([['Hello,', 'world!'], ['How', 'are', 'you?']],
header_row=['Header1', 'Header2', 'Header3'],
css_class='mytable')
table2 = SimpleTable([['Testing', 'this'], ['table', 'here']],
css_class='mytable')
page = HTMLPage()
page.add_table(table1)
page.add_table(table2)
page.css = css
page.save("test.html")