Jupyter comes with a command line utility jupyter nbconvert that can transform a jupyter notebook into different formats. Furthermore, it has options to tweak the output. For instance, the execute
flag executes the notebook before transforming it while the clear-output
flag is supposed to remove outputs from the notebook. Thus, if you want to execute the notebook notebook.ipynb
and transform it into a pdf file afterwards, you can issue the command
[sourcecode language=”text” title=”Listing 1. Example usage of nbconvert
. ” ]
jupyter nbconvert notebook.ipynb –execute –to pdf
[/sourcecode]
I stumbled upon the following problem when I tried to create a make
target for the SCP project. The make
target should do the following: It should clear the output of a specified notebook, execute it and then export it as a pdf file.
Problem description
In contrast to its purpose, the clear-output
flag does not remove the output from any notebook. Suppose your Jupyter notebook is notebook.ipynb
. Then,
[sourcecode language=”text” title=”Listing 2. Does not work: Using the clear-output
flag. ” ]
jupyter nbconvert –clear-output notebook.ipynb
[/sourcecode]
merely saves the file notebook.ipynb
again. The output remains in the notebook.
Solution
Unfortunately, this still seems to be an open issue. However, there is a more specific version of the command available that does exactly what we want:
[sourcecode language=”text” title=”Listing 3. Works: Using more specific options. ” ]
jupyter nbconvert –ClearOutputPreprocessor.enabled=True –inplace notebook.ipynb
[/sourcecode]
It is not clear to me what the current status of the issue is; in fact, there are recent commits in other projects referencing the issue and the issue itself is labeled as upstream. Apparently, a dependency (traitlets) is causing this bug.