Ankur De
Ankur De

Reputation: 141

How can I download a kaggle dataset on my Colab notebook?

Can someone help me with downloading Kaggle dataset on my Colab Notebook? I am new to Colab.

Upvotes: 0

Views: 851

Answers (3)

rnewd_user
rnewd_user

Reputation: 1662

its neccessary to set up kaggle for example

import json
import os
import shutil
import random
import cv2
import pandas as pd
from matplotlib import pyplot as plt


    KAGGLE_PATH = "/root/.kaggle"
    if os.path.exists(KAGGLE_PATH):
      !rm -r "$KAGGLE_PATH"
    
    !mkdir "$KAGGLE_PATH"
    !touch "$KAGGLE_PATH/kaggle.json"
    
    # https://www.kaggle.com/
    api_token = {"username":"your username","key":"your api key"}
    
    with open(KAGGLE_PATH+'/kaggle.json', 'w') as file:
        json.dump(api_token, file)
    
    !chmod 600 ~/.kaggle/kaggle.json

now you can download the dataset:

if not os.path.exists('./task'):

  os.makedirs('task')
else:
  !rm -rf task
  os.makedirs('task')

!kaggle datasets download -d user/explorer -p task
%pwd

!unzip -qn '/content/task/explorer.zip' -d /content/task > /dev/null
!rm /content/task/explorer.zip

Upvotes: 0

liamhp
liamhp

Reputation: 231

For future searchers: you can also just use

os.environ['KAGGLE_USERNAME'] = '<your-username>'
os.environ['KAGGLE_KEY'] = '<api-key>'

Run that with your information, then, if you're worried about exposing your API key, remove it

os.environ['KAGGLE_USERNAME'] = '<your-username>'
os.environ['KAGGLE_KEY'] = ''

Then you'll be able to download your dataset

!kaggle datasets download -d <kaggle-dataset>

EDIT:

You can also

from kaggle.api.kaggle_api_extended import KaggleApi
api = KaggleApi()
api.authenticate()
api.dataset_download_files('datasetlink', 'filename')

Upvotes: 0

Akash Dhar
Akash Dhar

Reputation: 171

You can use the following method to download any kaggle dataset to your Colab notebook.

  1. First, you have to create an API key in Kaggle.
  2. Go to kaggle.com and open your user settings page.
  3. From the API access section download an API key.
  4. A file called kaggle.json will be downloaded to your computer.
  5. Now open your Colab notebook and copy the following snippet to a cell.

    from google.colab import files

    files.upload()

    After executing this upload the kaggle.json file.

  6. Install the kaggle API using !pip install -q kaggle and move kaggle.json file to ~/.kaggle

    !mkdir -p ~/.kaggle

    !cp kaggle.json ~/.kaggle/

  7. Now you can download the dataset to your Colab notebook by copying the API command of the dataset that you want to download. For example:

    !kaggle competitions download -c titanic

The dataset should get downloaded to your notebook after this. Happy Coding.

Upvotes: 5

Related Questions