2019-01-09 15:15:22 +01:00
|
|
|
class table_gen:
|
2019-02-07 15:33:39 +01:00
|
|
|
"""small library of functions to generate the html tables"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, name):
|
2019-01-09 15:15:22 +01:00
|
|
|
self.name = name
|
|
|
|
|
self.rows = []
|
|
|
|
|
self.table_id = 'data'
|
|
|
|
|
|
2019-02-07 15:33:39 +01:00
|
|
|
def add_row(self, row):
|
|
|
|
|
"""add a row to table_gen object"""
|
2019-01-09 15:15:22 +01:00
|
|
|
self.rows.append(row)
|
|
|
|
|
|
|
|
|
|
def gen_table_head(self):
|
2019-02-07 15:33:39 +01:00
|
|
|
"""generate html table header"""
|
2019-01-09 15:15:22 +01:00
|
|
|
html = ''
|
|
|
|
|
|
|
|
|
|
html += '<thead>'
|
|
|
|
|
html += '<tr>'
|
|
|
|
|
for col in self.rows[0]:
|
2019-02-07 15:33:39 +01:00
|
|
|
html += '<th>' + str(col) + '</th>'
|
2019-01-09 15:15:22 +01:00
|
|
|
html += '</tr>'
|
|
|
|
|
html += '</thead>'
|
|
|
|
|
return html
|
|
|
|
|
|
|
|
|
|
def gen_table_body(self):
|
2019-02-07 15:33:39 +01:00
|
|
|
"""generate html body (used after gen_table_head)"""
|
2019-01-09 15:15:22 +01:00
|
|
|
html = ''
|
|
|
|
|
|
|
|
|
|
html += '<tbody>'
|
|
|
|
|
html += '<tr>'
|
|
|
|
|
for row in self.rows[1:]:
|
|
|
|
|
html += '<tr>'
|
|
|
|
|
for col in row:
|
|
|
|
|
html += '<td>' + str(col) + '</td>'
|
|
|
|
|
html += '</tr>'
|
|
|
|
|
html += '</tr>'
|
|
|
|
|
html += '</tbody>'
|
|
|
|
|
return html
|
2019-02-07 15:33:39 +01:00
|
|
|
|
2019-01-09 15:15:22 +01:00
|
|
|
def to_html(self):
|
2019-02-07 15:33:39 +01:00
|
|
|
"""writes table_gen object to inline html"""
|
2019-01-09 15:15:22 +01:00
|
|
|
html = ''
|
|
|
|
|
html += '<table id= \"'+self.table_id+'\">'
|
|
|
|
|
html += self.gen_table_head()
|
|
|
|
|
html += self.gen_table_body()
|
|
|
|
|
html += '</table>'
|
2019-02-07 15:33:39 +01:00
|
|
|
|
2019-01-09 15:15:22 +01:00
|
|
|
return html
|