itzy
itzy

Reputation: 11745

Run preprocessor using nbconvert as a library

I would like to run nbconvert with a preprocessor that removes cells marked with the tag "skip". I am able to do this from the command line, but when I try to use the nbconvert API within a notebook I run into problems.

An example

Following the example in the documentation, I get a notebook to work with.

from urllib.request import urlopen

url = 'http://jakevdp.github.com/downloads/notebooks/XKCD_plots.ipynb'
response = urlopen(url).read().decode()

import nbformat
nb = nbformat.reads(response, as_version=4)

I'll modify one cell so it gets skipped in the output.

nb.cells[1].metadata = {'tags': ['skip']}

Command line

Saving the file, and then running nbconvert from the command line:

nbformat.write(nb, 'nb.ipynb')

%%bash
jupyter nbconvert --to latex \
--TagRemovePreprocessor.remove_cell_tags='{"skip"}' \
--TagRemovePreprocessor.enabled=True \
'nb.ipynb'

This works. The output nb.tex file does not contain the cell tagged "skip".

API

Now let's try it using the API instead. First, without any preprocessing:

import nbconvert
latex, _ = LatexExporter().from_notebook_node(nb)
print(latex[:25])

\documentclass[11pt]{arti

Again, no problem. The conversion is working.

Now, trying to use the same preprocessor I used from the command line:

from traitlets.config import Config

c = Config()
c.RemovePreprocessor.remove_cell_tags = ('skip',)
c.LatexExporter.preprocessors = ['TagRemovePreprocessor']

LatexExporter(config=c).from_notebook_node(nb)

This time, I get:

ModuleNotFoundError: No module named 'TagRemovePreprocessor'

As far as I can see, this code matches the code sample in the documentation, except that I'm using the Latex exporter instead of HTML. So why isn't it working?

Upvotes: 2

Views: 1493

Answers (1)

Bitwise
Bitwise

Reputation: 31

For your particular case, I believe you can resolve the issue by changing: c.RemovePreprocessor.remove_cell_tags = ('skip',) -> c.TagRemovePreprocessor.remove_cell_tags = ('skip',)


For the benefit of others that come across this thread as I did by searching

ModuleNotFoundError: No module named 'TagRemovePreprocessor'

There is an open issue with TagRemovePreprocessor that causes all exporters other than the HTMLExporter (and LatexExporter?) to automatically disable this preprocessor.

In my case, I was attempting to use the NotebookExporter and needed to explicitly enable the preprocessor and change the preprocessing level like so:

import json
from traitlets.config import Config
from nbconvert import NotebookExporter
import nbformat

c = Config()
c.TagRemovePreprocessor.enabled=True # Add line to enable the preprocessor
c.TagRemovePreprocessor.remove_cell_tags = ["del_cell"]
c.preprocessors = ['TagRemovePreprocessor'] # Was previously: c.NotebookExporter.preprocessors

nb_body, resources = NotebookExporter(config=c).from_filename('notebook.ipynb')
nbformat.write(nbformat.from_dict(json.loads(nb_body)),'stripped_notebook.ipynb',4)

Upvotes: 3

Related Questions