-
Notifications
You must be signed in to change notification settings - Fork 44
/
classes.py
95 lines (67 loc) · 1.71 KB
/
classes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from flask_table import Table, Col
"""If we want to put an HTML class onto the table element, we can set
the "classes" attribute on the table class. This should be an iterable
of that are joined together and all added as classes. If none are set,
then no class is added to the table element.
"""
class Item(object):
def __init__(self, name, description):
self.name = name
self.description = description
class ItemTableOneClass(Table):
classes = ['class1']
name = Col('Name')
description = Col('Description')
class ItemTableTwoClasses(Table):
classes = ['class1', 'class2']
name = Col('Name')
description = Col('Description')
def one_class(items):
table = ItemTableOneClass(items)
# or {{ table }} in jinja
print(table.__html__())
"""Outputs:
<table class="class1">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>Name1</td>
<td>Description1</td>
</tr>
</tbody>
</table>
"""
def two_classes(items):
table = ItemTableTwoClasses(items)
# or {{ table }} in jinja
print(table.__html__())
"""Outputs:
<table class="class1 class2">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>Name1</td>
<td>Description1</td>
</tr>
</tbody>
</table>
"""
def main():
items = [Item('Name1', 'Description1')]
# user ItemTableOneClass
one_class(items)
print('\n######################\n')
# user ItemTableTwoClasses
two_classes(items)
if __name__ == '__main__':
main()