You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
importsysfrompathlibimportPathfromnbconvert.exportersimportNotebookExporterfromnbconvert.preprocessorsimportTagRemovePreprocessorfromtraitlets.configimportConfigdefclean(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 metadataforpinPath(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 configc=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 wordc.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 exporterc.NotebookExporter.preprocessors= ["nbconvert.preprocessors.TagRemovePreprocessor"]
clean(path, c)
Thanks for the help!
The text was updated successfully, but these errors were encountered:
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. 🤔
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:
Thanks for the help!
The text was updated successfully, but these errors were encountered: