Spaces:
No application file
No application file
# adjusted from: https://github.com/nryabykh/streamlit-aggrid-hints | |
from st_aggrid import AgGrid | |
from st_aggrid.grid_options_builder import GridOptionsBuilder | |
from st_aggrid.shared import GridUpdateMode, JsCode | |
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, | |
pagination_size=0, | |
theme="streamlit", | |
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) | |
if pagination_size > 0: | |
gb.configure_pagination(enabled=True, paginationAutoPageSize=False, paginationPageSize=pagination_size) | |
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, | |
theme=theme, | |
key=key, | |
custom_css=css, | |
enable_enterprise_modules=False | |
) | |
def highlight(color, condition): | |
code = f""" | |
function(params) {{ | |
color = "{color}"; | |
if ({condition}) {{ | |
return {{ | |
'backgroundColor': color | |
}} | |
}} | |
}}; | |
""" | |
return JsCode(code) | |