" MicromOne: How to Add a Table of Contents to Your Jupyter Notebook

Pagine

How to Add a Table of Contents to Your Jupyter Notebook

Working inside Jupyter notebooks is one of the most popular ways to write and share code, especially in data science, machine learning, and research workflows. As notebooks grow in length and complexity, navigating them can become frustrating. This is where a Table of Contents becomes extremely useful.

In this article, we’ll look at why a Table of Contents matters, how to create one manually, how anchors work in Jupyter, and how modern tools can generate a TOC automatically.

Why Your Notebook Needs a Table of Contents

Long notebooks with multiple sections such as data loading, preprocessing, analysis, and visualization are hard to navigate by scrolling alone. A Table of Contents helps by:

  • Improving navigation

  • Making the structure of the notebook clear

  • Enhancing readability for collaborators

  • Providing a quick overview of the workflow

This is particularly helpful when sharing notebooks or exporting them to HTML or PDF.

Creating a Manual Table of Contents with Markdown

One simple way to create a Table of Contents is by using Markdown links that point to section headers.

Example:

### Table of Contents

1. [Introduction](#introduction)
2. [Load Data](#load-data)
3. [Exploratory Analysis](#exploratory-data-analysis)
4. [Results](#results)
5. [Conclusion](#conclusion)

Each link refers to a specific section inside the notebook.

How Section Anchors Work in Jupyter Notebooks

In Jupyter notebooks, you usually do not need to manually define anchors using HTML. When you create a Markdown heading, Jupyter automatically generates an anchor for it.

Example:

## Introduction

Jupyter automatically creates the anchor:

#introduction

This means you can link to that section using:

[Introduction](#introduction)

Automatic Anchor Naming Rules

Jupyter follows these rules when creating anchors from headings:

  • All characters are converted to lowercase

  • Spaces are replaced with hyphens

  • Special characters are removed

Example:

## Exploratory Data Analysis

Generated anchor:

#exploratory-data-analysis

Link reference:

[Exploratory Analysis](#exploratory-data-analysis)

When to Use <a id=""></a> Manually

In most cases, Markdown headings are enough. However, manually defining an anchor using HTML can be useful in advanced scenarios, such as:

  • When you want a custom anchor name

  • When linking to a specific point that is not a heading

  • When you have duplicate section titles

Example:

<a id="custom-anchor"></a>
### My Section Title

You can then reference it like this:

[Go to section](#custom-anchor)

Automatic Table of Contents Options

If you prefer automation, there are powerful tools available.

Jupyter Notebook Extensions

The Table of Contents (2) extension from jupyter_contrib_nbextensions automatically generates a navigation panel based on notebook headings. It supports automatic updates, collapsible sections, and sidebar display.

JupyterLab Built-in Table of Contents

JupyterLab includes a built-in Table of Contents panel in the sidebar. It reads Markdown headers and allows quick navigation without installing additional extensions.