Link Search Menu Expand Document

Jupyter Notebooks (.ipynb)

Table of contents

  1. What is a Jupyter Notebook?
  2. Key Features
  3. File Structure
  4. How Jupyter Notebooks Work
    1. Local Jupyter Environment
    2. VS Code Integration
  5. Using Jupyter Notebooks in Google Colab
    1. Key Benefits of Colab
    2. How to Use Colab
      1. 1. Access Google Colab
      2. 2. Create a New Notebook
      3. 3. Open from Google Drive
      4. 4. Work with Cells
      5. 5. Enable GPU/TPU
      6. 6. Install Additional Packages
      7. 7. Mount Google Drive
      8. 8. Save Your Work
    3. Colab vs. Local Jupyter
    4. Common Colab Tips
  6. Common Use Cases
  7. Troubleshooting
  8. Additional Resources

What is a Jupyter Notebook?

A Jupyter Notebook (.ipynb file) is an interactive document that combines live code, visualizations, and narrative text in a single file. The .ipynb extension stands for “IPython Notebook,” reflecting its origins in the IPython project. These notebooks are widely used in data science, machine learning, scientific computing, and education because they allow you to write and execute code in small chunks (called “cells”) and see results immediately.

Key Features

  • Interactive Execution: Run code cell by cell and see output instantly
  • Multiple Languages: While commonly used with Python, Jupyter supports many programming languages (Julia, R, and more)
  • Rich Output: Display text, images, plots, tables, LaTeX equations, HTML, and interactive widgets
  • Markdown Support: Mix code with formatted text, headings, lists, and documentation
  • Portable: Share notebooks as .ipynb files that others can run and modify

File Structure

An .ipynb file is actually a JSON document that contains:

  • Cells: Code cells (executable) and Markdown cells (documentation)
  • Outputs: Results from executed code cells (text, images, data)
  • Metadata: Information about the kernel, language version, and cell execution order

Example structure:

{
  "cells": [
    {
      "cell_type": "markdown",
      "source": ["# My First Notebook"]
    },
    {
      "cell_type": "code",
      "source": ["print('Hello, World!')"],
      "outputs": [...]
    }
  ],
  "metadata": {...}
}

How Jupyter Notebooks Work

Local Jupyter Environment

  1. Install Jupyter: pip install jupyter
  2. Launch Jupyter: jupyter notebook or jupyter lab
  3. Browser Interface: Opens in your web browser, running on a local server
  4. Kernel: A computational engine (e.g., Python interpreter) runs in the background
  5. Execution: You edit cells in the browser; the kernel executes code and returns results

VS Code Integration

VS Code has built-in support for Jupyter notebooks:

  • Open .ipynb files directly in VS Code
  • Run cells inline without launching a separate Jupyter server
  • Use the same Python environment configured for your workspace
  • Debug notebook code with breakpoints

Using Jupyter Notebooks in Google Colab

Google Colab (Colaboratory) is a free, cloud-based Jupyter notebook environment provided by Google. It requires no local setup and provides free access to GPUs and TPUs for machine learning tasks.

Key Benefits of Colab

  • Zero Installation: Runs entirely in your browser—no local setup required
  • Free GPU/TPU Access: Accelerate machine learning and deep learning workloads
  • Google Drive Integration: Save notebooks directly to Google Drive
  • Collaboration: Share notebooks like Google Docs; multiple users can edit simultaneously
  • Pre-installed Libraries: Common data science packages (NumPy, Pandas, TensorFlow, PyTorch) are already installed

How to Use Colab

1. Access Google Colab

Visit https://colab.research.google.com and sign in with your Google account.

2. Create a New Notebook

  • Click File → New Notebook
  • Or upload an existing .ipynb file: File → Upload Notebook

3. Open from Google Drive

  • Click File → Open Notebook
  • Choose from Recent, Google Drive, GitHub, or upload a file

4. Work with Cells

Code Cell:

# Click in a cell and type code
import numpy as np
print(np.array([1, 2, 3]))
  • Press Shift + Enter to run the cell
  • Press Ctrl/Cmd + Enter to run without moving to the next cell

Text Cell:

  • Click + Text to add a Markdown cell
  • Format using Markdown syntax

5. Enable GPU/TPU

For machine learning tasks:

  1. Click Runtime → Change runtime type
  2. Select GPU or TPU from the Hardware accelerator dropdown
  3. Click Save

6. Install Additional Packages

Colab comes with many libraries pre-installed, but you can add more:

!pip install package-name

7. Mount Google Drive

Access files from your Google Drive:

from google.colab import drive
drive.mount('/content/drive')

8. Save Your Work

  • Auto-save: Colab automatically saves to Google Drive (if opened from Drive)
  • Manual save: File → Save or Ctrl/Cmd + S
  • Download: File → Download → Download .ipynb

Colab vs. Local Jupyter

FeatureGoogle ColabLocal Jupyter
SetupNo installation neededRequires Python & Jupyter installation
CostFree (with usage limits)Free, but requires your hardware
GPU/TPUFree access with limitsRequires compatible hardware
StorageGoogle DriveLocal disk
InternetRequiredOptional (for local work)
CollaborationReal-time sharingRequires file sharing
EnvironmentManaged by GoogleFull control over dependencies
Session TimeLimited (disconnects after ~12 hours idle)No time limits

Common Colab Tips

Upload Files:

from google.colab import files
uploaded = files.upload()

Download Files:

from google.colab import files
files.download('result.csv')

Check GPU Availability:

import tensorflow as tf
print("GPU Available:", tf.config.list_physical_devices('GPU'))

Clear Output:

  • Click the X icon next to a cell’s output
  • Or: Runtime → Restart runtime to clear all outputs

Common Use Cases

  • Data Analysis: Explore datasets interactively with Pandas and visualization libraries
  • Machine Learning: Train models with TensorFlow, PyTorch, scikit-learn
  • Scientific Computing: Numerical simulations, statistical analysis
  • Teaching & Learning: Interactive tutorials and coding exercises
  • Documentation: Create reports that combine code, results, and explanations

Troubleshooting

Notebook won’t run locally:

  • Ensure Jupyter is installed: pip install jupyter
  • Check that the correct kernel is selected
  • Verify your Python environment has required packages

Colab session disconnected:

  • Colab disconnects after ~90 minutes of inactivity or ~12 hours of continuous use
  • Save your work regularly to Google Drive
  • Re-run cells after reconnecting

Module not found in Colab:

  • Install the package: !pip install package-name
  • Restart runtime if needed: Runtime → Restart runtime

Out of memory:

  • Use smaller datasets or batch sizes
  • Enable GPU/TPU for better performance
  • In Colab, restart runtime to clear memory

Additional Resources


Last updated: January 2026


^
TOP

Copyright @ 2025 CityU STC