How to download Kaggle datasets directly into Colab

Samuel A Donkor
CSS Knust
Published in
2 min readAug 13, 2020

If you’re familiar with kaggle and Colab, then you’ve probably realized that it’s one thing to download datasets from kaggle on your local machine, but quite another to import and use the datasets on Colab.

We’ll look at simple steps of downloading datasets from kaggle directly into Colab. This tutorial will make use of python and the kaggle API.

Installation

Run the following command in Colab to access the kaggle API:

!pip install kaggle

API credentials

To create an API key in kaggle:

  1. Click on the avatar icon at the top right corner of the kaggle page
  2. Select and click ‘My Account’
  3. Scroll down to the API access section and click generate to download an API key.

This will download a file called kaggle.json to your computer. We’ll use this file later on in Colab.

4. Switch back to Colab and type the following code:

from google.colab import filesfiles.upload()

Click on ‘Choose Files’ to upload the kaggle.json file.

5. Move the kaggle.json file into the folder where the API expects to find it.

!mkdir -p ~/.kaggle                                               !cp kaggle.json ~/.kaggle/
!chmod 600 /root/.kaggle/kaggle.json

Commands

Now you can access datasets using the client.

To list datasets:

!kaggle datasets list

To search for specific datasets, add the -s tag and then the keyword you’re interested in.

!kaggle datasets list -s dogs

To download a dataset, use the kaggle datasets download command.

!kaggle datasets download dansbecker/dogs-gone-sideways

You could also get the command at the dataset page

Now that’s it. You have now fully made the connection between kaggle and colab work. Hope this makes your life a little much easier.

Implementation of the code on Github.

--

--

Samuel A Donkor
CSS Knust

AI4Medicine | Astrophysicist | Astrobiologist | Thoughts, opinions and things I’ve learned.... https://sites.google.com/view/samadon