I have modified code taken from Dash AG-Grid: Export Data As Excel to both prepend text to and specify a filename.
While the app works, the filename is “export.csv” when it is expected to be “help.csv” and also no text (i.e., “Help” is expected) is prepended to the exported csv file.
What am I missing in the following line wrt filename and prependContent:
dag.AgGrid(
id="grid-excel-export",
rowData=df.to_dict("records"),
columnDefs=columnDefs,
defaultColDef={"resizable": True, "filter": True},
enableEnterpriseModules=True,
dashGridOptions={"rowSelection":"multiple",
"csvExportParams": {"filename": "help.csv", "prependContent": "Help"},
},),])
Below is a full minimal code (again, taken from the aforementioned posting) example:
import dash_ag_grid as dag
from dash import Dash, html, Input, Output, clientside_callback
import pandas as pd
import os
app = Dash(__name__)
df = pd.read_csv(
"https://raw.githubusercontent.com/plotly/datasets/master/ag-grid/olympic-winners.csv"
)
columnDefs = [
{
"headerName": "Athlete Details",
"children": [
{"field": "athlete", "width": 180},
{"field": "age", "width": 90},
{"field": "country", "width": 140},
],
},
{
"headerName": "Sports Results",
"children": [
{"field": "sport", "width": 140},
{"field": "total", "width": 100, "filter": "agNumberColumnFilter", "columnGroupShow": "closed"},
{"field": "gold", "width": 100, "filter": "agNumberColumnFilter", "columnGroupShow": "open"},
{"field": "silver", "width": 100, "filter": "agNumberColumnFilter", "columnGroupShow": "open"},
{"field": "bronze", "width": 100, "filter": "agNumberColumnFilter", "columnGroupShow": "open"},
],
},
]
app.layout = html.Div(
[
html.Button("Export to csv", id="btn-excel-export"),
dag.AgGrid(
id="grid-excel-export",
rowData=df.to_dict("records"),
columnDefs=columnDefs,
defaultColDef={"resizable": True, "filter": True},
enableEnterpriseModules=True,
dashGridOptions={"rowSelection":"multiple",
"csvExportParams": {"filename": "help.csv", "prependContent": "Help"},
},),])
clientside_callback(
"""function (n) {
if (n) {
dash_ag_grid.getApi("grid-excel-export").exportDataAsCsv();
}
return dash_clientside.no_update
}""",
Output("btn-excel-export", "n_clicks"),
Input("btn-excel-export", "n_clicks"),
prevent_initial_call=True
)
if __name__ == "__main__":
app.run(debug=True)