Ag-grid custom aggregation function written in Python

Is there a way to write custom aggregation function in Python when using AG Grid?
I’m aware that we can write javascript functions for custom aggregation but I would like to do it on the server side with Python function. Is it possible? Thanks for your attention.

To give more context to my problem, I already have a dataframe with precalculated aggregate values and the aggregates are not straight forward (requires a lot of computational power). I just want to show the corresponding aggregates for each group using simple look up using group keys.

Hello @boscochw,

Welcome to the community!

I’m confused by this, if you are already combining the data in the df, shouldn’t you be able to just give the resulting data to the grid?

Could you explain it a little more or give an example?

Thanks for the reply! I’m using the rowGroup attribute in the colDef so the rows are collapsed if value is the same.

columnDefs = [
    {
        "headerName": "Portfolio Code",
        "field": "portfolio_code",
        "rowGroup": True,
        "hide": True,
    },
    {"headerName": "Risk Type", "field": "risk_type", "rowGroup": True, "hide": True},
    {"headerName": "Category", "field": "category", "rowGroup": True, "hide": True},
   *[
        {
            "headerName": format_header(col),
            "headerTooltip": format_header(col),
            "field": col,
            "aggFunc": <AGGFUNC>
            "valueFormatter": {
                "function": "params.value && d3.format('(.2f')(params.value)"
            },
        }
        for col in df.columns.tolist()
        if "bps" in col
    ],
]

I don’t have the function (AGGFUNC) to aggregate the values because the aggregation was done in another system but I have the aggregated values. But I can’t find a way to put those values into the cells.

Is there a possibility for a MRE?

Please see the MRE below.

import dash
import dash_ag_grid as dag
import pandas as pd

d = {'portfolio': [1, 1, 2, 2, 2, 3], 'val': [1, 2, 3, 4, 5, 6]}
df = pd.DataFrame(data=d)

totals = pd.DataFrame(data={"portfolio": [1, 2, 3], "val": ["0.123" ,"2.12", "0.1"]})


app = dash.Dash(
    __name__
)

app.layout = dag.AgGrid(
                         id=AG_GRID,
                         columnDefs=[
                            {"field": "portfolio", "rowGroup": True, "hide": True}
                            {"field": "val", "aggFunc": "I_DONT_HAVE_THE_FORMULA"}
                         ], 
                         rowData=df.to_dict('records')
                      )
   
if __name__ == '__main__':
    app.run_server(debug=True)

How do I put the total values into the grouped rows?

You could merge the df on the uid, seemingly the portfolio?

import dash
import dash_ag_grid as dag
import pandas as pd

d = {'portfolio': [1, 1, 2, 2, 2, 3], 'val': [1, 2, 3, 4, 5, 6]}
df = pd.DataFrame(data=d)

totals = pd.DataFrame(data={"portfolio": [1, 2, 3], "totals": ["0.123" ,"2.12", "0.1"]})

merged_df = df.merge(totals, on='portfolio')

app = dash.Dash(
    __name__
)

app.layout = dag.AgGrid(
                         id="AG_GRID",
                         columnDefs=[
                            {"field": "portfolio", "rowGroup": True, "hide": True},
                            {"field": "val", "valueGetter": {
                                "function": "params.data ? params.data.val : params.node.childrenAfterSort[0].data.totals"
                            }},
                         ],
                            enableEnterpriseModules=True,
                         rowData=merged_df.to_dict('records')
                      )

if __name__ == '__main__':
    app.run_server(debug=True)

This is setup to use the valueGetter to pull the data from the first child and pull the totals from there, since we merged it together.

image

1 Like

Thanks for the solution!

1 Like