How can I see the table of contents at the beginning of the notebook in jupyter lab

Dear all, I am not able to show at the beginning of the notebooks, in jupyter lab, the table of contents. I will appreciate help.

Did you try to click on the icon on the left side of JupyterLab? (third from the top on my setup - it looks like three horizontal lines and is right above the jigsaw piece).
If you don’t have any headings defined, it will show No Headings there, but if you do have headings, it should show a table of contents.

Thank you. It is what I do but the table of contents appears to the left of the screen. I would like to have it on the left and at the top. I want to have it at the top because I don’t see any other way to export it together with the notebook.

What do you want to export it to?

I want to export all the notebook, with table of contents, to HTML or to PDF.

I have found this solution. Copy the output of the function into a markdown cell in the notebook and we will have a TOC with indices and subscripts.

import urllib
import json

def generate_toc(notebook_path, indent_char=" “):
is_markdown = lambda it: “markdown” == it[“cell_type”]
is_title = lambda it: it.strip().startswith(”#“) and it.strip().lstrip(”#").lstrip()

with open(notebook_path, 'r') as in_f:
    nb_json = json.load(in_f)

toc_numbers = []  # Lista para llevar el conteo de los números de contenido en cada nivel

for cell in filter(is_markdown, nb_json["cells"]):
    for line in filter(is_title, cell["source"]):
        line = line.strip()
        title = line.lstrip("#").lstrip()

        level = line.count("#")  # Nivel del título según la cantidad de "#"

        if level > len(toc_numbers):
            toc_numbers.append(1)  # Agregar un nuevo nivel con numeración inicial en 1
            toc_numbers[level - 1] += 1  # Incrementar el número de contenido en el nivel actual
            toc_numbers[level:] = [1] * (len(toc_numbers) - level)  # Reiniciar numeración en niveles inferiores

        toc_number_str = ".".join(str(num) for num in toc_numbers[:level])
        indent = indent_char * level
        url = urllib.parse.quote(title.replace(" ", "-"))
        out_line = f"{indent}{toc_number_str} [{title}](#{url})<br>\n"
        print(out_line, end="")

Ejemplo de uso

path = ‘/media/enri/Mi_Proyecto/Py_Proyecto_2023/Trabajo estadística/2 Estadistica_Inferencial_probabilidad_2.0.ipynb’