-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagstyler.py
More file actions
74 lines (60 loc) · 1.98 KB
/
agstyler.py
File metadata and controls
74 lines (60 loc) · 1.98 KB
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
from st_aggrid import AgGrid
from st_aggrid.grid_options_builder import GridOptionsBuilder
from st_aggrid.shared import GridUpdateMode, JsCode
MAX_TABLE_HEIGHT = 500
def get_numeric_style_with_precision(precision: int) -> dict:
return {"type": ["numericColumn", "customNumericFormat"], "precision": precision}
PRECISION_ZERO = get_numeric_style_with_precision(0)
PRECISION_ONE = get_numeric_style_with_precision(1)
PRECISION_TWO = get_numeric_style_with_precision(2)
PINLEFT = {"pinned": "left"}
def draw_grid(
df,
formatter: dict = None,
selection="multiple",
use_checkbox=False,
fit_columns=False,
theme="streamlit",
max_height: int = MAX_TABLE_HEIGHT,
wrap_text: bool = False,
auto_height: bool = False,
grid_options: dict = None,
key=None,
css: dict = None
):
gb = GridOptionsBuilder()
gb.configure_default_column(
filterable=True,
groupable=False,
editable=False,
wrapText=wrap_text,
autoHeight=auto_height
)
if grid_options is not None:
gb.configure_grid_options(**grid_options)
for latin_name, (cyr_name, style_dict) in formatter.items():
gb.configure_column(latin_name, header_name=cyr_name, **style_dict)
gb.configure_selection(selection_mode=selection, use_checkbox=use_checkbox)
return AgGrid(
df,
gridOptions=gb.build(),
update_mode=GridUpdateMode.SELECTION_CHANGED | GridUpdateMode.VALUE_CHANGED,
allow_unsafe_jscode=True,
fit_columns_on_grid_load=fit_columns,
height=min(max_height, (1 + len(df.index)) * 29),
theme=theme,
key=key,
custom_css=css
)
def highlight(color, condition):
code = f"""
function(params) {{
color = "{color}";
if ({condition}) {{
return {{
'backgroundColor': color
}}
}}
}};
"""
return JsCode(code)