import dash_ag_grid as dag
from dash import Dash, html
import pandas as pd
import dash_bootstrap_components as dbc
app = Dash(__name__, external_stylesheets=[dbc.icons.BOOTSTRAP, dbc.themes.BOOTSTRAP])
df = pd.read_csv(
"https://raw.githubusercontent.com/plotly/datasets/master/ag-grid/olympic-winners.csv"
)
template_with_icon = """
<div class="ag-cell-label-container" role="presentation">
<span ref="eMenu" class="ag-header-icon ag-header-cell-menu-button"></span>
<span ref="eFilterButton" class="ag-header-icon ag-header-cell-filter-button"></span>
<div ref="eLabel" class="ag-header-cell-label" role="presentation">
<span ref="eSortOrder" class="ag-header-icon ag-sort-order ag-hidden"></span>
<span ref="eSortAsc" class="ag-header-icon ag-sort-ascending-icon ag-hidden"></span>
<span ref="eSortDesc" class="ag-header-icon ag-sort-descending-icon ag-hidden"></span>
<span ref="eSortMixed" class="ag-header-icon ag-sort-mixed-icon ag-hidden"></span>
<span ref="eSortNone" class="ag-header-icon ag-sort-none-icon ag-hidden"></span>
<i class="bi bi-trophy ms-2"></i>
<span ref="eText" class="ag-header-cell-text" role="columnheader"></span>
<span ref="eFilter" class="ag-header-icon ag-filter-icon"></span>
</div>
</div>
"""
columnDefs = [
{"field": "athlete"},
{"field": "sport", "type": "rightAligned"},
{"field": "gold", "type": "numericColumn", "headerComponentParams": {"template": template_with_icon}},
{"field": "silver", "type": "numericColumn", "headerComponentParams": {"template": template_with_icon}},
{"field": "bronze", "type": "numericColumn", "headerComponentParams": {"template": template_with_icon}},
]
app.layout = dbc.Container(
[
html.Div("Example of custom header templates"),
dag.AgGrid(
id="grid",
columnDefs=columnDefs,
rowData=df.to_dict("records"),
columnSize="sizeToFit",
dashGridOptions={"animateRows": False},
dangerously_allow_code=True
)
]
)
if __name__ == "__main__":
app.run(debug=True, port=8052)