dash-table icon indicating copy to clipboard operation
dash-table copied to clipboard

Customize name of exported data (Feature Request)

Open johnowhitaker opened this issue 6 years ago • 11 comments

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' });
}

}

johnowhitaker avatar Nov 05 '19 10:11 johnowhitaker

Please, please, please give us the ability to change the exported documents name!

Phillyclause89 avatar Dec 10 '19 00:12 Phillyclause89

+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' });
}

**}**

yortug avatar Feb 14 '20 02:02 yortug

Needed

sorenwacker avatar Apr 24 '20 18:04 sorenwacker

Agreed, would like to see this feature implemented!

Trahan2332 avatar Jun 25 '20 22:06 Trahan2332

also, agree! please please add this feature

DanielMS93 avatar Jun 29 '20 12:06 DanielMS93

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

akoustic avatar Nov 07 '20 16:11 akoustic

@mayhem-am Could you please elaborate on that a little - how would you incorporate that approach into a button in a dash app?

zherm-riffyn avatar Nov 10 '20 13:11 zherm-riffyn

@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)

akoustic avatar Nov 13 '20 07:11 akoustic

@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

akoustic avatar Nov 13 '20 07:11 akoustic

Still not possible to change the filename ?

sorenwacker avatar Jun 24 '22 18:06 sorenwacker

Yes this is definitely needed.

grawfin avatar Nov 01 '22 14:11 grawfin

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")

vjcolab avatar Feb 25 '23 22:02 vjcolab