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
jupyter nbconvert notebook.ipynb --execute --to pdf
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,
jupyter nbconvert --clear-output notebook.ipynb
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:
jupyter nbconvert --ClearOutputPreprocessor.enabled=True --inplace notebook.ipynb
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.