Kal*_*rly 6 export cell hide jupyter-notebook jupyter-lab
运行 Jupyter Lab 版本 3.0.11
我想在转换为 HTML 和/或 PDF 时隐藏或删除 Jupyter Notebook 和/或 Jupyter Lab 中的单元格、和/或隐藏单元格输入和/或隐藏单元格输出。而且,我想选择要删除哪些单元格/输入/输出以及保留哪些单元格/输入/输出。
我正在尝试 Jupyter Lab 的 celltags 扩展,但它不起作用。
这是带有“remove-input”单元标签的笔记本(更新:不再安装 celltags 扩展,但结果是相同的):
并且,这是它创建的 HTML 文档,其中输入未删除:
通过单击旁边的选项卡隐藏笔记本中的单元格不会转换为导出的 HTML 文档中的隐藏单元格。
正如您所看到的,我尝试使用 TagRemovePreprocessor,但这只会引发找不到模块的错误,即使我已经安装了导入的所有内容。我尝试了一些修复方法,但没有成功,但如果不需要的话,我真的不想给那头牦牛剃毛。
我宁愿单元格标签正常工作,就像在 R Studio markdown 文档中一样。此时,将我的笔记本复制并粘贴到 RMD 并使用 Python 内核运行它可能会更容易,因为 RMD 内置了该小部件和简单标签并可用。
话虽如此,任何 Jupyter Notebook 或 Jupyter Lab 解决方案都是受欢迎的,特别是如果它只是一个实际有效的简单切换。
谢谢!
PS 这是注释掉的第一个单元格中的代码以及其中的错误:
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")
Run Code Online (Sandbox Code Playgroud)
---------------------------------------------------------------------------
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, config, **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'
Run Code Online (Sandbox Code Playgroud)
我已经安装了nbconvert,并且TagRemovePreprocessor在其导入语句中没有抛出错误。
我可以运行c.HTMLExporter.preprocessors = ["nbf.preprocessors.TagRemovePreprocessor"]而不是c.HTMLExporter.preprocessors = ["TagRemovePreprocessor"],并且我从同一行抛出相同的错误,除了它说没有名为 的模块nbf。
| 归档时间: |
|
| 查看次数: |
3312 次 |
| 最近记录: |