How to download files from the challenges, so you can play around with them on your own

If you, like me, find the window that the challenges load in difficult to work with, you’re probably wondering if there’s a way to work with the files without having the challenge disappear from view as the page scrolls.

Well, good news: You can simply copy the text over to a free Google Colab document, which is just a different platform for Jupyter notebooks! All the code will run in the exact same way, and you can keep all of your work from throughout the challenge in one file.

The only wrinkle is that you’ll need the files provided for the challenges when you start to work with larger data sets. To do this, simply enter the following code in a cell on the challenge page:

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

def create_download_link( df, title = "Download CSV file", filename = "EDIT_THIS_FILENAME.csv"):
    csv = df.to_csv()
    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)

download = pd.read_csv('EDIT_THIS_FILENAME.csv')
create_download_link(download)

Replace EDIT_THIS_FILENAME in the above text, then run the cell, and it will create a button that you can use to download the provided file.

Next, you’ll need to import it into your Colab document. To do this, enter the following code in a new cell inside your Colab document:

from google.colab import files
uploaded = files.upload()

Running the code in the cell will create a file upload button. Click it, and select the .csv file that you downloaded from the challenge, then wait for it to finish uploading.

Finally, you’ll need to have your code access the file you just uploaded. To do this, put the following at the top of the code that you’re writing for the challenge:

import pandas as pd
import io
 
df = pd.read_csv(io.BytesIO(uploaded['EDIT_THIS_FILENAME.csv']))

Replace EDIT_THIS_FILENAME in the above text with the name of the file you uploaded, then run the cell, and it will read the .csv file into your Colab file, so that it can be used in the code that you’re writing.

And that’s it. You’ll be able to manipulate the file as much or as little as you want, in your own Colab file, free from accidental scrolling headaches.

Hope this helps someone!

8 Likes

Amazing! How did you figure this out? This is SUCH a help, thanks!!

2 Likes

No problem! To be honest, I just used Google, haha.

I searched for ‘how to export .csv files from jupyter notebooks’, and found the code to create a download link. Same for ‘how to import .csv files into Colab’.

Like a lot of programming challenges, I wasn’t the first one to run into the problem, haha! So I just re-used the solutions that others had created, with minor modifications.

2 Likes

I now have a github repo that contains the csv files for the challenges! I thought I should post it here so that anyone who sees your post also has the option of downloading from the repo if they find that easier.

I and others are adding any new csv files that come with the challenges. It has all the csv files up to today’s challenge (challenge 14) and will include any future csv files that come with future challenges.

For anyone interested: To download, first click on Code then click on Download ZIP:

2 Likes

Update: Downloading Files That Are Not CSV

Today’s challenge (#15) requires that we use a .db file for SQL, as opposed to a .csv file.
I wasn’t able to figure out how to change the method in your original post so that it can be used for .db files (instead of just .csv files), but I found the following snippet of code (from here) that works for .db files (and, I assume, all files). (I tested it and it worked! :D)

from IPython.display import FileLink
FileLink("FILENAME.extension")

For challenge 15, I replaced "FILENAME.extension" with "himalayas.db".

2 Likes

Easy…thanks so much!

2 Likes