Skip to content

Instantly share code, notes, and snippets.

@parente
Created September 10, 2013 13:13
Show Gist options
  • Select an option

  • Save parente/6509213 to your computer and use it in GitHub Desktop.

Select an option

Save parente/6509213 to your computer and use it in GitHub Desktop.
TOC in the View menu of IPython Notebook. Tested in 0.9.
.ui-menu {
width: 300px !important;
}
div.code_cell {
page-break-after: always;
page-break-inside: avoid;
}
.rendered_html h4 {
margin: 0.95em 0em;
}
$([IPython.events]).on('notebook_loaded.Notebook', function(){
var view_menu = $($('ul#menus > li > ul').get(2));
var render_toc = function() {
// remove existing toc render
view_menu.find('.toc').remove();
view_menu.append('<hr class="toc" />');
IPython.notebook.get_cells().forEach(function(cell, cell_num) {
if(cell.cell_type !== 'heading') return;
var text = cell.get_text();
if(cell.get_level() > 1) {
text = '&#x2514;' + text;
}
for(var i=1; i<cell.get_level(); i++) {
text = '&nbsp' + text;
}
var mi = $('<li class="toc"><a href="#">'+text+'</a></li>');
view_menu.append(mi);
mi.click(function() {
console.log(cell_num);
IPython.notebook.scroll_to_cell(cell_num, 250);
});
});
view_menu.data('menu').refresh();
};
$([IPython.events]).on('selected_cell_type_changed.Notebook', function (event, data) {
render_toc();
});
render_toc();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment