allow send_data_frame to send df.to_csv using send_bytes
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 24.4k
- Forks
- 2.3k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 13
Description
pandas df.to_csv supports encodings other than utf-8 in versions > 1.2 but only if the path_or_buf is a binary file type so if I want to do dcc.send_data_frame(df.to_csv, "mydf.csv", encoding='utf-8-sig'), the encoding will still be utf-8 unless I modify this line with this monkey patch so the file is sent as bytes:
dcc.express._data_frame_senders['to_csv'] = dcc.express.send_bytes
It would be great if there was an option so that send_data_frame used bytes if pandas > 1.2 and encoding was one of the kwargs.
Simple example to test:
import pandas as pd
# uncomment this line so that the specified encoding works
# dcc.express._data_frame_senders['to_csv'] = dcc.express.send_bytes
app = Dash(__name__)
app.layout = html.Div(
[
html.Button("Download CSV", id="btn_csv"),
dcc.Download(id="download-dataframe-csv"),
]
)
df = pd.DataFrame({"a": ['á', 'é', 'å', 'ñ'], "b": [2, 1, 5, 6], "c": ["x", "x", "y", "y"]})
@app.callback(
Output("download-dataframe-csv", "data"),
Input("btn_csv", "n_clicks"),
prevent_initial_call=True,
)
def func(n_clicks):
df.to_csv("code.csv") # the output file is 43 bytes using utf-8
return dcc.send_data_frame(df.to_csv, "mydf.csv", encoding='utf-8-sig') # the output file is 46 bytes using utf-8-sig
if __name__ == "__main__":
app.run_server(debug=True)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at components/dash-core-components/dash_core_components_base/express.py around the linked line and inspect how send_data_frame selects the sender for to_csv. Reproduce the example with pandas and encoding='utf-8-sig', then verify that the downloaded CSV preserves the requested encoding while existing send_data_frame behavior remains unchanged.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- pandas, python
- Domain
- api, data
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100