Notebook Integration: Colab and Kaggle

Notebook Integration: Colab and Kaggle

Seamlessly move from UI to Code. Learn how to export your AI Studio work directly to Google Colab or Kaggle notebooks for reproducible research.

Notebook Integration

The web UI is great for testing 1 prompt. But when you want to run that prompt on 1,000 rows of data, you need code. Google AI Studio integrates natively with Google Colab.

The "Open in Colab" Workflow

  1. Refine Prompt: tweak your prompt in AI Studio until it is perfect.
  2. Export: Click the < > (Get Code) button.
  3. Action: Instead of "Copy", click "Open in Colab".

What Happens?

A new Colab notebook opens in a new tab. It is pre-filled with:

  1. Installation: !pip install -q -U google-generativeai
  2. Secret Management: Code to securely retrieve your API key (using Colab's "Secrets" manager, not hardcoded text).
  3. Configuration: The exact generation config (temperature, safety) you set in the UI.
  4. Execution: The code to run your prompt.

Why use Notebooks?

  • Iterate on Logic: You can add Python logic to loop through a dataset and call the Gemini function for each row.
  • Visualization: Use matplotlib to graph the sentiments returned by Gemini.
  • Sharing: Share the notebook with teammates. They can run it with their API keys.

Quick Tip: Colab Secrets

Never paste your API key into a code cell.

  1. In Colab, click the "Key" icon on the left (Secrets).
  2. Name: GEMINI_API_KEY.
  3. Value: (Your key).
  4. Access in code:
    from google.colab import userdata
    api_key = userdata.get('GEMINI_API_KEY')
    

Summary

The "Open in Colab" button is the fastest way to turn a prototype into a script. It handles the boilerplate so you can focus on the logic.

In the next lesson, we cover Model Registry and Versioning.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn