【问题标题】:Remove cells from Jupyter Notebook with nbconvert使用 nbconvert 从 Jupyter Notebook 中删除单元格
【发布时间】:2018-10-16 11:56:19
【问题描述】:

How to hide one specific cell (input or output) in IPython Notebook? 中提到的建议不起作用。

在 Windows 上,我执行以下操作

jupyter nbconvert a.ipynb --TagRemovePreprocessor.remove_cell_tags="{'remove_cell'}"

但出现错误

traitlets.traitlets.TraitError: The 'remove_cell_tags' trait of a TagRemovePreprocessor instance must be a set, but a value of type 'unicode' (i.e. u"{'remove_cell'}") was specified.

我也试过'{"remove_cell"}'

我正在使用 nbconvert 5.4.0

任何想法如何做到这一点?

【问题讨论】:

  • 你找到解决方案了吗?

标签: jupyter nbconvert


【解决方案1】:

您需要在调用 TagRemovePreprocessor 之前启用它。

下面的代码显示了如何启用它以及如何将您的标签作为一个列表括起来,以便您可以根据需要排除多个标签。要排除单个标签,只需将一个元素放入列表中,例如 ['remove_cell']。

如果要转换为 html,则不需要参数 --to html(因为 html 是默认值)。如果你想转成python,比如把--to html改成--to python

jupyter nbconvert a.ipynb --TagRemovePreprocessor.enabled=True --TagRemovePreprocessor.remove_cell_tags="['remove_cell', 'other_tag_to_remove']" --to html

请注意,TagRemovePreprocessor 仅在 nbconvert 5.3 及更高版本中可用:https://nbconvert.readthedocs.io/en/latest/changelog.html?highlight=TagRemovePreprocessor

【讨论】:

  • 我需要用单引号而不是双引号将 remove_cell_tags 值括起来,以使其在 Powershell 中的 Windows 10 上工作。
【解决方案2】:

需要一些额外的引用才能工作:

--TagRemovePreprocessor.remove_cell_tags={\"remove_cell\"}.

但是请注意,noteboot 到笔记本转换的持续问题 - 在这种情况下,预处理器(包括标签删除)似乎不会运行。在这个 SO 问题中查看更多信息:

jupyter nbconvert --to notebook not excluding raw cells

更新:未在 Windows 上测试,仅在 Linux 上测试

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-13
    • 2020-11-09
    • 2016-09-22
    • 2019-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多