在命令行上使用带有jupyter nbconvert v5.3.1的自定义预处理器

问题描述 投票:0回答:1

我正在尝试执行我在命令行上编写的名为RemoveCellsWithNoTags的自定义预处理器。在documentation之后,这是我的尝试命令

jupyter nbconvert --Exporter.preprocessors=["custompreprocessor.RemoveCellsWithNoTags"] --to script mynotebook.ipynb

这给了我以下错误

zsh: no matches found: --Exporter.preprocessors=[custompreprocessor.RemoveCellsWithNoTags]

标准命令工作正常

jupyter nbconvert --to script mynotebook.ipynb

为了完整性,这里是我的custompreprocessor.py文件中的代码。

from nbconvert.preprocessors import Preprocessor

class RemoveCellsWithNoTags(Preprocessor):

    def preprocess(self, notebook, resources):
        notebook.cells = [cell for cell in notebook.cells if 'tags' in cell.metadata]
        return notebook, resources

Update #1 - Workaround using a config file

我已经设法使用配置文件,但这对我来说并不理想,它正在工作。

nb_convert_config.py文件内容

c = get_config()

c.NbConvertApp.notebooks = ['mynotebook.ipynb']
c.NbConvertApp.export_format = 'python'
c.Exporter.preprocessors = ['custompreprocessor.RemoveCellsWithNoTags']

然后命令变为

jupyter nbconvert --config nbconvert_config.py
jupyter-notebook jupyter nbconvert
1个回答
0
投票

你可能只需要从你的shell中逃脱[](似乎是zsh):

jupyter nbconvert \
  --Exporter.preprocessors=\["custompreprocessor.RemoveCellsWithNoTags"\] \
  --to script mynotebook.ipynb

线索在您收到的错误消息中

zsh: no matches found: --Exporter.preprocessors=[custompreprocessor.RemoveCellsWithNoTags]

错误消息来自shell - 而不是jupyter-nbconvert

© www.soinside.com 2019 - 2024. All rights reserved.