plotly / dash

Data Apps & Dashboards for Python. No JavaScript Required.

Home Page:https://plotly.com/dash

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Feature Request] Render table can add Grand Total

trantrinhquocviet opened this issue · comments

Thanks so much for your interest in Dash!

Before posting an issue here, please check the Dash community forum to see if the topic has already been discussed. The community forum is also great for implementation questions. When in doubt, please feel free to just post the issue here :)

Is your feature request related to a problem? Please describe.
Hey, i have ideal in visualized table, how to add grand total. It's a footer such as the column header. But it's calculate by column number in table.

Describe the solution you'd like
I encourage encountered when make a table very long and have pagination. So, how about add html or add a row grand total below table with pagination.

Describe alternatives you've considered
It's helpful for me.

image

Hi @trantrinhquocviet

This is possible using pinned rows. Try running the example below:

See more examples in the docs: https://dash.plotly.com/dash-ag-grid/row-pinning
If you need more help, please feel free to post question on the Forum: https://community.plotly.com/

import dash_ag_grid as dag
from dash import Dash, html, Input, Output, Patch, callback
import pandas as pd

app = Dash(__name__)


df = pd.read_csv(
    "https://raw.githubusercontent.com/plotly/datasets/master/ag-grid/olympic-winners.csv"
)


app.layout = html.Div(
    [
        dag.AgGrid(
            id="row-pinning-bottom",
            rowData=df.to_dict("records"),
            columnDefs=[{"field": x} for x in ["country", "athlete", "year", "total"]],
            defaultColDef={"editable": True, "sortable": True, "resizable": True},
            columnSize="sizeToFit",
            dashGridOptions={"pagination": True},
        ),
    ],
)


@callback(
    Output("row-pinning-bottom", "dashGridOptions"),
    Input("row-pinning-bottom", "virtualRowData"),
)
def row_pinning_bottom(data):
    dff = df if data is None else pd.DataFrame(data)
    total = dff["total"].sum()
    pinned_data = [{"country": "Total", "total": total}]

    grid_option_patch = Patch()
    grid_option_patch["pinnedBottomRowData"] = pinned_data
    return grid_option_patch


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

image