Table of contents jupiter notebook как установить

от admin

How to Add a Table of Contents in the Jupyter Notebook

It is essential to build a framework that is simple for the new readers to understand in order to avoid this situation. A table of contents is a very helpful approach to illustrate the procedure and can even aid in developing an initial strategy.

In this article, we will demonstrate how to create a table of contents using a Jupyter notebook.

How to Add a Table of Contents in a Jupyter Notebook

No matter how big or little the project is, adding a table of contents makes it clear with what path the project will take. The only tools required to include a table of contents in a Jupyter notebook are the “anchor tags” in the appropriate places. The links that point to the other sections of the notebook’s table of content are translated into such links.

Creating a table of content in a Jupyter notebook is quite easy and simple. You can complete it in the following number of steps:

Step 1: Select the Markdown Format
We can add the table of content in the Jupyter notebook using the HTML anchor. This is a simple two-step process. Open the Jupyter notebook and select the markdown cell format instead of the code.

Step 2: Create the Structure of the Table of Content
First, create a table of contents using the markdown in the notebook. Here, we also need to link the anchors that we will create in the next step. Use the following text and paste it into the markdown cell:

Press “Shift + Enter” to run the previous lines in the Jupyter notebook. The table of content should display like this:

The structure of the table of content follows the markdown format available in the Jupyter notebook. In the previous screenshot, the displayed name of the link is enclosed in brackets [] and the reference to the anchor tags is placed in parenthesis preceded by a hash (#) symbol.

The markdown cell in the anchor tag is included in either a title to go with the operation or a description of the current processes.

Step 3: Create Anchor Tags
Now, we will create the anchor tags in order to link with the table of contents. Create the chapters, sections, and subsections. Enter the following text in the next markdown cell:

Press “Shift + Enter” or run this cell to see the effects. The following output should display on your notebook:

Here, you will notice that you can easily navigate to the desired section from the table of content.

Conclusion

We demonstrated in this article how to create a table of content in a Jupyter notebook. Using a table of content, you can easily manage all the content and can easily navigate to the desired section of your document. We can also add a table of content in a Jupyter notebook using the pre-build extensions. I hope this article increases your understanding about the Jupyter notebook.

About the author

Samreena Aslam

Samreena Aslam holds a master’s degree in Software Engineering. Currently, she’s working as a Freelancer & Technical writer. She’s a Linux enthusiast and has written various articles on Computer programming, different Linux flavors including Ubuntu, Debian, CentOS, and Mint.

Table of Contents (2)¶

The toc2 extension enables to collect all running headers and display them in a floating window, as a sidebar or with a navigation menu. The extension is also draggable, resizable, collapsable, dockable and features automatic numerotation with unique links ids, and an optional toc cell. Sections of currently selected/edited or running cells are highlighted in the toc. Some minor diplay tweaks are also available (moving header tile/menus, widening cells); Finally, the toc can preserved when exporting to html.

First demo: Floating toc window and SideBar, toc auto-update, section numbering¶

Second demo: Save as html with toc / Navigation menu¶

Third demo: Notebook scrolling and Collapsing sections¶

The table of contents is automatically updated when modifications occur in the notebook. The toc window can be moved and resized. It can be docked as a sidebar or dragged from the sidebar into a floating window. The table of contents can be collapsed or the window can be completely hidden. The navigation menu can be enabled/disabled via the nbextensions configuration utility. It can also be resized. The position, dimensions, and states (that is ‘collapsed’ and ‘hidden’ states) are remembered (actually stored in the notebook’s metadata) and restored on the next session.

There is a configurable option to skip h1 headers from the ToC, to allow their use as a notebook title. However, this cause issues in latex exports, where h1 are converted to sections. Alternatively, headers of any level can be omitted from being the toc by adding an html tag with the css class tocSkip at the end of the header line; e.g. as in

The toc window also provides two links in its header for further functionalities:

  • the “n” link toggles automatic numerotation of all header lines
  • the “t” link toggles a toc cell in the notebook, which contains the actual table of contents, possibly with the numerotation of the different sections. The position of the toc cell in the notebook can be configured by creating a cell with metadata (View > Cell Toolbar > Edit Metadata): < “toc”: 1 >, this cell will then be replaced by the table of contents.

The state of these two toggles is memorized and restored on reload.

Configuration¶

The initial configuration can be set using the jupyter_nbextensions_configurator facility, included with jupyter_contrib_nbextnensions. Configurable options include:

  • Display Table of Contents as a sidebar (otherwise as a floating window; default: true)
  • Title of the sidebar/window (default: Contents )
  • The maximum depth of headers to display on toc (with a default of 4)
  • The state of the toc cell (default: false, ie not present)
  • Title of the toc cell sidebar/window (default: Table of Contents )
  • Add a navigation menu (default: true)
  • Widening the display area to fit the browser window (may be useful with sidebar option; default: true)
  • The numbering of headers (true by default)
  • Moving header title and menus on the left (default: true)
  • Marking toc item of first header displayed on viewport when scrolling the notebook (default: true)
  • Skipping h1 headers, useful if you want to use h1 as unnumbered notebook title (default: false)
  • Customization of highlighting the title of currently selected/running sections.
  • Customization of background, fonts, border and highlighting colors in the toc window and navigation menus (as third demo).
  • Collapse/uncollapse ToC2 sections when collapsible_headings is used to collapse/uncollapse notebook sections (default: false).
Читать:
Как записать вектор в файл c

Some config settings are stored in notebook metadata, so that they can be altered per-notebook, as well as setting the default in the configurator. The differents states and position of the floating window have reasonable defaults and can be modfied per notebook.

Demo with dark theme¶

Export¶

It is possible to export (most of) table of contents functionalities to html. The idea is to link a relevant part of the javascript extension and the css, and add a small script in the html file. This is done using a template by

An exporter is also available. It is now possible to export to html with toc by

If you also use latex_envs, you can embed both functionalities while exporting with

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

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

You can add a TOC manually with Markdown and HTML. Heres how I have been adding:

Create TOC at top of Jupyter Notebook:

Add html anchors throughout body:

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.

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

JupyterLab ToC instructions

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.

As a side panel

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

In the notebook as a cell

At the time being,
this can either be done manually as in Matt Danchos answer,
or automatically via the toc2 jupyter notebook extension
in the classic notebook interface.

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 cant 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).

In an exported HTML file

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):

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:

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.

Installing Table of Contents extension to Jupyter notebook

Table of contents 2 is a very useful extension to Jupyter notebook that allows you to have a nested outline with links to all the Markdown headings in a Jupyter notebook. This is basically essential when you’re working with a notebook of any real length. I don’t know why it isn’t just a standard feature in Jupyter.

Figuring out how to install it is a bit annoying as instructions are poorly written across several Github repos, but the process is actually very simple (if a little hacky). Assuming you followed my instructions on setting up a virtual environment in Python 3, activate it with (substitute your own path if needed):

Now install jupyter_contrib_nbextensions. This bundles a bunch of extensions to Jupyter, as well as a configuration panel that will be added to the Jupyter interface.

This command is needed to complete the installation of the extensions to Jupyter itself:

Now on the main Jupyter page, click the tab “Nbextensions”, then check the box for “Table of contents (2)”.

Screen Shot 2018-04-19 at 7.03.29 PM

Screen Shot 2018-04-19 at 7.03.37 PM

Now when you start a notebook, click the “Table of Contents” button, and you’ll get a sidebar on the left with your table of contents. Voila!

Похожие статьи