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?']], h
| 186 | |
| 187 | |
| 188 | class SimpleTable: |
| 189 | """A table class to create HTML tables, populated by HTML table rows. |
| 190 | |
| 191 | Example: |
| 192 | # Table from lists |
| 193 | table = SimpleTable([['Hello,', 'world!'], ['How', 'are', 'you?']]) |
| 194 | |
| 195 | # Table with header row |
| 196 | table = SimpleTable([['Hello,', 'world!'], ['How', 'are', 'you?']], |
| 197 | header_row=['Header1', 'Header2', 'Header3']) |
| 198 | |
| 199 | # Table from SimpleTableRow |
| 200 | rows = SimpleTableRow(['Hello,', 'world!']) |
| 201 | table = SimpleTable(rows) |
| 202 | """ |
| 203 | |
| 204 | def __init__(self, rows=None, header_row=None, css_class=None): |
| 205 | """Table constructor. |
| 206 | |
| 207 | Keyword arguments: |
| 208 | rows -- iterable of SimpleTableRow |
| 209 | header_row -- row that will be displayed at the beginning of the table. |
| 210 | if this row is SimpleTableRow, it is the programmer's |
| 211 | responsibility to verify whether it was created with the |
| 212 | header flag set to True. |
| 213 | css_class -- table CSS class |
| 214 | """ |
| 215 | rows = rows or [] |
| 216 | if isinstance(rows[0], SimpleTableRow): |
| 217 | self.rows = rows |
| 218 | else: |
| 219 | self.rows = [SimpleTableRow(row) for row in rows] |
| 220 | |
| 221 | if header_row is None: |
| 222 | self.header_row = None |
| 223 | elif isinstance(header_row, SimpleTableRow): |
| 224 | self.header_row = header_row |
| 225 | else: |
| 226 | self.header_row = SimpleTableRow(header_row, header=True) |
| 227 | |
| 228 | self.css_class = css_class |
| 229 | |
| 230 | def __str__(self): |
| 231 | """Return the HTML code for the table as a string.""" |
| 232 | table = [] |
| 233 | |
| 234 | if self.css_class: |
| 235 | table.append("<table class=%s>" % self.css_class) |
| 236 | else: |
| 237 | table.append("<table>") |
| 238 | |
| 239 | if self.header_row: |
| 240 | table.append(str(self.header_row)) |
| 241 | |
| 242 | for row in self.rows: |
| 243 | table.append(str(row)) |
| 244 | |
| 245 | table.append("</table>") |
no outgoing calls
no test coverage detected