为 jupyter nbconvert V6 指定自定义模板路径的正确方法是什么? [英] What is the proper way to specify a custom template path for jupyter nbconvert V6?
问题描述
为 nbconvert 指定自定义模板路径的正确方法是什么?
What is the proper way to specify a custom template path for nbconvert?
在 nbonvert 版本 6 下,模板现在是一个包含多个文件的目录.这些模板可以存在于任意数量的位置,具体取决于平台.
Under nbonvert version 6, templates are now a directory with several files. Those templates can live in any number of locations depending on the platform.
树莓派:
['/home/pi/.local/share/jupyter/nbconvert/templates', '/usr/local/share/jupyter/nbconvert/templates', '/usr/share/jupyter/nbconvert/templates']
带有 Pyenv 的 OS X:
OS X with Pyenv:
['/Users/ac/Library/Jupyter/nbconvert/templates', '/Users/ac/.pyenv/versions/3.8.5/Python.framework/Versions/3.8/share/jupyter/nbconvert/templates', '/usr/local/share/jupyter/nbconvert/templates', '/usr/share/jupyter/nbconvert/templates']
我正在尝试在多个不同平台上同步我的模板,并想指定一个自定义位置.
I'm trying to sync my templates over several different platforms and would like to specify a custom location.
这篇帖子 2 年前的帖子似乎是正确的,但似乎应用于 nbconvert V5 -- 该方法已将名称从 template_path
更改为 template_paths
.
This post from 2 years ago seems correct, but appears to apply to V5 of nbconvert -- the method has changed names from template_path
to template_paths
.
我已经尝试了上面链接中建议的解决方案,使用我知道放置在已知位置之一时可以工作的模板.尝试按照建议指定自定义位置时,我最终遇到此错误:
I've tried the solution suggested in the link above using a template that I know works when placed in one of the known locations. I end up with this error when trying to specify a custom location as suggested:
jinja2.exceptions.TemplateNotFound: null.j2
我怀疑通过将路径设置为 /path/to/.jupyter/templates/my_template/
,我完全覆盖了所有其他模板位置并丢失了 null.j2我的模板扩展的代码>模板.我在最后包含了我的模板,以防万一它有一些导致此问题的错误.
I suspect that by setting the path to /path/to/.jupyter/templates/my_template/
, I completely override all the other template locations and lose the null.j2
template that my template extends. I've included my template at the end on the off chance it has some errors that are causing this.
V6 配置文件的文档 也没有太大帮助:>
The docs for V6 config files are not much help either:
TemplateExporter.template_paths : List
Default: ['.']
No description
和
PythonExporter.template_paths : List
Default: ['.']
No description
有一个 2019 年 5 月的长线程在 Git Repo 上讨论了这个问题,但我无法完全理解最终结论是什么.
There's a long thread from May 2019 discussing this on the Git Repo, but I can't quite make sense of what the ultimate conclusion was.
我的自定义 Python 模板:
My custom Python template:
{%- extends 'null.j2' -%}
## set to python3
{%- block header -%}
#!/usr/bin/env python3
# coding: utf-8
{% endblock header %}
## remove cell counts entirely
{% block in_prompt %}
{% if resources.global_content_filter.include_input_prompt -%}
{% endif %}
{% endblock in_prompt %}
## remove markdown cells entirely
{% block markdowncell %}
{% endblock markdowncell %}
{% block input %}
{{ cell.source | ipython2python }}
{% endblock input %}
## remove magic statement completely
{% block codecell %}
{{'' if "get_ipython" in super() else super() }}
{% endblock codecell%}
推荐答案
关于 Git 的问题 #1428Repo 包含此解决方案的基础.
Issue #1428 on the Git Repo contains the basis for this solution.
从头开始/最近从 v5 升级到 v6,请执行以下操作:
From scratch/recent upgrade from v5 to v6 do the following:
- 在
~/.jupyter
中为 V6 生成当前和最新的配置文件
- Generate a current and up-to-date configuration file for V6 in
~/.jupyter
$ jupyter nbconvert --generate-config
- 编辑配置文件
~/.jupyter/jupyter_nbconvert_config.py
,添加以下几行:
- Edit the configuration file
~/.jupyter/jupyter_nbconvert_config.py
to add the following lines:
from pathlib import Path
# set a custom path for templates in
c.TemplateExporter.extra_template_basedirs
my_templates = Path('~/my/custom/templates').expanduser().absolute()
# add the custom path to the extra_template_basedirs
c.TemplateExporter.extra_template_basedirs = [my_templates]
将模板添加到
~/my/custom/templates
目录
- 每个模板都必须在自己的子目录中 (
/my/custom/templates/foo_template
) - 每个模板必须包含一个
conf.json
和index.py.j2
文件.索引是实际的模板.请参阅下面的示例
- Each template must be in its own sub directory (
/my/custom/templates/foo_template
) - Each template must contain a
conf.json
andindex.py.j2
file. the index is the actual template. See below for an example
运行 nbconvert:
run nbconvert:
$ jupyter nbconvert --to python --template my_custom_template foo.ipynb
conf.json
基本示例
conf.json
Basic Example
{
"base_template": "base",
"mimetypes": {
"text/x-python": true
}
}
index.py.j2
示例
index.py.j2
Example
{%- extends 'null.j2' -%}
## set to python3
{%- block header -%}
#!/usr/bin/env python3
# coding: utf-8
{% endblock header %}
## remove cell counts entirely
{% block in_prompt %}
{% if resources.global_content_filter.include_input_prompt -%}
{% endif %}
{% endblock in_prompt %}
## remove markdown cells entirely
{% block markdowncell %}
{% endblock markdowncell %}
{% block input %}
{{ cell.source | ipython2python }}
{% endblock input %}
## remove magic statement completely
{% block codecell %}
{{'' if "get_ipython" in super() else super() }}
这篇关于为 jupyter nbconvert V6 指定自定义模板路径的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!