<commit_before>"""Table-of-contents magic
for IPython Notebook

Just do:

%load_ext nbtoc
%nbtoc

to get a floating table of contents

All the interesting code, c/o @magican and @nonamenix:
https://gist.github.com/magican/5574556

"""

import io
import os

from IPython.display import display_html, display_javascript

here = os.path.abspath(os.path.dirname(__file__))

with io.open(os.path.join(here, 'nbtoc.js')) as f:
    toc_js = f.read()

with io.open(os.path.join(here, 'nbtoc.html')) as f:
    toc_html = f.read()

def nbtoc(line):
    display_html(toc_html, raw=True)
    display_javascript(toc_js, raw=True)

def load_ipython_extension(ip):
    ip.magics_manager.register_function(nbtoc)
<commit_msg>modified 'nbtoc.py' to auto download the 'js' and 'html' dependencies when running %install_ext<commit_after>"""Table-of-contents magic
for IPython Notebook

Just do:

%load_ext nbtoc
%nbtoc

to get a floating table of contents

All the interesting code, c/o @magican and @nonamenix:
https://gist.github.com/magican/5574556

"""

import io
import os

from IPython.display import display_html, display_javascript

here = os.path.abspath(os.path.dirname(__file__))

if not os.path.isfile(os.path.join(here, 'nbtoc.js')) or not os.path.isfile(os.path.join(here, 'nbtoc.js')):
    import urllib2
    def download(url, fout):
        """ Saves the url file to fout filename """
        filein  = urllib2.urlopen(url)
        fileout = open(fout, "wb")

        while True:
            bytes = filein.read(1*1024) # 1*1024bytes
            fileout.write(bytes)

            if bytes == "": break

        filein.close()
        fileout.close()

    download('https://raw.github.com/minrk/ipython_extensions/master/nbtoc.js', os.path.join(here, 'nbtoc.js'))
    download('https://raw.github.com/minrk/ipython_extensions/master/nbtoc.html', os.path.join(here, 'nbtoc.html'))

with io.open(os.path.join(here, 'nbtoc.js')) as f:
    toc_js = f.read()

with io.open(os.path.join(here, 'nbtoc.html')) as f:
    toc_html = f.read()

def nbtoc(line):
    display_html(toc_html, raw=True)
    display_javascript(toc_js, raw=True)

def load_ipython_extension(ip):
    ip.magics_manager.register_function(nbtoc)

{'fraction_non_alphanumeric': 0.1338475499092559, 'fraction_numerical': 0.011796733212341199, 'mean_word_length': 3.8675496688741724, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6354153', 'n_tokens_mistral': 847, 'n_tokens_neox': 817, 'n_words': 174}