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

Template inheritance from another template in the same directory? #1441

Open
BoPeng opened this issue Oct 8, 2020 · 5 comments
Open

Template inheritance from another template in the same directory? #1441

BoPeng opened this issue Oct 8, 2020 · 5 comments
Labels

Comments

@BoPeng
Copy link
Contributor

BoPeng commented Oct 8, 2020

I am trying to convert my previous templates to the new template system. I have

sos-full/
    conf.json (base_template: "classic")
    index.html.j2. ( extends "classic/index.html.j2")
sos-cm/
    conf.json (base_template: "sos-full")
    index.html.j2 (extends "sos-full/index.html.j2")

command

jupyter nbconvert source.ipynb --to html \
--TemplateExporter.extra_template_basedirs=/path/to/templates \
--template sos-full

works but

jupyter nbconvert source.ipynb --to html \
--TemplateExporter.extra_template_basedirs=/path/to/templates \
--template sos-cm

yields an error

    {% extends 'sos-full/index.html.j2' %}
jinja2.exceptions.TemplateNotFound: sos-full/index.html.j2

Did I do anything wrong?

@BoPeng BoPeng added the question label Oct 8, 2020
@MSeal
Copy link
Contributor

MSeal commented Oct 9, 2020

Huh that is odd. I think it's a bug that the custom template name in the middle is not recognized, but there is a work-around to this.

When you have a template in the base_template path you can refer to it without the path in the name. e.g. extends "index.html.j2", but the name of the file has to be unique or you can get into an infinite loop loading the wrong file with the same name. So to solve this move index.html.j2 in sos-full to sos-base.html.j2 (or some other unique name) and make a new index that just extends extends "sos-base.html.j2" and put the same extension in sos-cm's index.html.j2. This is how classic provides a base that both it and other templates can extend independently.

@BoPeng
Copy link
Contributor Author

BoPeng commented Oct 9, 2020

Let me try. I did get infinite loops etc when I tried other ways.

@BoPeng
Copy link
Contributor Author

BoPeng commented Nov 18, 2020

@MSeal Thank you very much for the suggestion. It works!

Just to help others who might have the same problem, the new layout is, as demonstrated here,

sos-full/
    conf.json (base_template: "classic")
    sos-full.html.j2, ( extends "classic/index.html.j2") 
    index.html.j2. ( extends "sos-full.html.j2")
sos-cm/
    conf.json (base_template: "sos-full")
    sos-cm.html.j2, ( extends "sos-full.html.j2") 
    index.html.j2 (extends "sos-cm.html.j2")

Two remaining questions though:

  1. How do I supply two paths to option --TemplateExporter.extra_template_basedirs? I tried
jupyter nbconvert test.ipynb --to html --TemplateExporter.extra_template_basedirs=path1:path2

and

jupyter nbconvert test.ipynb --to html --TemplateExporter.extra_template_basedirs path1 path2

but neither works.

EDIT:

The solution to this problem is

jupyter nbconvert test.ipynb --to html --TemplateExporter.extra_template_basedirs=path1 --TemplateExporter.extra_template_basedirs path1=path2
  1. In the old style I have a bunch of "parts", and in a directory structure like
parts/
    toc.tpl
sos-cm-toc.tpl
sos-report-toc.tpl 

I could use the parts by using

 {% import 'parts/toc.tpl' as toc %}

in both sos-cm-toc.tpl and sos-report-toc.tpl.

With the new style I have to keep the toc.tpl under both directories. (e.g. parts under sos-report-toc and parts under sos-cm-toc). Is there anyway I can share the toc.tpl between templates?

@JBlaschke
Copy link

@BoPeng until this bug is fixed, I was able to use symlinks as a workaround. I know this is not ideal, but at least it lets me get something to work.

@JBlaschke
Copy link

By the way, is there some progress on this issue?

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

No branches or pull requests

3 participants