gpt4 book ai didi

python - 为 jupyter nbconvert V6 指定自定义模板路径的正确方法是什么?

转载 作者:行者123 更新时间:2023-12-03 21:46:00 25 4
gpt4 key购买 nike

为 nbconvert 指定自定义模板路径的正确方法是什么?
在 nbonvert 版本 6 下,模板现在是一个包含多个文件的目录。这些模板可以存在于任意数量的位置,具体取决于平台。
树莓派:

['/home/pi/.local/share/jupyter/nbconvert/templates', '/usr/local/share/jupyter/nbconvert/templates', '/usr/share/jupyter/nbconvert/templates']
带有 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']
我正在尝试在几个不同的平台上同步我的模板,并想指定一个自定义位置。
This post从 2 年前开始似乎是正确的,但似乎适用于 nbconvert 的 V5 -- 该方法已更改名称 template_pathtemplate_paths .
我已经尝试使用上面链接中建议的解决方案,使用我知道放置在已知位置之一时可以工作的模板。尝试按照建议指定自定义位置时,我最终遇到此错误: jinja2.exceptions.TemplateNotFound: null.j2我怀疑是通过将路径设置为 /path/to/.jupyter/templates/my_template/ ,我完全覆盖了所有其他模板位置并丢失了 null.j2我的模板扩展的模板。我在最后包含了我的模板,因为它有一些导致这种情况的错误。
The docs for V6 config files也没有太大帮助:
TemplateExporter.template_paths : List
Default: ['.']

No description

PythonExporter.template_paths : List
Default: ['.']

No description
有一个 long thread from May 2019在 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%}

最佳答案

Issue #1428 on the Git Repo包含此解决方案的基础。
从头开始/最近从 v5 升级到 v6,请执行以下操作:

  • ~/.jupyter 中为 V6 生成当前和最新的配置文件
  • $ jupyter nbconvert --generate-config
  • 编辑配置文件 ~/.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]

  • 将模板添加到 ~/my/custom/templates目录
  • 每个模板都必须在其自己的子目录中 ( /my/custom/templates/foo_template )
  • 每个模板必须包含一个 conf.json index.py.j2文件。索引是实际的模板。请参阅下面的示例

  • 运行 nbconvert:
  • $ jupyter nbconvert --to python --template my_custom_template foo.ipynb
    conf.json基本示例
    {
    "base_template": "base",
    "mimetypes": {
    "text/x-python": true
    }
    }
    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() }}

    关于python - 为 jupyter nbconvert V6 指定自定义模板路径的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64127278/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com