Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toc2: sidebar + html export #593

Merged
merged 7 commits into from
Jun 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions extensions/toc2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Toc2 Exporter class"""

#-----------------------------------------------------------------------------
# Copyright (c) 2016, the IPython IPython-Contrib Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------

from traitlets.config import Config
from traitlets.config import Config
from nbconvert.exporters.html import HTMLExporter

#-----------------------------------------------------------------------------
# Classes
#-----------------------------------------------------------------------------

class TocExporter(HTMLExporter):
"""
Exports to an html document, embedding toc2 extension (.html)
"""

def _file_extension_default(self):
return '.html'

def _template_file_default(self):
return 'toc3'

output_mimetype = 'text/html'

def _raw_mimetypes_default(self):
return ['text/markdown', 'text/html', '']

@property
def default_config(self):
import jupyter_core.paths
import os
c = Config({'ExtractOutputPreprocessor':{'enabled':True}})
c.merge(super(TocExporter,self).default_config)

user_templates = os.path.join(jupyter_core.paths.jupyter_data_dir(), 'templates')
c.TemplateExporter.template_path = [
'.', user_templates ]
return c
41 changes: 33 additions & 8 deletions nbextensions/usability/toc2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

## Description and main features

This extension adds a small button in the main toolbar, which enables to collect all running headers in the notebook and display them in a floating window.
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. Finally, the toc can preserved when exporting to html.

![](icon.png)
#### First demo:
![](demo.gif)

The table of contents is automatically updated when modifications occur in the notebook. The toc window can be moved and resized, the table of contents can be collapsed or the window can be completely hidden. 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. The floating window also provides two links in its header for further functionalities:
#### Second demo:
![](demo2.gif)

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. 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.
Expand All @@ -16,11 +20,30 @@ The state of these two toggles is memorized and restored on reload.
![](image.png)

## Configuration
The initial configuration can be given using the IPython-contrib nbextensions facility. The maximum depth of headers to display on toc can be precised there (with a default of 4), as well as the state of the toc cell (default: false, ie not present) and the numbering of headers (true by default). The differents states and position of the floating window have reasonable defaults and can be modfied per notebook).


The initial configuration can be given using the IPython-contrib nbextensions facility. It includes:

- The toc initial mode (floating or sidebar)
- The maximum depth of headers to display on toc (with a default of 6)
- The state of the toc cell (default: false, ie not present)
- The numbering of headers (true by default).

The differents states and position of the floating window have reasonable defaults and can be modfied per notebook).

## 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
```
or
```
jupyter nbconvert FILE.ipynb --template toc2
```
For the first template (toc), the files toc2.js and main.css (originally located in JUPYTER_DATA_DIR/nbextensions/usability/toc2) must reside in the same directory as intended for the html file. In the second template, these files are linked to the IPython-notebook-extensions github website. Export configuration (parameters) shall be edited directly in the template files (in templates directory JUPYTER_DATA_DIR/templates). An option "Save as HTML (with toc)" is also provided in the File menu and enable to directly convert the actual notebook. This option requires the IPython kernel and is not present with other kernels.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would there be a way to simply include these files in the template/final output? static one page files are best for sending this stuff to my adviser :-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JanSchulz You just have to replace the link in the template by

{% include "FILENAME" ignore missing%}

This assumes that the file is in the same directory as the notebook (full paths do not work in jinja tempates).
I do not do it because anyway you still need an internet connection to get access to jquery. Please make a PR with an extra template if you want

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO the default should be to include stuff which is not on a CDN, so that sending the html file will work as long as a user has access to the internet... IMO the "toc must be on my drive" is useless because IMO html exports are to be exchanged with peers and so not on my drive. Or do I misunderstand anything here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps. You do not need to have toc or the notebook installed. Everything will work as long as the user has an internet access.
If you do not have access to internet, you will not have jquery and the toc javascript will not work. This is not true in the notebook since it has a copy of jquery inside. Otherwise you need to have an access to jquery, and most of the time this suppose an internet access.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You do not need to have toc or the notebook installed. Everything will work as long as the user has an internet access.

+1



# Testing
- At loading of the notebook, configuration and initial rendering of the table of contents were fired on the event "notebook_loaded.Notebook". Curiously, it happened that this event was either not always fired or detected. Thus I rely here on a combination of "notebook_loaded.Notebook" and "kernel_ready.Kernel" instead.
- 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 https://github.com/ipython-contrib/IPython-notebook-extensions/issues/429

Expand All @@ -33,6 +56,8 @@ from https://gist.github.com/magican/5574556
- @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](https://github.com/minrk/ipython_extensions/pull/53) 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
- @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](https://github.com/ipython-contrib/IPython-notebook-extensions/issues/532), 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This results in two dots...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will correct this at next iteration.

Binary file added nbextensions/usability/toc2/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added nbextensions/usability/toc2/demo2.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 78 additions & 8 deletions nbextensions/usability/toc2/main.css
Original file line number Diff line number Diff line change
@@ -1,18 +1,70 @@
/*extracted from https://gist.github.com/magican/5574556*/

#toc {

#toc-level0 li > a:hover { display: block; background-color: #DAA520}
#toc-level0 a {color: #333333; text-decoration: none;}
#navigate_menu li a:hover {background-color: #f1f1f1}

/*#Navigate_menu {
list-style-type: none;
max-width: 500px;
min-width: 100px;
width: 250px;
overflow: auto;
}

#navigate_menu {
overflow: hidden;
}

#Navigate_menu {
list-style-type: none;
overflow: auto;
}*/

#navigate_menu {
/*display: block;*/
list-style-type: none;
max-width: 800px;
min-width: 100px;
width: 250px;
overflow: auto;
}


#navigate_menu a {
list-style-type: none;
color: #333333;
text-decoration: none;
}

#navigate_menu li {
padding-left: 2px;
clear: both;
list-style-type: none;
}

#navigate_menu-level0 {padding-left: 10px;}
#navigate_menu-level0 ul {padding-left: 10px;}


.toc {
max-height: 500px;
padding: 0px;
overflow-y: auto;
font-weight: normal;
color: #333333;
white-space: nowrap;
overflow-x: auto;
}

#toc ol.toc-item {
.toc ol.toc-item {
counter-reset: item;
list-style: none;
padding: 0.1em;
}

#toc ol.toc-item li {
.toc ol.toc-item li {
display: block;
}

Expand All @@ -22,16 +74,16 @@
margin: 0;
}

#toc ol.toc-item li:before {
.toc ol.toc-item li:before {
font-size: 90%;
font-family: Georgia, Times New Roman, Times, serif;
counter-increment: item;
content: counters(item, ".")" ";
}


#toc-wrapper {
position: fixed;
.float-wrapper {
position: fixed !important;
top: 120px;
max-width:600px;
right: 20px;
Expand All @@ -44,10 +96,29 @@
overflow: hidden;
}

.sidebar-wrapper {
height: 100%;
left: 5px;
padding: 10px;
position: fixed !important;
width: 212px;
max-width: 28%;
background-color: #fff;
border-style: solid;
border-color: #eeeeee;
opacity: .99;
overflow: hidden;
}


.col-md-9 {
overflow:hidden;
margin-left: 14%;
width: 80%}

#toc-wrapper.closed {
min-width: 100px;
width: auto;
height:auto;
transition: width;
}
#toc-wrapper:hover{
Expand Down Expand Up @@ -100,4 +171,3 @@
.lev6 {margin-left: 180px}
.lev7 {margin-left: 200px}
.lev8 {margin-left: 220px}

Loading