Table of contents jupiter notebook как установить
Перейти к содержимому

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

  • автор:

Table Of Contents¶

The table of contents extension is built-in JupyterLab since version 3.0. This makes it easy to see and navigate the structure of a document.

Anyone using earlier JupyterLab version should install a dedicated extension.

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.

Here is an animation showing the table of content use, with a notebook from the Python Data Science Handbook :

The table of contents tool will automatically generate a table of contents for your notebook by taking all the headings from your markdown cells. Because the Notebook does not utilize formal page breaks or numbers, each listed section will be hyperlinked to the actual section within your document.

Automatic section numbering will go through your Notebook and number your sections and subsection as designated by your headings. This means that if you’ve moved one or more big sections around several times, you won’t have to go through your document and renumber it, as well as all its subsections, yourself.

Automatic section numbering can be skipped for first-level headings ( h1 ) by setting the numberingH1 setting to false . To perform that go to settings and click on Advanced Settings Editor and then go to Table of Contents section and in User Preferences add numberingH1:false and save the settings. Here is an animation showing its use

Context menus are added to those table of contents headings having notebook sections containing runnable code cells, and clicking the Run Cell(s) option will make the cells run in notebook. Here is an animation showing its use

There will be a synchronization between table of contents and notebook if the syncCollapseState attribute is set to true in the settings. If a heading is collapsed in the table of contents the notebook will also gets collapsed and similarly expanding cells in notebook will expand the table of contents. To perform that go to settings and click on Advanced Settings Editor and then go to Table of Contents section and in User Preferences add syncCollapseState:true and save the settings. Here is an animation showing its use

The headings in the cell outputs get numbered by default in the table of contents and the notebook. This can be tuned by changing the settings includeOutput to false . To perform that go to settings and click on Advanced Settings Editor and then go to Table of Contents section and in User Preferences add includeOutput:false and save the settings. Here is an animation showing its use

Settings¶

The extension behavior can be modified via settings which can be set in JupyterLab’s advanced settings editor.

  • collapsibleNotebooks: enable the ability to collapse sections of notebooks from the table of contents
  • numberingH1 : numbers the first-level headings ( h1 ) if set to true
  • syncCollapseState : synchronization of collapsing behaviour between the table of contents and notebook
  • includeOutput : includes the numbering for Outputs in both notebook and table of contents.

© Copyright 2018, Project Jupyter.
The Jupyter Trademark is registered with the U.S. Patent &amp Trademark Office. Revision d3897d2d .

Jupyter Notebook ToC

This is a simple extension to generate a table of contents for a Jupyter notebook.

Basic Usage

It will return something like this:
Table of Contents  [  [ [ [

You can pass arguments to customize the following:

  • title — The title of the table of contents.
  • depth — The maximum depth of headings to include in the table of contents.
  • make_hyperlinks — Whether to make the table of contents entries hyperlinks.
  • print_output — Whether to print the ToC or just return it as a string.

Contributing

Feel free to open an issue or submit a pull request.

To publish a new version to PyPI:

pip install twine wheel python setup.py sdist bdist_wheel twine upload dist/* 

Development

Running Tests

The test suite can be run using Python’s built-in unittest framework.

Navigate to the tests directory and run:

python -m unittest test_jupyter_toc.py 

Or, if you’re in the root directory:

python -m unittest tests/test_jupyter_toc.py 

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

source .virtualenvs/jupyter/

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.

pip install jupyter_contrib_nbextensions

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

jupyter contrib nbextension install --user

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!

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

## title 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).

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

jupyter nbconvert FILE.ipynb --template toc 
jupyter nbconvert FILE.ipynb --template toc2 

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

jupyter nbconvert --to html_toc FILE.ipynb 

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

jupyter nbconvert --to html_with_toclenvs FILE.ipynb 

For the first template (toc), the files toc2.js and main.css (originally located in /jupyter_contrib_nbextensions/nbextensions/toc2 ) must reside in the same directory as intended for the html file. In the second template, these files are linked to the ipython-contrib/jupyter_contrib_nbextensions github website. Export configuration (parameters) shall be edited directly in the template files (in templates directory /jupyter_contrib_nbextensions/templates ). An option “Save as HTML (with toc)” is also provided in the File menu and enables the direct conversion of the actual notebook. This option requires the IPython kernel and is not present with other kernels.

Testing¶

  • At loading of the notebook, configuration and initial rendering of the table of contents were fired on the event “notebook_loaded.Notebook”. It happens that the extension is sometimes loaded after this event. I now rely on a direct rendering at load and on a combination of “notebook_loaded.Notebook” and “kernel_ready.Kernel”.
  • This extension also includes a quick workaround as described in #429

History¶

  • This extension was adapted by minrk in minrk/ipython_extensions from gist.github.com/magican/5574556
  • Added to the ipython-contrib/jupyter_contrib_nbextensions repo by @JanSchulz
  • @juhasch, automatic update on markdown rendering,
  • @JanSchulz, enable maths in headers links
  • @jfbercher december 06, 2015 – Big update: automatic numbering, toc cell, window dragging, configuration parameters
  • @jfbercher december 24, 2015 – nested numbering in toc-window, following the fix by @paulovn in @minrk’s repo. December 30-31, updated config in toc2.yaml to enable choosing the initial visible state of toc_window via a checkbox ; and now resizable.
  • @slonik-az february 13, 2016. Rewritten toc numberings (more robust version), fixed problems with skipped heading levels, some code cleanup
  • @jfbercher february 21, 2016. Fixed some issues when resizing the toc window. Now avoid overflows, clip the text and add a scrollbar.
  • @jfbercher february 22, 2016. Add current toc number to headings anchors. This enable to get unique anchors for recurring headings with the same text. An anchor with the original ID is still created and can be used (but toc uses the new ones!). It is also possible to directly add an html anchor within the heading text. This is taken into account when building toc links (see comments in code).
  • @jfbercher april 29, 2016. Triggered by @cqcn1991, cf discussion here, add a sidebar option. The floating toc window can be dragged and docked as a left sidebar. The sidebar can be dragged out as a floating window. These different states are stored and restored when reloading the notebook. Add html export capability via templates toc.tpl and toc2.tpl (see above).
  • @jfbercher may 04, 2016. Added a “Save as HTML with toc” menu. Added a new “Navigate” menu with presents the contents of the toc. Changed default styling for links in tocs.
  • @jfbercher july 28, 2016. A dedicated exporter was added. It is now possible to export to html with toc by jupyter nbconvert —to html_toc FILE.ipynb
  • @jfbercher september 21, 2016. Fixed empty size of navigation menu (if no resize had occur). Changed system/notebook configuration parameters storing, loading and merging.
  • @jfbercher november 16, 2016.
    • Fixed saving issue due to a race condition in loading/writing metadata; see issues #1882 and #762
    • As suggested by @dinya in #791, added highlighting of the section that contains the currently edited/selected/executing cell. Colors can be customized by changing .toc-item-highlight-select and .toc-item-highlight-execute classes in css. -[update nov 23]. As suggested by @jcb91, the highlight colors can now be configured via the nbextensions–configurator, instead of changing the css.
    • Threshold (number of headings levels in toc)taken globally as requested in #646 (if it exists, otherwise default)
    • Make toc2 template inherits from nbextensions template, as mentioned in #847
    • On header/menu/toolbar resize (resize-header.Page event), resize toc2 sidebar
    • On ‘toggle-all-headers’ event from hide_menubar extension, resize toc2 sidebar
    • Remove MathJax preview in headers and links – addresses (issue 14 in latex_envs)[https://github.com/jfbercher/jupyter_latex_envs/issues/14]
    • Added a parameter to enable/disable cell widening (which is useful when sideBar is on) — default is to widen — address #871
    • Updated README to please @KadeG in #871
    • Support customization of background, fonts, border and highlighting colors in the toc window and navigation menus with PR #969
    • correct toc tree construction
    • simplify toc cell processing
    • constrain draggable toc to the body
    • various bits of cleanup
    • add settings dialog for per-notebook metadata-stored settings

    © Copyright 2015-2018, Jupyter Contrib Team. Revision 03109e43 .

    Built with Sphinx using a theme provided by Read the Docs.
    Read the Docs v: latest

    Versions latest stable Downloads On Read the Docs Project Home Builds Free document hosting provided by Read the Docs.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *