Dash AG Grid > Tree Data sum in orgHierarchy

Hi

my code:

import dash_ag_grid as dag
from dash import Dash, html, dcc
import pandas as pd

app = Dash(__name__)

data = {
    'org': ['org1', 'org2', 'org1'],
    'dep': ['dep1', 'dep2', 'dep3'],
    'services_success': [10, 20, 30],
}

df = pd.DataFrame(data)


rowData = []

for index, row in df.iterrows():
    rowData.append({
        "orgHierarchy": [row['org'], row['dep']],
        "services_success": row['services_success'],
    },
    )


grid = html.Div(
    [
        dag.AgGrid(
            id="tree-data-example",
            columnDefs=[
                {"field": "services_success"},
            ],
            defaultColDef={
                "flex": 1,
            },
            dashGridOptions={
                "autoGroupColumnDef": {
                    "headerName": "Organisation",
                    "minWidth": 300,
                },
                "groupDefaultExpanded": -1,
                "getDataPath": {"function": "getDataPath(params)"},
                "treeData": True,
            },
            rowData=rowData,
            enableEnterpriseModules=True
        ),
    ]
)

app.layout = html.Div(
    [
        dcc.Markdown("Example: Organisational Hierarchy using Tree Data "),
        grid,
    ])


if __name__ == "__main__":
    app.run(debug=True)

Please tell me how I can withdraw the amount to orgHierarchy
Like this


Thank you for paying attention to why topic

Hello @zaphire121,

Check out here:

Looks like you would just pass an aggFunc to the columnDef:

import dash_ag_grid as dag
from dash import Dash, html, dcc
import pandas as pd

app = Dash(__name__)

data = {
    'org': ['org1', 'org2', 'org1'],
    'dep': ['dep1', 'dep2', 'dep3'],
    'services_success': [10, 20, 30],
}

df = pd.DataFrame(data)


rowData = []

for index, row in df.iterrows():
    rowData.append({
        "orgHierarchy": [row['org'], row['dep']],
        "services_success": row['services_success'],
    },
    )


grid = html.Div(
    [
        dag.AgGrid(
            id="tree-data-example",
            columnDefs=[
                {"field": "services_success", 'aggFunc': 'sum'},
            ],
            defaultColDef={
                "flex": 1,
            },
            dashGridOptions={
                "autoGroupColumnDef": {
                    "headerName": "Organisation",
                    "minWidth": 300,
                },
                "groupDefaultExpanded": -1,
                "getDataPath": {"function": "getDataPath(params)"},
                "treeData": True,
            },
            rowData=rowData,
            enableEnterpriseModules=True
        ),
    ]
)

app.layout = html.Div(
    [
        dcc.Markdown("Example: Organisational Hierarchy using Tree Data "),
        grid,
    ])


if __name__ == "__main__":
    app.run(debug=True)
2 Likes

thx, Bryan

1 Like

Hi, Bryan
Why use aggregation - custom functions in Python? I can’t figure out how to do this in Python code

Thx for your advice

To use custom functions in your grid, they need to be JS functions that you pass to the grid.

Check out here:

2 Likes