This repository was archived by the owner on Jun 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 301
/
Copy pathdash-table-conditional-formatting.py
95 lines (80 loc) · 2.28 KB
/
dash-table-conditional-formatting.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
import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(50, 4), columns=list('ABCD'))
COLORS = [
{
'background': '#fef0d9',
'text': 'rgb(30, 30, 30)'
},
{
'background': '#fdcc8a',
'text': 'rgb(30, 30, 30)'
},
{
'background': '#fc8d59',
'text': 'rgb(30, 30, 30)'
},
{
'background': '#d7301f',
'text': 'rgb(30, 30, 30)'
},
]
def is_numeric(value):
try:
float(value)
return True
except ValueError:
return False
def cell_style(value, min_value, max_value):
style = {}
if is_numeric(value):
relative_value = (value - min_value) / (max_value - min_value)
if relative_value <= 0.25:
style = {
'backgroundColor': COLORS[0]['background'],
'color': COLORS[0]['text']
}
elif relative_value <= 0.5:
style = {
'backgroundColor': COLORS[1]['background'],
'color': COLORS[1]['text']
}
elif relative_value <= 0.75:
style = {
'backgroundColor': COLORS[2]['background'],
'color': COLORS[2]['text']
}
elif relative_value <= 1:
style = {
'backgroundColor': COLORS[3]['background'],
'color': COLORS[3]['text']
}
return style
def generate_table(dataframe, max_rows=100):
max_value = df.max(numeric_only=True).max()
min_value = df.min(numeric_only=True).max()
rows = []
for i in range(min(len(dataframe), max_rows)):
row = []
for col in dataframe.columns:
value = dataframe.iloc[i][col]
style = cell_style(value, min_value, max_value)
row.append(html.Td(value, style=style))
rows.append(html.Tr(row))
return html.Table(
# Header
[html.Tr([html.Th(col) for col in dataframe.columns])] +
# Body
rows)
app = dash.Dash()
app.css.append_css({
"external_url": "https://codepen.io/chriddyp/pen/bWLwgP.css"
})
app.layout = html.Div(children=[
generate_table(df)
])
if __name__ == '__main__':
app.run_server(debug=True)