Code to download CSV from Kaggle notebook (without commit run)

I wanted to post this to keep it handy for anyone who is hand tuning their nn on Kaggle and needs to download a CSV from it.
Apparently Kaggle will create a download button if you do the “commit” and run to completion, but since I’m hand tuning the various CNN’s, I didn’t want to restart and wait another four hours just to re-run and download the results and submit for the competition.

I found this code from Kaggle, and made one adjustment - at least for the digits competition, you have to create the CSV with index=False. Otherwise you’ll get a bunch of obtuse errors about column 1 when you submit. (This isn’t my code, someone else wrote it and I just fixed the index=False issue).

from IPython.display import HTML
import pandas as pd
import numpy as np
import base64

# function that takes in a dataframe and creates a text link to  
# download it (will only work for files < 2MB or so)
def create_download_link(df, title = "Download CSV file", filename = "cnn15_submission-122-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 a random sample dataframe
#df = pd.DataFrame(np.random.randn(50, 4), columns=list('ABCD'))

# create a link to download the dataframe
create_download_link(submission)

# ↓ ↓ ↓  Yay, download link! ↓ ↓ ↓ 

#--------------------------------------------------

Anyway, I would have been stuck without this, so hope it is helpful!