{"cells":[{"metadata":{"trusted":true,"_uuid":"2426a7185273d1680318ee05ffc5bf0994beec00","collapsed":true},"cell_type":"markdown","source":"Someone asked [on the forums](https://www.kaggle.com/product-feedback/61487#post359375) whether it was possible to download a file directly from the kernel without having to commit it. I did some poking around and found a nifty example [Polong Lin](http://www.polonglin.com) that allows you to do just that by creating a download link for a data file. \n\nThe download link will work in both the editor and kernel viewer. ","execution_count":null},{"metadata":{"trusted":true,"_uuid":"2e5aa08616661bc9d73b87907db1c1c6f4ad6cf4"},"cell_type":"code","source":"# import the modules we'll need\nfrom IPython.display import HTML\nimport pandas as pd\nimport numpy as np\nimport base64\n\n# function that takes in a dataframe and creates a text link to  \n# download it (will only work for files < 2MB or so)\ndef create_download_link(df, title = \"Download CSV file\", filename = \"data.csv\"):  \n    csv = df.to_csv()\n    b64 = base64.b64encode(csv.encode())\n    payload = b64.decode()\n    html = '<a download=\"{filename}\" href=\"data:text/csv;base64,{payload}\" target=\"_blank\">{title}</a>'\n    html = html.format(payload=payload,title=title,filename=filename)\n    return HTML(html)\n\n# create a random sample dataframe\ndf = pd.DataFrame(np.random.randn(50, 4), columns=list('ABCD'))\n\n# create a link to download the dataframe\ncreate_download_link(df)\n\n# ↓ ↓ ↓  Yay, download link! ↓ ↓ ↓ ","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}