The Kaggle API is a powerful tool that allows users to interact with Kaggle services directly from the command line or programmatically through scripts and applications. It provides access to various functionalities such as downloading datasets, submitting competitions, managing notebooks, and more.
Key Features
- Access Datasets: Download existing datasets or upload your own.
- Competitions: Participate in Kaggle competitions by downloading data, making submissions, and tracking your progress.
- Notebooks: Manage Jupyter notebooks, including creating, running, and sharing them.
- Kernels: Run code on Kaggle’s servers, with access to powerful GPUs and other resources.
- User Information: Fetch details about user profiles, kernels, and datasets.
How to Set Up and Use Kaggle API
1. Install the Kaggle API
You can install the Kaggle API using pip:
pip install kaggle
2. Obtain API Credentials
- Go to your Kaggle account settings: Kaggle Account.
- Scroll down to the "API" section.
- Click on "Create New API Token". This will download a
kaggle.json file containing your API credentials.
3. Configure the API
Place the kaggle.json file in the appropriate directory:
- Linux/macOS:
~/.kaggle/kaggle.json
- Windows:
C:\Users\<your-username>\.kaggle\kaggle.json
Ensure the directory has the correct permissions:
chmod 600 ~/.kaggle/kaggle.json # Linux/macOS
Alternatively, you can set the environment variables directly:
export KAGGLE_USERNAME=your_kaggle_username
export KAGGLE_KEY=your_kaggle_key
Common Usage Examples
Downloading a Dataset
To download a dataset, you first need to know its name or ID. You can find this on the dataset page. For example, to download the Titanic dataset:
kaggle datasets download -d heptapod/titanic
This will download a zip file of the dataset into your current directory.
Listing Available Datasets
You can search for datasets with specific keywords:
kaggle datasets list -s "titanic"
Uploading a Dataset
First, create a dataset-metadata.json file describing your dataset:
{
"title": "My New Dataset",
"id": "username/my-new-dataset",
"licenses": [
{
"name": "CC0-1.0"
}
]
}
Then use the API to create the dataset:
kaggle datasets create -p /path/to/your/dataset/folder
Downloading Competition Data
To download data for a specific competition, you need the competition name or ID:
kaggle competitions download -c titanic
This will download the competition data into your current directory.
Submitting to a Competition
Store your predictions in a file (e.g., submission.csv), then submit it to the competition:
kaggle competitions submit -c titanic -f submission.csv -m "Initial submission"
Managing Kernels
To list all your kernels:
kaggle kernels list --user your_username
To pull a kernel:
kaggle kernels pull your_username/your_kernel_name
Advanced Usage
Running Kaggle API in Python Script
You can also use the Kaggle API in a Python script by importing the kaggle module. Here is an example:
import kaggle
# List datasets in a specific category
datasets = kaggle.api.dataset_list(search="titanic")
for dataset in datasets:
print(dataset.ref)
# Download a specific dataset
kaggle.api.dataset_download_files("heptapod/titanic", path="datasets/titanic", unzip=True)
# Download competition data
kaggle.api.competition_download_files("titanic", path="competitions/titanic", unzip=True)
Useful Links
Conclusion
The Kaggle API is a versatile tool that makes it easy to interact with Kaggle services programmatically. Whether you're downloading datasets, managing notebooks, or participating in competitions, the API provides a seamless and efficient way to integrate Kaggle into your workflow.