在命令行

时间:2018-03-07 16:43:25

标签: jupyter-notebook jupyter nbconvert

我正在尝试在命令行上执行我编写的名为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

更新#1 - 使用配置文件的解决方法

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

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

2 个答案:

答案 0 :(得分:0)

您可能只需要从外壳中逸出[](看来是 zsh ):

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

提示是您收到的错误消息

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

错误消息来自外壳程序-不是 jupyter-nbconvert

答案 1 :(得分:0)

语法如下:

preprocess.py

from nbconvert.preprocessors import Preprocessor

class RemoveCellsWithNoTags(Preprocessor):
    def preprocess(self, notebook, resources):
        executable_cells = []
        for cell in notebook.cells:
            if cell.metadata.get('tags'):
                if "skip" in cell.metadata.get('tags'):
                    continue
            executable_cells.append(cell)
        notebook.cells = executable_cells
        return notebook, resources

然后导出笔记本:

jupyter nbconvert --Exporter.preprocessors=[\"preprocess.RemoveCellsWithNoTags\"] getting-started-keras.ipynb