5

I've made a custom nbconvert template and want it to be accessible from any folder where I launch nbconvert utility. Where should I put my template?

I couldn't find anything in official docs. I have already tried usual places for jupyter configs, like /usr/share/jupyter, ~/.local/share/jupyter, ~/.jupyter, to no avail.

The only place I've found so far is the folder where python package lives:

$ pip show nbconvert | grep Location | cut -d" " -f2
/usr/lib/python3.6/site-packages

If I create nbconvert/templates/html directory there and put my template in it, nbconvert --to html --template <my_template_name> ... works fine. But this is an ugly hack which I'll need to re-do every time I update nbconvert.

Seems that I can provide nbconvert with environment variable, but I would prefer to avoid this option.

krvkir
  • 771
  • 7
  • 12

4 Answers4

3

You need to tell nbconvert to look for your template by creating an jupyter_nbconvert_config.py file and storing it in ~/.jupyter.

I use this for LaTeX--here's what my file looks like:

import os
c = get_config()

c.LatexExporter.template_path = ['.', os.path.expanduser('~/.jupyter/templates')]
c.LatexExporter.template_file = 'custom_latex.tplx'

Assuming you template extends an existing one, you need to include '.' when setting template_path so it knows where to look for the standard templates.

badgerm
  • 463
  • 1
  • 4
  • 13
  • Any idea how to do this in nbconvert 6+? I've [tried everything I can think of](https://stackoverflow.com/questions/64127278/what-is-the-proper-way-to-specify-a-custom-template-path-for-jupyter-nbconvert-v), but I can't seem to make it work. Any help would be appreciated! – Aaron Ciuffo Sep 30 '20 at 18:50
3

From the docs.

The recommended place to save custom templates, so that they are globally accessible to nbconvert, is your jupyter data directories:

  • share/jupyter

    • nbconvert
      • templates
        • html
        • latex

Alternately

from jupyter_core.paths import jupyter_path
print(jupyter_path('nbconvert','templates'))
KWx
  • 310
  • 1
  • 10
1

I encountered this problem when installing nbconvert to a custom location using:

pip install --target=/foooooo/baaaaar nbconvert

You just need to set a JUPYTER_PATH environment variable.

JUPYTER_PATH=/foooooo/baaaaar/share/jupyter
tomasbedrich
  • 1,350
  • 18
  • 26
0

As an alternative to editing jupyter_nbconvert_config.py you can also edit jupyter_nbconvert_config.json. First assert that ~/.jupyter is in the config path with jupyter --path. Then insert in jupyter_nbconvert_config.json a template directory. I added a subfolder custome_templates to mine:

{
  "Exporter": {
    "template_path": [
      ".", 
      "/home/moutsopoulosg/miniconda/envs/myenv/lib/python2.7/site-packages/jupyter_contrib_nbextensions/templates",
      "/home/moutsopoulosg/.jupyter/custom_templates"
    ], 
    ...
  },
  "version": 1
}

Then nbconvert --template mytemplate Untitiled.ipynb picks up my template.