Search code examples
makefilejupyternbconvert

jupyter nbconvert --to notebook not excluding raw cells


I'm trying to export notebooks from a ./doc folder to a `./notebook/ folder in the root of my project, but remove all the raw cells where I have rst.

I tried the following:

jupyter nbconvert --to notebook $< --output=$@ --TemplateExporter.exclude_raw=True

I'm doing this in a Makefile (so the $< and $@ are the name of the local notebooks and the name of the notebook once it is moved to the top-level ./notebook dir, respectively).

It runs, but the raw cells are still there in the copies of the notebooks. Is there something I'm missing?


Solution

  • There seems to be due to a bug - when the output format is 'notebook', then nbconverts seems to take a short cut, bypassing preprocessors.

    I've come across at least one more issue on github related to it: https://github.com/jupyter/nbconvert/issues/895

    Here is my workaround.

    1. Create a custom template file custom_notebook.tpl with the following content:
    {% extends 'null.tpl'%}
    
    {%- block body %}
    {{ nb | json_dumps }}
    {% endblock body %}
    
    

    This essentially copies the notebook, but allows preprocessors to run.

    1. In the command line replace --to notebook with --to custom --template=custom_notebook.tpl

    The result will be a valid notebook with filters run on it. I have not tested with --TemplateExporter.exclude_raw=True but it worked with --TagRemovePreprocessor.remove_cell_tags that suffered the same null effect with --to notebook option.