Skip to content
Create a gist now

Instantly share code, notes, and snippets.

class TableCell(object):
bg_colour = None
def __init__(self, value, header=False, bg_colour=None, text_colour=None):
self.value = value
self.header = header
self.bg_colour = bg_colour
self.text_colour = text_colour
def _make_css(self):
rules = []
if self.bg_colour:
rules.append('background-color:%s' % self.bg_colour)
if self.text_colour:
rules.append('color:%s' % self.text_colour)
return '; '.join(rules)
def _repr_html_(self):
tag = 'th' if self.header else 'td'
attrs = []
style = self._make_css()
if style:
return "<%s %s>%s</%s>"% (tag, ' '.join(attrs), self.value, tag)
class TableRow(object):
def __init__(self, *cells):
self.cells = []
for c in cells:
def append_cell(self, c):
if not isinstance(c, TableCell):
c = TableCell(c)
def _repr_html_(self):
return '<tr>' + ''.join(c._repr_html_() for c in self.cells) + '</tr>'
class TableHeaderRow(TableRow):
def append_cell(self, c):
if not isinstance(c, TableCell):
c = TableCell(c, header=True)
class Table(object):
def __init__(self, *rows):
self.rows = []
for r in rows:
def append_row(self, r):
if not isinstance(r, TableRow):
r = TableRow(*r)
def _repr_html_(self):
return '<table>\n' + '\n'.join(r._repr_html_() for r in self.rows) + '\n</table>'

Nice! May I ask, what of csv files?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.