Export Data to CSV

The grid data can be exported to CSV with an API call, or using the right-click context menu (Enterprise only) on the Grid.

What Gets Exported

The same data that is in the grid gets exported, but none of the GUI representation of the data will be. What this means is:

The CSV export will be enabled by default. If you want to disable it, you can set the property suppressCsvExport = True in your gridOptions.

Basic Example

The example below shows the default behaviour when exporting the grid’s data to CSV.

import dash_ag_grid as dag
from dash import Dash, Input, Output, html, dcc, callback
from plotly import data

app = Dash(__name__)

df = data.election()

columnDefs = [
    {"field": "district"},
    {"field": "total"},
    {"field": "winner"},
    {"field": "result"},
]

app.layout = html.Div(
    [
        dcc.Markdown(
            "Click on the download button below to enable the export data to CSV feature of AG Grid."
        ),
        html.Button("Download CSV", id="csv-button", n_clicks=0),
        dag.AgGrid(
            id="export-data-grid",
            columnSize="sizeToFit",
            columnDefs=columnDefs,
            rowData=df.to_dict("records"),
            csvExportParams={
                "fileName": "ag_grid_test.csv",
            },
        ),
    ]
)


@callback(
    Output("export-data-grid", "exportDataAsCsv"),
    Input("csv-button", "n_clicks"),
)
def export_data_as_csv(n_clicks):
    if n_clicks:
        return True
    return False


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

Click on the download button below to enable the export data to CSV feature of AG Grid.

Additional Options

See the reference section below and the AG Grid docs for more details and examples of the options you can set for the CSV export.

This example shows how to exclude the headings on export and include hidden columns on export.

import dash_ag_grid as dag
from dash import Dash, Input, Output, html, dcc, ctx, callback

app = Dash(__name__)

columnDefs = [
    {"headerName": "Make", "field": "make"},
    {"headerName": "Model", "field": "model"},
    {"headerName": "Price", "field": "price"},
    {"headerName": "Sale Price", "field": "sale", "hide": True},
]

rowData = [
    {"make": "Toyota", "model": "Celica", "price": 35000, "sale": 33000},
    {"make": "Ford", "model": "Mondeo", "price": 32000, "sale": 29000},
    {"make": "Porsche", "model": "Boxster", "price": 72000, "sale": 69000},
]

app.layout = html.Div(
    [
        dcc.Markdown(
            "Click on the download button below to enable the export data to CSV feature of AG Grid."
        ),
        html.Button("Download CSV - no headings", id="no-headings", n_clicks=0),
        html.Button("Download CSV - include hidden", id="all-cols", n_clicks=0),
        dag.AgGrid(
            id="export-grid-options",
            columnSize="sizeToFit",
            columnDefs=columnDefs,
            rowData=rowData,
        ),
    ]
)


@callback(
    Output("export-grid-options", "exportDataAsCsv"),
    Output("export-grid-options", "csvExportParams"),
    Input("no-headings", "n_clicks"),
    Input("all-cols", "n_clicks"),
    prevent_initial_call=True,
)
def export_data_as_csv(*_):
    if ctx.triggered_id == "no-headings":
        return True, {
            "fileName": "ag_grid_no_headings_test.csv",
            "skipColumnHeaders": True,
        }
    return True, {"fileName": "ag_grid_all_cols_test.csv", "allColumns": True}


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

Click on the download button below to enable the export data to CSV feature of AG Grid.

Reference

csvExportParams (dict; optional): Object with properties to pass to the exportDataAsCsv() method. csvExportParams is a dict with keys: