DEV Community

Victor Maina
Victor Maina

Posted on

Kaggle datasets magic

Using the Kaggle API key simplified.

Step 1
Install the Kaggle Library

Install kaggle library

Step 2 Mount the google drive

Image description

Step 2.1 Allow access to your google drive in the pop-up that will show after running this code
Image description

Step 3: Ensure you have a Kaggle account it is free of charge
These are the steps
To create a Kaggle account, follow these steps:

  1. Visit Kaggle's Website: Open your web browser and go to the Kaggle website at https://www.kaggle.com/

  2. Sign Up: On the Kaggle homepage, you will see a "Sign Up" button on the top right corner. Click on it.

  3. Sign Up with Google, Facebook, or Email: You have three options to create your account:

    • Sign Up with Google: If you have a Google account, you can use it to sign up quickly. Click on the "Sign Up with Google" button, and follow the prompts to authenticate with your Google account.
    • Sign Up with Facebook: If you prefer to use your Facebook account, click on the "Sign Up with Facebook" button, and follow the prompts to authenticate with your Facebook credentials.
    • Sign Up with Email: If you want to sign up using your email address, click on the "Sign Up with Email" option. Enter your email address, create a password, and click the "Create Account" button.
  4. Agree to Terms of Service and Privacy Policy: After signing up, you might be asked to agree to Kaggle's Terms of Service and Privacy Policy. Read the terms, and if you agree, click the appropriate checkboxes and proceed.

  5. Complete Your Profile (Optional): Once you've created your account, you can optionally complete your profile by providing additional information, such as your full name, profile picture, and a short bio. Completing your profile helps other Kaggle users learn more about you.

  6. Verify Your Email (Optional): If you signed up using an email address, Kaggle might send you an email verification link. Click the link to verify your email address. This step is optional, but verifying your email can help you receive important notifications and updates from Kaggle.

  7. Start Using Kaggle: With your account created and verified (if applicable), you can now start using Kaggle to explore datasets, participate in competitions, collaborate with others, and access various machine learning resources.

Step 4: In your Google Colab notebook, make a directory for Kaggle at the temporary instance location on Colab Drive.
Now that you have created a Kaggle account
Download your Kaggle API key (.json file). You can do this by going to your Kaggle account page and clicking 'Create new API token' under the API section.
*These are the steps to create an API key very simple *
In the top right corner, you will see your profile icon

Image description

*Next, click on settings as such *

Image description

On this section written API, click create a new API token, a new Kaggle.json file will be downloaded on your local computer

Check your downloads for a kaggle.json file to verify.

Make a directory to copy your JSON file in your Colab notebook by running this line
Image description

The directory name .kaggle is prefixed with a dot (.) which makes it a hidden directory in Unix-based systems. Hidden directories are not normally shown when listing the contents of a directory unless the appropriate flag is used. This naming convention is often used for configuration files and data not meant to be directly accessed or manipulated by users.
The command is commonly used in Kaggle, a platform for data science and machine learning enthusiasts. In the Kaggle context, this command might create a hidden .kaggle directory to store Kaggle API credentials or configuration files required for interacting with the Kaggle API from the command line.
By creating this hidden directory, users can store sensitive information or configuration files related to Kaggle without cluttering their home directory or exposing these files to unintended access.

Next to Navigate to the Drive Directory: After mounting Google Drive, you can navigate to the root directory of your Google Drive by running the following code cell:

! mkdir ~/.kaggle
Enter fullscreen mode Exit fullscreen mode

Next create a folder where you will store your Kaggle.json file, I named mine Kaggle_ API_credentials

!mkdir "Kaggle_API_Credentials"
Enter fullscreen mode Exit fullscreen mode

Confirm to see if the folder has been successfully created on the left side inside your drive, we can clearly see it has been created successfully.

Next: let us upload the downloaded Kaggle.json file to our Google Drive as such.

Image description

Next: copy link to the json file from our google drive as such

Image description

Click on the three dots next to kaggle.json and copy the path
Image description

Next
FINALLY! Upload the JSON file to Google Drive and copy it to the temporary location.

!cp /content/drive/MyDrive/Kaggle_API_Credentials/kaggle.json ~/.kaggle/kaggle.json
Enter fullscreen mode Exit fullscreen mode

We can now gladly download any data from Kaggle that we have configured our notebook appropriately and followed these instructions to the letter.

import os
os.environ['KAGGLE_CONFIG_DIR']='/content/drive/MyDrive/Kaggle_API_Credentials/'
Enter fullscreen mode Exit fullscreen mode

Image description

Next: we need to unzip the files in that we have downloaded as the come as zipped files
as such

!unzip \*.zip && rm *.zip
Enter fullscreen mode Exit fullscreen mode

Image description

Happy coding!

Top comments (0)