A class to create HTML pages containing CSS and tables.
| 261 | |
| 262 | |
| 263 | class HTMLPage: |
| 264 | """A class to create HTML pages containing CSS and tables.""" |
| 265 | |
| 266 | def __init__(self, tables=None, css=None, encoding="utf-8"): |
| 267 | """HTML page constructor. |
| 268 | |
| 269 | Keyword arguments: |
| 270 | tables -- List of SimpleTable objects |
| 271 | css -- Cascading Style Sheet specification that is appended before the |
| 272 | table string |
| 273 | encoding -- Characters encoding. Default: UTF-8 |
| 274 | """ |
| 275 | self.tables = tables or [] |
| 276 | self.css = css |
| 277 | self.encoding = encoding |
| 278 | |
| 279 | def __str__(self): |
| 280 | """Return the HTML page as a string.""" |
| 281 | page = [] |
| 282 | |
| 283 | if self.css: |
| 284 | page.append('<style type="text/css">\n%s\n</style>' % self.css) |
| 285 | |
| 286 | # Set encoding |
| 287 | page.append( |
| 288 | '<meta http-equiv="Content-Type" content="text/html;charset=%s">' % self.encoding |
| 289 | ) |
| 290 | |
| 291 | for table in self.tables: |
| 292 | page.append(str(table)) |
| 293 | page.append("<br />") |
| 294 | |
| 295 | return "\n".join(page) |
| 296 | |
| 297 | def __iter__(self): |
| 298 | """Iterate through tables""" |
| 299 | yield from self.tables |
| 300 | |
| 301 | def save(self, filename): |
| 302 | """Save HTML page to a file using the proper encoding""" |
| 303 | with codecs.open(filename, "w", self.encoding) as outfile: |
| 304 | for line in str(self): |
| 305 | outfile.write(line) |
| 306 | |
| 307 | def add_table(self, table): |
| 308 | """Add a SimpleTable to the page list of tables""" |
| 309 | self.tables.append(table) |
| 310 | |
| 311 | |
| 312 | def fit_data_to_columns(data, num_cols): |
no outgoing calls
no test coverage detected