微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Jupyter Lab 或 Jupyter Notebook 中隐藏单元格和输入

如何解决如何在 Jupyter Lab 或 Jupyter Notebook 中隐藏单元格和输入

运行 Jupyter Lab 3.0.11 版

当转换为 HTML 和/或 PDF 时,我想在 Jupyter Notebook 和/或 Jupyter Lab 中隐藏或删除单元格,和/或隐藏单元格输入和/或隐藏单元格输出。而且,我想选择要删除和保留哪些单元格/输入/输出

我正在尝试使用 Jupyter Lab 的 celltags 扩展程序,但它不起作用。

这是带有“remove-input”单元格标签的笔记本(更新:不再安装 celltags 扩展,但结果相同):

enter image description here

而且,这是它在未删除输入的情况下创建的 HTML 文档:

enter image description here

通过单击旁边的选项卡来隐藏笔记本中的单元格并没有转换为导出的 HTML 文档中的隐藏单元格。

如您所见,我已尝试使用 TagRemovePreprocessor,但这只会引发错误,即即使我已安装导入的所有内容,也未找到该模块。我尝试了一些修复方法,但都没有奏效,但如果我不需要,我真的不想给那头牦牛剃毛。

我宁愿单元格标签正常工作,就像在 R Studio 降价文档中一样。在这一点上,将我的笔记本复制并粘贴到 RMD 并使用 Python 内核运行它可能会更容易,因为 RMD 具有内置的小部件和简单的标签功能强大。

综上所述,欢迎使用任何 Jupyter Notebook 或 Jupyter Lab 解决方案,特别是如果它只是一个实际有效的简单切换。

谢谢!

附言这是注释掉的第一个单元格中的代码,以及它的错误

from traitlets.config import Config
import nbformat as nbf
from nbconvert.exporters import HTMLExporter
from nbconvert.preprocessors import TagRemovePreprocessor

c = Config()
c.TagRemovePreprocessor.enabled=True
# Configure our tag removal
c.TagRemovePreprocessor.remove_cell_tags = ("remove_cell",)
c.TagRemovePreprocessor.remove_all_outputs_tags = ('remove_output',)
c.TagRemovePreprocessor.remove_input_tags = ('remove_input',)

# Configure and run out exporter
c.HTMLExporter.preprocessors = ["TagRemovePreprocessor"]
# c.HTMLExporter.preprocessors = ["nbf.preprocessors.TagRemovePreprocessor"]
HTMLExporter(config=c).from_filename("./test.ipynb")
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-4-2a221250acbf> in <module>
     14 c.HTMLExporter.preprocessors = ["TagRemovePreprocessor"]
     15 # c.HTMLExporter.preprocessors = ["nbf.preprocessors.TagRemovePreprocessor"]
---> 16 HTMLExporter(config=c).from_filename("./test.ipynb")

~\anaconda3\envs\py3\lib\site-packages\nbconvert\exporters\templateexporter.py in __init__(self,config,**kw)
    323             Template to use when exporting.
    324         """
--> 325         super().__init__(config=config,**kw)
    326 
    327         self.observe(self._invalidate_environment_cache,~\anaconda3\envs\py3\lib\site-packages\nbconvert\exporters\exporter.py in __init__(self,**kw)
    112         super().__init__(config=with_default_config,**kw)
    113 
--> 114         self._init_preprocessors()
    115 
    116 

~\anaconda3\envs\py3\lib\site-packages\nbconvert\exporters\templateexporter.py in _init_preprocessors(self)
    488 
    489     def _init_preprocessors(self):
--> 490         super()._init_preprocessors()
    491         conf = self._get_conf()
    492         preprocessors = conf.get('preprocessors',{})

~\anaconda3\envs\py3\lib\site-packages\nbconvert\exporters\exporter.py in _init_preprocessors(self)
    264         # Load user-specified preprocessors.  Enable by default.
    265         for preprocessor in self.preprocessors:
--> 266             self.register_preprocessor(preprocessor,enabled=True)
    267 
    268 

~\anaconda3\envs\py3\lib\site-packages\nbconvert\exporters\exporter.py in register_preprocessor(self,preprocessor,enabled)
    225             # Preprocessor is a string,import the namespace and recursively call
    226             # this register_preprocessor method
--> 227             preprocessor_cls = import_item(preprocessor)
    228             return self.register_preprocessor(preprocessor_cls,enabled)
    229 

~\anaconda3\envs\py3\lib\site-packages\traitlets\utils\importstring.py in import_item(name)
     36     else:
     37         # called with un-dotted string
---> 38         return __import__(parts[0])

ModuleNotFoundError: No module named 'TagRemovePreprocessor'

我已经安装了 nbconvert,并且 TagRemovePreprocessor 不会在其导入语句中抛出错误

我可以运行 c.HTMLExporter.preprocessors = ["nbf.preprocessors.TagRemovePreprocessor"] 而不是 c.HTMLExporter.preprocessors = ["TagRemovePreprocessor"],并且从同一行抛出相同的错误,只是它说没有名为 nbf 的模块。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。