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: start h1 numbering at a predefined counter #1245

Closed
fmaussion opened this issue Feb 22, 2018 · 11 comments
Closed

TOC2: start h1 numbering at a predefined counter #1245

fmaussion opened this issue Feb 22, 2018 · 11 comments

Comments

@fmaussion
Copy link

Sorry if this has been asked before: is there a way to start the numbering of the h1 level at any other number than 1?

I'd like to do so because my h1 headers are titles of "chapters", as defined by the bookbook tool.

@jcb91
Copy link
Member

jcb91 commented Feb 22, 2018

Hmm. So if I understand correctly you want the h1 heading in a notebook to be say number 2, because it follows an h1 in another notebook? I don't think there is a way to do this so far, but I expect it would be relatively easy to implement as a feature the ability to manually set the starting number, if it'd be useful...

@fmaussion
Copy link
Author

Thanks for the quick reply! Yes, I would find it very useful indeed. But I'm not familiar at all with the development of nbextensions so I'm afraid I can't really help :(

@fmaussion
Copy link
Author

Actually, now that I think of it a bit more, the easiest way for this kind of structure might be to skip h1 headers altogether (which is already an option as I can see).

I still think this would be a useful feature but maybe you don't have to put it on top of your priority list...

@fmaussion
Copy link
Author

fmaussion commented Feb 22, 2018

For the context: I use toc2 essentially for it's ability to generate TOCs automatically, then use bookbook to generate html outputs.

The problems I see with the numbering is that since the titles are not automatically numbered in the html output, the toc is the only listing containing section numbers. Since I guess this is not something easily done on the TOC2 side, I might skip numbering altogether...

@jfbercher
Copy link
Member

Do you mean something like that?

<h1> something </h1> --> no number something
<h2> something </h2> --> 1.1 something
<h2> something </h2> --> 1.2 something
<h3> something </h3> --> 1.2.1 something
<h1> something else </h1> --> no number something else
<h2> something </h2> --> 2.1 something else
<h2> something </h2> --> 2.2 something else
<h3> something </h3> --> 2.2.1 something else

@fmaussion
Copy link
Author

In an ideal world, I would like to have the following feature:

In the notebooks:

01-Notebook.ipynb
   # Notebook 1 long title
   ## Notebook 1 - Section 1
   ### Notebook 1 - Section 1 - Subsection 1

02-Notebook.ipynb
  <start counter notebook at 2>
   # Notebook 2 long title
   ## Notebook 2 - Section 1
   ### Notebook 2 - Section 1 - Subsection 1

Rendered in html output as:

in 01-Notebook.html:
   ToC:
   1.1 Notebook 1 - Section 1
   1.1.1 Notebook 1 - Section 1 - Subsection 1

in 02-Notebook.html:
   ToC:
   2.1 Notebook 2 - Section 1
   2.1.1 Notebook 2 - Section 1 - Subsection 1

Sorry if it's not clear, it's difficult to describe ;)

@jfbercher
Copy link
Member

jfbercher commented Feb 22, 2018

Just see the two last comments.

a) I see what you want for numbering
It is probably possible to start numbering at a given number by adding a

lbl_ary[min_lvl] = some_number //read from configuration or cell data

before L567 in toc2.js and altering L567 with a +1
b) Numbering in generated html files use the very same javascript that we use in the livenotebook. As I see, since you use bookbook for converting, the relevant javascript is not included in the generated files so that there is no numering at all. Perhaps that this can be done by pointing to the toc2.tpl template rather than the default (probably full.tpl). For that add

    def _template_file_default(self):
        return 'toc2'

in the MyHTMLExporter definition of (your version of) file html.py in the bookbook package

@fmaussion
Copy link
Author

Thanks a lot for your help! Starting with problem b) first: doing as you suggest I get a jinja2.exceptions.TemplateNotFound: toc2 error:

Traceback (most recent call last):
  File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/mowglie/.pyvirtualenvs/py3/lib/python3.5/site-packages/bookbook/html.py", line 98, in <module>
    main()
  File "/home/mowglie/.pyvirtualenvs/py3/lib/python3.5/site-packages/bookbook/html.py", line 95, in main
    convert_directory(args.source_dir, args.output_dir)
  File "/home/mowglie/.pyvirtualenvs/py3/lib/python3.5/site-packages/bookbook/html.py", line 79, in convert_directory
    convert(nbfile, output_dir)
  File "/home/mowglie/.pyvirtualenvs/py3/lib/python3.5/site-packages/bookbook/html.py", line 63, in convert
    output, resources = exporter.from_filename(str(source_path))
  File "/home/mowglie/.pyvirtualenvs/py3/lib/python3.5/site-packages/nbconvert/exporters/exporter.py", line 174, in from_filename
    return self.from_file(f, resources=resources, **kw)
  File "/home/mowglie/.pyvirtualenvs/py3/lib/python3.5/site-packages/nbconvert/exporters/exporter.py", line 192, in from_file
    return self.from_notebook_node(nbformat.read(file_stream, as_version=4), resources=resources, **kw)
  File "/home/mowglie/.pyvirtualenvs/py3/lib/python3.5/site-packages/nbconvert/exporters/html.py", line 85, in from_notebook_node
    return super(HTMLExporter, self).from_notebook_node(nb, resources, **kw)
  File "/home/mowglie/.pyvirtualenvs/py3/lib/python3.5/site-packages/nbconvert/exporters/templateexporter.py", line 295, in from_notebook_node
    output = self.template.render(nb=nb_copy, resources=resources)
  File "/home/mowglie/.pyvirtualenvs/py3/lib/python3.5/site-packages/nbconvert/exporters/templateexporter.py", line 111, in template
    self._template_cached = self._load_template()
  File "/home/mowglie/.pyvirtualenvs/py3/lib/python3.5/site-packages/nbconvert/exporters/templateexporter.py", line 266, in _load_template
    return self.environment.get_template(template_file)
  File "/home/mowglie/.pyvirtualenvs/py3/lib/python3.5/site-packages/jinja2/environment.py", line 830, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/home/mowglie/.pyvirtualenvs/py3/lib/python3.5/site-packages/jinja2/environment.py", line 804, in _load_template
    template = self.loader.load(self, name, globals)
  File "/home/mowglie/.pyvirtualenvs/py3/lib/python3.5/site-packages/jinja2/loaders.py", line 408, in load
    raise TemplateNotFound(name)
jinja2.exceptions.TemplateNotFound: toc2

Replacing 'toc2' with the path to the actual template file did not help.

@jfbercher
Copy link
Member

This shall also be added in MyHTMLExporter class to update the search path. Or copy toc2.tpl in current directory; usually it is in the teamplate search path

    from traitlets.config import Config

    @property
    def default_config(self):
        c = Config()
        #  import here to avoid circular import
        from jupyter_contrib_nbextensions.nbconvert_support import (
            templates_directory)
        c.merge(super(MyHTMLExporter, self).default_config)

        c.TemplateExporter.template_path = [
            '.',
            templates_directory(),
        ]
        return c

@fmaussion
Copy link
Author

Brilliant! Worked like a charm, thanks a lot. Will talk with the developer of bookbook in order to add this option to the project.

For problem a) we'll have to wait a little bit: I have zero experience in javascript and this looks like a more complicated endeavor.

@fmaussion
Copy link
Author

Closed via #1252

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants