Customize name of exported data (Feature Request)
When adding an Export button (as per the final example here: https://dash.plot.ly/datatable/editable), the file is downloaded with a set name that cannot easily be changed. I think the filename is hard-coded in src/dash-table/components/Export/utils.tsx (exportWorkbook function copied in below).
It would be useful to be able to set the filename to something more descriptive. For example, dash_table.DataTable could have an extra parameter export_filename in addition to export_columns, export_format etc.
The function where the name is currently hard-coded:
export async function exportWorkbook (wb: WorkBook, format: string) { const XLSX = await LazyLoader.xlsx;
if (format === 'xlsx') {
XLSX.writeFile(wb, 'Data.xlsx', { bookType: 'xlsx', type: 'buffer' });
} else if (format === 'csv') {
XLSX.writeFile(wb, 'Data.csv', { bookType: 'csv', type: 'buffer' });
}
}
Please, please, please give us the ability to change the exported documents name!
+1
If I was a bit more capable I'd try change it myself, but my javascript is scrappy at best, and my development experience is minimal.
In an attempt to be productive; I'd image the simplest way to add this funcitonality is to do something like wrapping the aforementioned code in another if-block which references an added 'export_name' string parameter in the original Dash datatable...
**if (export_name !== None) {**
if (format === 'xlsx') {
XLSX.writeFile(wb, '**export_name**.xlsx', { bookType: 'xlsx', type: 'buffer' });
} else if (format === 'csv') {
XLSX.writeFile(wb, '**export_name**.csv', { bookType: 'csv', type: 'buffer' });
}
**} else {**
if (format === 'xlsx') {
XLSX.writeFile(wb, 'Data.xlsx', { bookType: 'xlsx', type: 'buffer' });
} else if (format === 'csv') {
XLSX.writeFile(wb, 'Data.csv', { bookType: 'csv', type: 'buffer' });
}
**}**
Needed
Agreed, would like to see this feature implemented!
also, agree! please please add this feature
You could use pandas as pd.DataFrame.from_records(data from dash-table).to_csv('name.csv',index=False) to get the same result with a custom name
@mayhem-am Could you please elaborate on that a little - how would you incorporate that approach into a button in a dash app?
@mayhem-am Could you please elaborate on that a little - how would you incorporate that approach into a button in a dash app?
I used a button and a text input field to enter a custom filename dcc.Input( id="input_filename", type="text", placeholder="File name", ), html.Button('Done', id='done', n_clicks=0)
registered through callbacks
[Input('done','n_clicks')],[State('table','data'),State('input_filename','value')]
def updateout(_,tab_data,filename)
The button upon click validates the table data and saves it using import pandas as pd
pd.DataFrame.from_records(tab_data).to_csv(filename+'.csv',index=False)
@mayhem-am Could you please elaborate on that a little - how would you incorporate that approach into a button in a dash app?
You could check out the image annotator I built which addresses the feature request. Link to image annotation
Still not possible to change the filename ?
Yes this is definitely needed.
dcc.Download allows to rename exported datatable
app.layout = html.Div([
dash_table.DataTable(id="output-table"),
dcc.Input( id="input_filename", type="text", placeholder="File name", ),
html.Button("Download Excel", id="btn_xlsx"),
dcc.Download(id="download-dataframe-xlsx"),
])
...
@app.callback(
Output("download-dataframe-xlsx", "data"),
[ State("output-table","data"),
State("input_filename","value")],
Input("btn_xlsx", "n_clicks"),
prevent_initial_call=True,
)
def func(data_json, file_name, n_clicks):
df = pd.DataFrame.from_records(data_json)
return dcc.send_data_frame(df.to_excel, file_name + ".xlsx", sheet_name="Sheet_name_1")