-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.py
executable file
·124 lines (105 loc) · 3.35 KB
/
table.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# -*- coding: utf-8 -*-
'''
The objective is to build all the functions to create html tables and charts.
@author: Andréas
@date: Monday, August the 14th
'''
from bs4 import BeautifulSoup
from report import report
import pandas as pd
import html
def dashTable(dataframe, indexed=True, auto_width=True, headed=True):
'''
Sets an html table from a dataframe.
'''
def set_indexed_df():
if indexed:
return dataframe
else:
return dataframe.set_index([dataframe.columns[0]])
def set_width(df):
if auto_width:
return ''
else:
return '{}%'.format(100. / (len(df.columns) + 1))
def set_name(df):
if df.index.name:
return df.index.name
else:
return ''
def clean_content(table):
content = html.set_content(table)
content.replace('width=""', '')
return content
def set_table_body(df, width):
table = ''
for index, row in df.iterrows():
cells = [html.Td(row[i], style={'text-align':'right'}, width=width)
for i in range(len(row))]
# Left index insertion
cells.insert(0, html.Td(index, width=width))
table += html.Tr(cells)
return table
def set_table_head(df, width):
html_cells = [html.Td(col, style={'text-align':'right', 'valign':'top'}, width=width)
for col in df.columns]
# Top index column space insertion
if indexed:
html_cells.insert(0, html.Td('', width=width))
else:
name = set_name(df)
html_cells.insert(0, html.Td(name, width=width))
html_row = html.Tr(html_cells, style={'background':'white', 'font-weight':'bold'})
return html_row
def set_body():
df = set_indexed_df()
width = set_width(df)
table = set_table_body(df, width)
return table
def set_head():
if headed:
df = set_indexed_df()
width = set_width(df)
head = set_table_head(df, width)
return head
else:
return ''
head = set_head()
body = set_body()
table = head + body
content = clean_content(table)
table = html.Table(content)
return table
def seriesTable(series):
'''
Sets an html table from a series.
'''
dataframe = series.to_frame()
table = dashTable(dataframe, headed=False)
return table
def textTable(series):
''' Sets a text table in which the index are bolded with the corresponding value below. '''
html_text_table = ''
for index in series.index:
subtitle = html.Strong(index)
content = html.P(series[index], className="blue-text")
html_text_table += subtitle + content
return html_text_table
'''
==========================================================
'''
if __name__ == '__main__':
index = ['ourson', 'chaton', 'croco']
data = {
'mange' : ['poisson', 'pate', 'chevre'],
'boit': ['eau', 'lait', 'eau'],
'couleur': ['marron', 'blanc', 'vert']
}
df = pd.DataFrame(data, index=index)
d = {key: ''.join(value) for key, value in data.items()}
s = pd.Series(d)
table = textTable(s)
content = html.Div(table, className='row')
Report = report()
Report.New_Page(content)
Report.Build()