This extension does not work with IPython 3+.

This commit is contained in:
Roger Labbe 2015-04-19 09:36:46 -07:00
parent 0086fcd201
commit dda63fb329
2 changed files with 0 additions and 153 deletions

View File

@ -1,74 +0,0 @@
console.log("Section numbering...");
function number_sections(threshold) {
var h1_number = 0;
var h2_number = 0;
if (threshold === undefined) {
threshold = 2; // does nothing so far
}
var cells = IPython.notebook.get_cells();
for (var i=0; i < cells.length; i++) {
var cell = cells[i];
if (cell.cell_type !== 'heading') continue;
var level = cell.level;
if (level > threshold) continue;
if (level === 1) {
h1_number ++;
var h1_element = cell.element.find('h1');
var h1_html = h1_element.html();
console.log("h1_html: " + h1_html);
var patt = /^[0-9]+\.\s(.*)/; // section number at start of string
var title = h1_html.match(patt); // just the title
if (title != null) {
h1_element.html(h1_number + ". " + title[1]);
}
else {
h1_element.html(h1_number + ". " + h1_html);
}
h2_number = 0;
}
if (level === 2) {
h2_number ++;
var h2_element = cell.element.find('h2');
var h2_html = h2_element.html();
console.log("h2_html: " + h2_html);
var patt = /^[0-9]+\.[0-9]+\.\s/;
var result = h2_html.match(patt);
if (result != null) {
h2_html = h2_html.replace(result, "");
}
h2_element.html(h1_number + "." + h2_number + ". " + h2_html);
}
}
}
number_sections();
// $([IPython.evnts]).on('create.Cell', number_sections);
$([IPython.events]).on('selected_cell_type_changed.Notebook', number_sections);

View File

@ -1,79 +0,0 @@
"""
IPython Notebook magic command for automatically numbering sections of a notebook "interactively"
Use:
%install_ext https://raw.github.com/dpsanders/ipython_extensions/master/section_numbering/secnum.py
once to install, and then
%load_ext secnum
%secnum
to get automatic section numbering, which is updated when any cell changes type.
(This could, of course, be more efficient.)
This is based, completely and shamelessly, on MinRK's `nbtoc` magic
<https://github.com/minrk/ipython_extensions>
Thanks also to:
- Clayton Davis, for a great explanation of HTML, CSS and JavaScript
- Brian Granger, for a crucial hint about extracting a JQuery object from a notebook cell object
- Fernando Perez, for an inspiring talk at SciPy 2013 (and, of course, for creating IPython)
"""
import io
import os
import urllib3
from IPython.display import display_html, display_javascript
here = os.path.abspath(os.path.dirname(__file__))
secnum_js = ""
def download(fname, redownload=False):
"""download a file
if redownload=False, the file will not be downloaded if it already exists.
"""
dest = os.path.join(here, fname)
if os.path.exists(dest) and not redownload:
return
url = 'https://raw.github.com/dpsanders/ipython_extensions/master/section_numbering/' + fname
print("Downloading %s to %s" % (url, dest))
filein = urllib3.urlopen(url)
fileout = open(dest, "wb")
chunk = filein.read(1024)
while chunk:
fileout.write(chunk)
chunk = filein.read(1024)
filein.close()
fileout.close()
def load_file(fname, redownload=False):
"""load global variable from a file"""
download(fname, redownload)
with io.open(os.path.join(here, fname)) as f:
globals()[fname.replace('.', '_')] = f.read()
load_file('secnum.js')
def secnum(line):
"""add a table of contents to an IPython Notebook"""
display_javascript(secnum_js, raw=True)
def update_secnum(line):
"""download the latest version of the nbtoc extension from GitHub"""
download('secnum.py', True)
download('secnum.js', True)
get_ipython().extension_manager.reload_extension("secnum")
def load_ipython_extension(ip):
ip.magics_manager.register_function(secnum)
ip.magics_manager.register_function(update_secnum)