Aar*_*ffo 4 python jupyter-notebook nbconvert
为 nbconvert 指定自定义模板路径的正确方法是什么?
在 nbonvert 版本 6 下,模板现在是一个包含多个文件的目录。这些模板可以存在于任意数量的位置,具体取决于平台。
树莓派:
['/home/pi/.local/share/jupyter/nbconvert/templates', '/usr/local/share/jupyter/nbconvert/templates', '/usr/share/jupyter/nbconvert/templates']
Run Code Online (Sandbox Code Playgroud)
带有 Pyenv 的 OS X:
['/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']
Run Code Online (Sandbox Code Playgroud)
我正在尝试在几个不同的平台上同步我的模板,并想指定一个自定义位置。
2 年前的这篇文章似乎是正确的,但似乎适用于 nbconvert 的 V5——该方法已将名称从 更改template_path为template_paths。
我已经尝试使用上面链接中建议的解决方案,使用我知道放置在已知位置之一时可以工作的模板。尝试按照建议指定自定义位置时,我最终遇到此错误:
jinja2.exceptions.TemplateNotFound: null.j2
我怀疑通过将路径设置为/path/to/.jupyter/templates/my_template/,我完全覆盖了所有其他模板位置并丢失了null.j2我的模板扩展的模板。我在最后包含了我的模板,因为它有一些导致这种情况的错误。
V6 配置文件的文档也没有太大帮助:
TemplateExporter.template_paths : List
Default: ['.']
No description
Run Code Online (Sandbox Code Playgroud)
和
PythonExporter.template_paths : List
Default: ['.']
No description
Run Code Online (Sandbox Code Playgroud)
从 2019 年 5 月开始,在 Git Repo 上有很长的讨论这个问题,但我不太明白最终的结论是什么。
我的自定义 Python 模板:
{%- 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%}
Run Code Online (Sandbox Code Playgroud)
Git Repo 上的问题 #1428包含此解决方案的基础。
从头开始/最近从 v5 升级到 v6,请执行以下操作:
~/.jupyter$ jupyter nbconvert --generate-config
Run Code Online (Sandbox Code Playgroud)
~/.jupyter/jupyter_nbconvert_config.py以添加以下几行: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]
Run Code Online (Sandbox Code Playgroud)
将模板添加到~/my/custom/templates目录
/my/custom/templates/foo_template)conf.json和index.py.j2文件。索引是实际的模板。请参阅下面的示例运行 nbconvert:
$ jupyter nbconvert --to python --template my_custom_template foo.ipynb
Run Code Online (Sandbox Code Playgroud)
conf.json 基本示例
{
"base_template": "base",
"mimetypes": {
"text/x-python": true
}
}
Run Code Online (Sandbox Code Playgroud)
index.py.j2 例子
{%- 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() }}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1400 次 |
| 最近记录: |