Creating Table of Contents in Jupyter Notebook

Ahmet Ekiz
5 min readJul 5, 2022

--

When I read or write Jupyter Notebook I want to see a Table of Contents for ease of reading. I also learned that in order to better tell the story of the data or codes, the descriptions of the codes should be written in the notebook. I think this is the best way to code and learn data science.

When I want to create a new notebook, I always look through my old notebooks for a nice copy-paste chart. So I made mini templates for myself. I leave it here for anyone who wants to use it. Below are the sources and some code in which I use markdown.

Contents

  1. Create Table of Contents
  2. Headers and Internal Links
  3. External Links
  4. Colored Notes
  5. Other Formats
  6. Sources

Create Table of Contents

We will write the following code to create the Table of Contents above:

<a id="0"></a> <br>
# Table of Contents
1. [Introduction to Tabular Playground Series - Nov 2021](#1)
1. [Variable Describtions](#7)
1. [Load and Glance at the Data](#2)
1. [Missing Values](#3)
1. [Create a Validation Set](#4)
1. [Remove target column](#5)
1. [Feature Scaling](#6)
1. [First Model](#8)
1. [Evaluation Metrics for Training set](#9)
1. [Evaluation Metrics for Validation set](#10)
1. [First Submission](#11)
1. [Selecting Models](#12)
1. [Helper Functions to Try New Models](#13)
1. [Split to the Small Data for Evaluating Models Fast](#14)
1. [ML Models](#15)
1. [XGBoost](#16)
1. [Training](#17)

Headers and Internal Links

Click contents

To link headers and contents:

<a id="section_ID"></a>

You can add section ID before the header or text.

<a id="12"></a> 
# 7. Selecting Models
Ignore the blue “back to the top” text.

You should add the number of section you want to link to the end of the content line with parenthesis and # as above in the table of contents and below.

1. [Selecting Models](#12)

External Links

[link text](http://url)
This is [My Kaggle Account](https://www.kaggle.com/ahmetekiz).

Colored Notes

I found colored boxes from IBM Markdown for Jupyter notebooks cheatsheet. You should check for tutorial. They can be useful. These are basicallyHTML codes, you can check for other options and tutorials.

Blue boxes(alert-info)

<div class="alert alert-block alert-info"> <b>Tip:</b> Use blue boxes (alert-info) for tips and notes.  If it’s a note, you don’t have to include the word “Note”. </div>
Blue box

Yellow boxes (alert-warning)

<div class="alert alert-block alert-warning">
<b>Example:</b> Use yellow boxes for examples that are not inside code cells, or use for mathematical formulas if needed.
</div>
Yellow box

Green boxes (alert-success)

<div class="alert alert-block alert-success">
<b>Up to you:</b> Use green boxes sparingly, and only for some specific purpose that the other boxes can't cover. For example, if you have a lot of related content to link to, maybe you decide to use green boxes for related links from each section of a notebook.
</div>
Green box

Red boxes (alert-danger)

<div class="alert alert-block alert-danger"> 
<b>Just don't:</b> In general, avoid the red boxes. These should only be used for actions that might cause data loss or another major issue.
</div>

Other Formats

Bullet List

- age: age in years 
- sex
- 1 = male
- 0 = female
- cp: chest pain type (4 values)
Bullet list

Numbered Lists

1. Numbered item
1. Subs step
1. Second sub step
1. Numbered item
Numbered List

Emphasis — Bold and Italic

- Bold text: __string__ or **string**
- Italic text: _string_ or ***string***
Bold and Italic

Mathematical symbols

$ x+y=3^2 $
Mathematical Symbol

Monospace font

`This text will looked monospace font.`
Monospace

Indenting

Normal text. > Text that will be indented when the Markdown is rendered. Still in intented text. Normal Text
Indenting text

Horizontal Line

# Heading 1 before the line 
***
After the line
Horizontal Line

Colored Text

<font color = 'red'> 
Random Forest Classification algorithm accuracy is %85.24.
Colored Text

Line Breaker

Sometimes markdown doesn’t make line breaks when you want them.
To force a linebreak like in this sentence, use the following code: <br>After line breaker.
Line Breaker Result

Bonus: Displaying Video in Jupyter Notebook

from IPython.display import HTML
HTML("""
<center>
<video alt="Someone Running" width="600" controls>
<source src="videos/run_1.mp4" type="video/mp4">
</video>
</center>
""")
Displaying Video in Jupyter Notebook

Sources

--

--

Ahmet Ekiz

Machine Learning Engineer | MSc. CmpE | Computer Vision | Data Science | Robotics | BSc. Mechatronics