【问题标题】:Right place to put custom nbconvert templates放置自定义 nbconvert 模板的正确位置
【发布时间】:2018-07-02 13:16:13
【问题描述】:

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

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

目前我找到的唯一地方是python包所在的文件夹:

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

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

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

【问题讨论】:

    标签: python jupyter-notebook jinja2 nbconvert


    【解决方案1】:

    您需要通过创建jupyter_nbconvert_config.py 文件并将其存储在~/.jupyter 中来告诉nbconvert 查找您的模板。

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

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

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

    【讨论】:

    • 知道如何在 nbconvert 6+ 中执行此操作吗?我有tried everything I can think of,但我似乎无法让它工作。任何帮助将不胜感激!
    【解决方案2】:

    来自docs

    保存自定义模板以便 nbconvert 可以全局访问它们的推荐位置是您的 jupyter 数据目录:

    • 分享/jupyter

      • nb 转换
        • 模板
          • html
          • 乳胶

    交替

    from jupyter_core.paths import jupyter_path
    print(jupyter_path('nbconvert','templates'))
    

    【讨论】:

      【解决方案3】:

      我在将nbconvert 安装到自定义位置时遇到了这个问题:

      pip install --target=/foooooo/baaaaar nbconvert
      

      您只需要设置一个JUPYTER_PATH environment variable

      JUPYTER_PATH=/foooooo/baaaaar/share/jupyter
      

      【讨论】:

        【解决方案4】:

        除了编辑jupyter_nbconvert_config.py,您还可以编辑jupyter_nbconvert_config.json。首先断言~/.jupyter 位于jupyter --path 的配置路径中。然后在jupyter_nbconvert_config.json 中插入一个模板目录。我添加了一个子文件夹custome_templates 到我的:

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

        然后nbconvert --template mytemplate Untitiled.ipynb 拿起我的模板。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-09-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-28
          • 1970-01-01
          相关资源
          最近更新 更多