jupyterlab/jupyterlab-toc

Numbering doesn't show on HTML when sections are added programatically

melovv opened this issue · 0 comments

Hi.
Awesome plugin! Thanks a lot for that.

I'm working with notebooks but numbering is not showing on HTML when sections are added programatically, although they appear in the notebook.

from IPython.display import HTML, Markdown

from notebook import notebookapp
import urllib
import json
import os
import ipykernel

def notebook_path():
    """Returns the absolute path of the Notebook or None if it cannot be determined
    NOTE: works only when the security is token-based or there is also no password
    """
    connection_file = os.path.basename(ipykernel.get_connection_file())
    kernel_id = connection_file.split('-', 1)[1].split('.')[0]

    for srv in notebookapp.list_running_servers():
        try:
            if srv['token']=='' and not srv['password']:  # No token and no password, ahem...
                req = urllib.request.urlopen(srv['url']+'api/sessions')
            else:
                req = urllib.request.urlopen(srv['url']+'api/sessions?token='+srv['token'])
            sessions = json.load(req)
            for sess in sessions:
                if sess['kernel']['id'] == kernel_id:
                    return sess['notebook']['path']
        except:
            pass  # There may be stale entries in the runtime directory 
    return None


for h1 in range(2):
    display(Markdown("# Section: " + str(h1)))
    for h2 in range(2):
        display(Markdown("## Subsection: " + str(h2)))

To export:

nb_path = notebook_path()
!jupyter nbconvert --to html_embed --template toc2 --TemplateExporter.exclude_input=True "$nb_path";

NOTEBOOK:
Screen Shot 2020-01-17 at 11 34 51 AM

HTML OUTPUT:
Screen Shot 2020-01-17 at 11 35 00 AM