How can I add a table of contents to a Jupyter / JupyterLab notebook?

后端 未结 10 1780
深忆病人
深忆病人 2020-12-12 11:12

The documentation at http://ipython.org/ipython-doc/stable/interactive/notebook.html says

You can provide a conceptual structure for your computationa

相关标签:
10条回答
  • 2020-12-12 11:52

    Here is my approach, clunky as it is and available in github:

    Put in the very first notebook cell, the import cell:

    from IPythonTOC import IPythonTOC
    
    toc = IPythonTOC()
    

    Somewhere after the import cell, put in the genTOCEntry cell but don't run it yet:

    ''' if you called toc.genTOCMarkdownCell before running this cell, 
    the title has been set in the class '''
    
    print toc.genTOCEntry()
    

    Below the genTOCEntry cell`, make a TOC cell as a markdown cell:

    <a id='TOC'></a>
    
    #TOC
    

    As the notebook is developed, put this genTOCMarkdownCell before starting a new section:

    with open('TOCMarkdownCell.txt', 'w') as outfile:
    
        outfile.write(toc.genTOCMarkdownCell('Introduction'))
    
    !cat TOCMarkdownCell.txt
    
    !rm TOCMarkdownCell.txt
    

    Move the genTOCMarkdownCell down to the point in your notebook where you want to start a new section and make the argument to genTOCMarkdownCell the string title for your new section then run it. Add a markdown cell right after it and copy the output from genTOCMarkdownCell into the markdown cell that starts your new section. Then go to the genTOCEntry cell near the top of your notebook and run it. For example, if you make the argument to genTOCMarkdownCell as shown above and run it, you get this output to paste into the first markdown cell of your newly indexed section:

    <a id='Introduction'></a>
    
    ###Introduction
    

    Then when you go to the top of your notebook and run genTocEntry, you get the output:

    [Introduction](#Introduction)
    

    Copy this link string and paste it into the TOC markdown cell as follows:

    <a id='TOC'></a>
    
    #TOC
    
    [Introduction](#Introduction)
    

    After you edit the TOC cell to insert the link string and then you press shift-enter, the link to your new section will appear in your notebook Table of Contents as a web link and clicking it will position the browser to your new section.

    One thing I often forget is that clicking a line in the TOC makes the browser jump to that cell but doesn't select it. Whatever cell was active when we clicked on the TOC link is still active, so a down or up arrow or shift-enter refers to still active cell, not the cell we got by clicking on the TOC link.

    0 讨论(0)
  • 2020-12-12 11:56

    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.

    0 讨论(0)
  • 2020-12-12 11:57

    How about using a Browser plugin that gives you an overview of ANY html page. I have tried the following:

    • HTML 5 Outliner for Chrome
    • Headings Map for Firefox

    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.

    0 讨论(0)
  • 2020-12-12 12:00

    nbextensions ToC instructions

    Introduction

    As @Ian and @Sergey have mentioned, nbextensions is a simple solution. To elaborate their answer, here is a few more information.

    What is nbextensions?

    The nbextensions contains a collection of extensions that add functionality to your Jupyter notebook.

    For example, just to cite a few extensions:

    • Table of Contents

    • Collapsible headings

    Install nbextensions

    The installation can be done through Conda or PIP

    # If conda:
    conda install -c conda-forge jupyter_contrib_nbextensions
    # or with pip:
    pip install jupyter_contrib_nbextensions
    

    You will see the new tab Nbextensions in the jupyter notebook menu. Uncheck the checkbox at the top disable configuration for nbextensions without explicit compatibility (they may break your notebook environment, but can be useful to show for nbextension development) and then check Table of Contents(2). That is all. Screenshot:

    Copy js and css files

    To copy the nbextensions' javascript and css files into the jupyter server's search directory, do the following:

    jupyter contrib nbextension install --user
    

    Toggle extensions

    Note that if you are not familiar with the terminal, it would be better to install nbextensions configurator (see the next section)

    You can enable/disable the extensions of your choice. As the documentation mentions, the generic command is:

    jupyter nbextension enable <nbextension require path>
    

    Concretely, to enable the ToC (Table of Contents) extension, do:

    jupyter nbextension enable toc2/main
    

    Install Configuration interface (optional but useful)

    As its documentation says, nbextensions_configurator provides config interfaces for nbextensions.

    It looks like the following:

    To install it if you use conda:

    conda install -c conda-forge jupyter_nbextensions_configurator
    

    If you don't have Conda or don't want to install through Conda, then do the following 2 steps:

    pip install jupyter_nbextensions_configurator
    jupyter nbextensions_configurator enable --user
    
    0 讨论(0)
提交回复
热议问题