This notebook provides a recipe for using Kaggle API with Colab


In [73]:
!pip install kaggle


Requirement already satisfied: kaggle in /usr/local/lib/python3.6/dist-packages
Requirement already satisfied: urllib3>=1.15 in /usr/local/lib/python3.6/dist-packages (from kaggle)
Requirement already satisfied: certifi in /usr/local/lib/python3.6/dist-packages (from kaggle)
Requirement already satisfied: six>=1.10 in /usr/local/lib/python3.6/dist-packages (from kaggle)
Requirement already satisfied: python-dateutil in /usr/local/lib/python3.6/dist-packages (from kaggle)

API Credentials

To use the Kaggle API, sign up for a Kaggle account at https://www.kaggle.com. Then go to the 'Account' tab of your user profile (https://www.kaggle.com//account) and select 'Create API Token'. This will trigger the download of kaggle.json, a file containing your API credentials.

Place this file on your Google Drive anywhere.

With the next snippet you download your credentials to Colab and you can start using Kaggle API


In [72]:
from googleapiclient.discovery import build
import io, os
from googleapiclient.http import MediaIoBaseDownload
from google.colab import auth

auth.authenticate_user()

drive_service = build('drive', 'v3')
results = drive_service.files().list(
        q="name = 'kaggle.json'", fields="files(id)").execute()
kaggle_api_key = results.get('files', [])

filename = "/content/.kaggle/kaggle.json"
os.makedirs(os.path.dirname(filename), exist_ok=True)

request = drive_service.files().get_media(fileId=kaggle_api_key[0]['id'])
fh = io.FileIO(filename, 'wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
    status, done = downloader.next_chunk()
    print("Download %d%%." % int(status.progress() * 100))
os.chmod(filename, 600)


Download 100%.

In [71]:
!kaggle competitions list


ref                                             deadline             category            reward  teamCount  userHasEntered  
----------------------------------------------  -------------------  ---------------  ---------  ---------  --------------  
imagenet-object-detection-challenge             2029-12-31 07:00:00  Research         Knowledge          0           False  
imagenet-object-detection-from-video-challenge  2029-12-31 07:00:00  Research         Knowledge          0           False  
imagenet-object-localization-challenge          2029-12-31 07:00:00  Research         Knowledge          2           False  
titanic                                         2020-04-07 00:00:00  Getting Started  Knowledge      10257            True  
house-prices-advanced-regression-techniques     2020-03-01 23:59:00  Getting Started  Knowledge       4512           False  
digit-recognizer                                2020-01-07 00:00:00  Getting Started  Knowledge       1891           False  
competitive-data-science-predict-future-sales   2019-01-01 23:59:00  Playground           Kudos        126           False  
whale-categorization-playground                 2018-07-09 23:59:00  Playground           Kudos         91           False  
inaturalist-2018                                2018-06-04 23:59:00  Research             Kudos         14           False  
imaterialist-challenge-furniture-2018           2018-05-30 23:59:00  Research            $2,500         27           False  
landmark-retrieval-challenge                    2018-05-22 23:59:00  Research            $2,500         69           False  
landmark-recognition-challenge                  2018-05-22 23:59:00  Research            $2,500        127           False  
talkingdata-adtracking-fraud-detection          2018-05-07 23:59:00  Featured           $25,000        648           False  
donorschoose-application-screening              2018-04-25 23:59:00  Playground            Swag        163           False  
data-science-bowl-2018                          2018-04-16 23:59:00  Featured          $100,000       2400           False  
mens-machine-learning-competition-2018          2018-04-02 23:59:00  Featured           $50,000        232           False  
womens-machine-learning-competition-2018        2018-04-01 23:59:00  Featured           $50,000        398           False  
jigsaw-toxic-comment-classification-challenge   2018-03-20 23:59:00  Featured           $35,000       4286           False  
plant-seedlings-classification                  2018-03-12 23:59:00  Playground           Kudos        836           False  
dog-breed-identification                        2018-02-26 23:59:00  Playground           Kudos       1286           False