本文介绍了放置自定义 nbconvert 模板的正确位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个自定义 nbconvert 模板,并希望它可以从我启动 nbconvert 实用程序的任何文件夹访问.我应该把我的模板放在哪里?

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?

我在官方文档中找不到任何内容.我已经尝试过 jupyter 配置的常用位置,例如 /usr/share/jupyter~/.local/share/jupyter~/.jupyter,无济于事.

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.

到目前为止我发现的唯一地方是 python 包所在的文件夹:

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

如果我在那里创建 nbconvert/templates/html 目录并将我的模板放入其中,nbconvert --to html --template <my_template_name>... 工作正常.但这是一个丑陋的黑客,每次更新 nbconvert 时我都需要重新做.

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.

似乎我可以为 nbconvert 提供环境变量,但我更愿意避免使用此选项.

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

推荐答案

你需要告诉 nbconvert 通过创建一个 jupyter_nbconvert_config.py 文件并将其存储在 ~/.jupyter.

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

我将它用于 LaTeX——这是我的文件的样子:

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'

假设您的模板扩展了现有模板,您需要在设置 template_path 时包含 '.' 以便它知道在哪里寻找标准模板.

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.

这篇关于放置自定义 nbconvert 模板的正确位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-08 22:16