Reputation: 141
Can someone help me with downloading Kaggle dataset on my Colab Notebook? I am new to Colab.
Upvotes: 0
Views: 851
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
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
Reputation: 171
You can use the following method to download any kaggle dataset to your Colab notebook.
kaggle.json
will be downloaded to your computer.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.
Install the kaggle API using !pip install -q kaggle
and move kaggle.json
file to ~/.kaggle
!mkdir -p ~/.kaggle
!cp kaggle.json ~/.kaggle/
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