Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove_input_tags doesn't seem to be working (in the way I understand it) #2159

Open
TLouf opened this issue Jun 27, 2024 · 1 comment
Open

Comments

@TLouf
Copy link

TLouf commented Jun 27, 2024

Hi, I followed this tutorial to create a script that would remove (parts of) cells based on tags. Surprisingly, works perfectly for removing outputs or whole cells, but doesn't for input. To make sure I understood correctly, TagRemovePreprocessor.remove_input_tags will remove the code from the cells tagged with the provided tags, right?

For context, here's my script:

import sys
from pathlib import Path

from nbconvert.exporters import NotebookExporter
from nbconvert.preprocessors import TagRemovePreprocessor
from traitlets.config import Config


def clean(path, config, suffix="-sol"):
    exporter = NotebookExporter(config=config)
    exporter.register_preprocessor(TagRemovePreprocessor(config=config), True)

    # Configure and run our exporter - returns a tuple - first element with ipynb,
    # second with notebook metadata
    for p in Path(path).glob(f"**/*{suffix}.ipynb"):
        output = exporter.from_filename(str(p))
        p.with_stem(p.stem.removesuffix(suffix)).write_text(output[0])


if __name__ == "__main__":
    path = sys.argv[1]
    # Setup config
    c = Config()
    # Configure tag removal - be sure to tag your cells to remove  using the
    # words remove_cell to remove cells. You can also modify the code to use
    # a different tag word
    c.TagRemovePreprocessor.remove_cell_tags = ("hide-cell",)
    c.TagRemovePreprocessor.remove_all_outputs_tags = ("hide-output",)
    c.TagRemovePreprocessor.remove_input_tags = ("hide-input",)
    c.TagRemovePreprocessor.enabled = True
    # Configure and run out exporter
    c.NotebookExporter.preprocessors = ["nbconvert.preprocessors.TagRemovePreprocessor"]

    clean(path, c)

Thanks for the help!

@takluyver
Copy link
Member

It looks like the preprocessor sets some temporary metadata on the cell to tell the exporter to skip the input part, but that's only checked by template-based exporters, which excludes NotebookExporter.

You could also argue that leaving an empty code area is different from hiding/excluding the code area entirely, which isn't possible with notebook output. 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants