Created
January 19, 2015 17:35
-
-
Save pmav99/48674d01053175a0c6c9 to your computer and use it in GitHub Desktop.
Bootstrap Table Helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# author: Panagiotis Mavrogiorgos | |
# email: gmail, pmav99 | |
""" | |
A help class for generating bootstrap tables. | |
""" | |
from __future__ import division | |
from __future__ import print_function | |
from __future__ import unicode_literals | |
from __future__ import absolute_import | |
from jinja2 import Template | |
table_template = """ | |
<table class="{{ table_classes }}"> | |
{%- if caption %}<caption>{{ caption }}</caption>{% endif -%} | |
{# If there is a header row, display it! #} | |
{%- if headers %} | |
<thead> | |
<tr class=""> | |
{%- for header in headers %} | |
<th>{{ header }}</th> | |
{%- endfor %} | |
</tr> | |
</thead> | |
{%- endif %} | |
<tbody> | |
{%- for row in rows %} | |
<tr class=""> | |
{%- for cell in row %} | |
<td>{{cell}}</td> | |
{%- endfor %} | |
</tr> | |
{%- endfor %} | |
</tbody> | |
{# If there is a footer row, display it! #} | |
{%- if footers -%} | |
<tfoot> | |
<tr class=""> | |
{%- for footer in footers %} | |
<th class="">{{ footer }}</th> | |
{%- endfor %} | |
</tr> | |
</tfoot> | |
{%- endif %} | |
</table> | |
""" | |
class BootstrapTable(object): | |
pad_value = " " | |
def __init__(self, rows, headers=None, footers=None, caption=None, | |
hover=False, condensed=False, bordered=False, stripped=False, row_padding=True): | |
self.template = Template(table_template) | |
self.caption = caption | |
self.headers = headers | |
self.footers = footers | |
self.rows = rows | |
# Determine table classes. | |
self.table_classes = table_classes = ["table"] | |
if hover: | |
table_classes.append("table-hover") | |
if condensed: | |
table_classes.append("table-condensed") | |
if bordered: | |
table_classes.append("table-bordered") | |
if stripped: | |
table_classes.append("table-stripped") | |
self.table_classes = " ".join(table_classes) | |
if row_padding: | |
self.pad_rows() | |
def pad_rows(self): | |
""" | |
Appends <tds> with " " to the <tfoot>, <thead> and the <tbody> rows. | |
We do that in order to maintain visual uniformity. | |
""" | |
nbsp = " " | |
max_number_of_columns = self.calc_max_number_of_columns() | |
if len(self.headers) < max_number_of_columns: | |
self.headers.append(nbsp) | |
for row in self.rows: | |
while len(row) < max_number_of_columns: | |
row.append(nbsp) | |
if len(self.footers) < max_number_of_columns: | |
self.footers.append(nbsp) | |
def calc_max_number_of_columns(self): | |
""" Returns the max number of columns between the <thead>, the <tfoot> and the <tbody> rows. """ | |
data_rows = [self.headers, self.footers] + self.rows | |
return max(map(len, (data_rows))) | |
def render(self): | |
return self.template.render( | |
table_classes=self.table_classes, | |
caption=self.caption, | |
headers=self.headers, | |
footers=self.footers, | |
rows=self.rows | |
) | |
B = BootstrapTable( | |
headers=["a", "b", "c", "d", "e"], | |
condensed=True, | |
stripped=True, | |
rows=[ | |
[1, 2, 3, 4], | |
[5, 6, 7], | |
[8, 9, 10, 11], | |
], | |
footers=[14, 17, 20, 15], | |
) | |
print(B.render()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment