Skip to content

Instantly share code, notes, and snippets.

@HaiyangXu
Created November 18, 2017 13:18
Show Gist options
  • Save HaiyangXu/244ae64d93c9142197791014f2a92503 to your computer and use it in GitHub Desktop.
Save HaiyangXu/244ae64d93c9142197791014f2a92503 to your computer and use it in GitHub Desktop.
Download CSV file from data frame in jupyter
from IPython.display import HTML
import base64
import pandas as pd
def create_download_link( df, title = "Download CSV file", filename = "data.csv"):
csv = df.to_csv(index =False)
b64 = base64.b64encode(csv.encode())
payload = b64.decode()
html = '<a download="{filename}" href="data:text/csv;base64,{payload}" target="_blank">{title}</a>'
html = html.format(payload=payload,title=title,filename=filename)
return HTML(html)
create_download_link(df)
@stevesimmons
Copy link

Note in Chrome this is only for smaller dataframes. Chrome's max data URI size is 2MB. In this case, this is after Base64 encoding.

@marcosalbaregidor
Copy link

Hi,

I am trying to use this code to download dataframes bigger than 2MB. Is there any way of doing this?

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment