Created
November 18, 2017 13:18
-
-
Save HaiyangXu/244ae64d93c9142197791014f2a92503 to your computer and use it in GitHub Desktop.
Download CSV file from data frame in jupyter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
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.
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
From https://medium.com/ibm-data-science-experience/how-to-upload-download-files-to-from-notebook-in-my-local-machine-6a4e65a15767