A table of contents is auto-generated in the left sidebar when you have a notebook, markdown, latex or python files opened. The entries are clickable, and scroll the document to the heading in question. In the sidebar panel, you can number headings, collapse sections, and navigate into the file.
Header cellStart the text in markdown cell by # symbol. Use as many # symbols corresponding to level of header you want. It means single # will render biggest header line, and six # symbols renders header of smallest font size.
You can add a TOC manually with Markdown and HTML. Here's how I have been adding:
## TOC:
* [First Bullet Header](#first-bullet)
* [Second Bullet Header](#second-bullet)
## First Bullet Header <a class="anchor" id="first-bullet"></a>
code blocks...
## Second Bullet Header <a class="anchor" id="second-bullet"></a>
code blocks...
It may not be the best approach, but it works. Hope this helps.
There is an ipython nbextension that constructs a table of contents for a notebook. It seems to only provide navigation, not section folding.
There are already many good answers to this question, but they often require tweaks to work properly with notebooks in JupyterLab. I wrote this answer to detail the possible ways of including a ToC in a notebook while working in and exporting from JupyterLab.
The jupyterlab-toc extension adds the ToC as a side panel that can number headings, collapse sections, and be used for navigation (see gif below for a demo). This extension is included by default since JupyterLab 3.0, in older version you can install it with the following command
jupyter labextension install @jupyterlab/toc
At the time being, this can either be done manually as in Matt Dancho's answer, or automatically via the toc2 jupyter notebook extension in the classic notebook interface.
First, install toc2 as part of the jupyter_contrib_nbextensions bundle:
conda install -c conda-forge jupyter_contrib_nbextensions
Then,
launch JupyterLab,
go to Help --> Launch Classic Notebook
,
and open the notebook in which you want to add the ToC.
Click the toc2 symbol in the toolbar
to bring up the floating ToC window
(see the gif below if you can't find it),
click the gear icon and check the box for
"Add notebook ToC cell".
Save the notebook and the ToC cell will be there
when you open it in JupyterLab.
The inserted cell is a markdown cell with html in it,
it will not update automatically.
The default options of the toc2 can be configured in the "Nbextensions" tab in the classic notebook launch page. You can e.g. choose to number headings and to anchor the ToC as a side bar (which I personally think looks cleaner).
nbconvert
can be used to export notebooks to HTML
following rules of how to format the exported HTML.
The toc2
extension mentioned above adds an export format called html_toc
,
which can be used directly with nbconvert
from the command line
(after the toc2
extension has been installed):
jupyter nbconvert file.ipynb --to html_toc
# Append `--ExtractOutputPreprocessor.enabled=False`
# to get a single html file instead of a separate directory for images
Remember that shell commands can be added to notebook cells
by prefacing them with an exclamation mark !
,
so you can stick this line in the last cell of the notebook
and always have an HTML file with a ToC generated
when you hit "Run all cells"
(or whatever output you desire from nbconvert
).
This way,
you could use jupyterlab-toc
to navigate the notebook while you are working,
and still get ToCs in the exported output
without having to resort to using the classic notebook interface
(for the purists among us).
Note that configuring the default toc2 options
as described above,
will not change the format of nbconver --to html_toc
.
You need to open the notebook in the classic notebook interface
for the metadata to be written to the .ipynb file
(nbconvert reads the metadata when exporting)
Alternatively,
you can add the metadata manually
via the Notebook tools tab of the JupyterLab sidebar,
e.g. something like:
"toc": {
"number_sections": false,
"sideBar": true
}
If you prefer a GUI-driven approach,
you should be able to open the classic notebook
and click File --> Save as HTML (with ToC)
(although note that this menu item was not available for me).
The gifs above are linked from the respective documentation of the extensions.
Here is one more option without too much JS hassle: https://github.com/kmahelona/ipython_notebook_goodies
How about using a Browser plugin that gives you an overview of ANY html page. I have tried the following:
They both work pretty well for IPython Notebooks. I was reluctant to use the previous solutions as they seem a bit unstable and ended up using these extensions.
I recently created a small extension to Jupyter named jupyter-navbar. It searches for headers written in markdown cells, and displays links to them in the sidebar in a hierarchical fashion. The sidebar is resizable and collapsible. See screenshot below.
It is easy to install, and takes advantage of the 'custom' JS and CSS codes that get executed whenever a notebook is opened, so you don't need to manually run it.
There are now two packages that can be used to handle Jupyter extensions:
jupyter_contrib_nbextensions that installs extensions, including table of contents;
jupyter_nbextensions_configurator that provides graphical user interfaces for configuring which nbextensions are enabled (load automatically for every notebook) and provides controls to configure the nbextensions' options.
UPDATE:
Starting from recent versions of jupyter_contrib_nbextensions
, at least with conda
you don't need to install jupyter_nbextensions_configurator
because it gets installed together with those extensions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With