# Get Dataframe as a csv file

**URL:** <https://community.plotly.com/t/get-dataframe-as-a-csv-file/25176>\
**Category:** Dash Python\
**Created:** [June 28, 2019, 11:16am UTC](https://community.plotly.com/t/get-dataframe-as-a-csv-file/25176 "2019-06-28T11:16:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![keend](https://avatars.discourse-cdn.com/v4/letter/k/f04885/32.png) [@keend](https://community.plotly.com/u/keend)\
**Post date:** [June 28, 2019, 11:16am UTC](https://community.plotly.com/t/get-dataframe-as-a-csv-file/25176/1 "2019-06-28T11:16:50Z")

</div>

Hi,

I’m trying to download a dataframe as a csv, where I try to do as discussed in this topic; [Allowing users to download CSV on click - #7 by kfyao](https://community.plotly.com/t/allowing-users-to-download-csv-on-click/5550/7)

My problem is, that it writes all the data in the first row and is not recognizing any line breakers.

I send the dataframe as csv to the href link

```
@app.callback(Output('my-link', 'href'),
              [Input('download-button', 'n_clicks')])
def update_link(clicks):
global df
return '/dash/urlToDownload?value={}'.format(df.to_csv(index=False, sep=',',
                                                                             encoding='utf-8'))

```

My route looks like this;

```
@app.server.route('/dash/urlToDownload')
def download_csv():
    value = flask.request.args.get('value')
    # create a dynamic csv or file here using `StringIO`
    # (instead of writing to the file system)
    strIO = io.StringIO()
    strIO.write(value)

    writer = csv.writer(strIO, delimiter=',', quotechar='|')
    writer.writerow(value)

    mem = io.BytesIO()
    mem.write(strIO.getvalue().encode('utf-8'))

    mem.seek(0)
    strIO.close()
    return flask.send_file(mem,
                     mimetype='text/csv',
                     attachment_filename='downloadFile.csv',
                     as_attachment=True)

```

An example of what value is in this case;

> version,id,value1,2,3

where the expected csv should look like this;

```
 version id value
 1 2 3

```

---

<div class="post-metadata">

**Author:** ![Abhinav](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@Abhinav](https://community.plotly.com/u/Abhinav)\
**Post date:** [July 10, 2020, 9:40am UTC](https://community.plotly.com/t/get-dataframe-as-a-csv-file/25176/2 "2020-07-10T09:40:37Z")

</div>

> [@keend](#):
>
> ```auto
> .format(df.to_csv(index=False, sep=',',
> encoding='utf-8')
> 
> ```

@keend are you able to resolve this issue? if yes, could you please you share your approach

---

<div class="post-metadata">

**Author:** ![Emil](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/emil/32/31734_2.png) [@Emil](https://community.plotly.com/u/Emil)\
**Post date:** [July 10, 2020, 9:48am UTC](https://community.plotly.com/t/get-dataframe-as-a-csv-file/25176/3 "2020-07-10T09:48:39Z")

</div>

You could try out my `Download` component, it makes downloads (also csv) simpler,

> [@Show and Tell - Download Component](https://community.plotly.com/t/show-and-tell-download-component/41502):
>
> Facilitated by the need to enable download of in-memory data, i wrote a Download component some time ago. I have recently updated the code and added a few examples to the documentation. As there has been a number of question on the forum related to downloading data, files, and pandas data frame, i figured it might be of interest to others. As of version 0.0.18, the basic syntax is like this, import dash import dash\_html\_components as html from dash.dependencies import Output, Input from dash\_e…
