How to hide cells and input in Jupyter Lab or Jupyter Notebook
Asked Answered
H

0

6

Running Jupyter Lab Version 3.0.11

I'd like to hide or remove cells, and/or hide cell input, and/or hide cell output in Jupyter Notebook and/or Jupyter Lab when converted to HTML and/or PDF. And, I want to choose which cells/input/output to remove and which to keep.

I'm trying Jupyter Lab's celltags extension, but it isn't working.

Here's the notebook with a "remove-input" cell tag (UPDATE: the celltags extension is no longer installed, but the results are the same):

enter image description here

And, here's the HTML document it creates with the input not removed:

enter image description here

Hiding the cell in the notebook by clicking the tab next to it didn't translate to a hidden cell in the exported HTML doc.

As you can see, I have tried using TagRemovePreprocessor, but that just throws the error that the module is not found even though I have everything installed that I import. I tried a couple of fixes to that, which didn't work, but I don't really want to shave that yak if I don't need to.

I'd rather cell tags just work, like in R Studio markdown documents. At this point, it might even be easier to copy and paste my notebook to an RMD and run it with a Python kernel, since RMDs have that widget and easy tag built in and functional.

All that said, any Jupyter Notebook or Jupyter Lab solution is welcome, especially if it's just a simple toggle that actually works.

Thanks!

P.S. Here's the code from the commented out first cell, and the error from it:

from traitlets.config import Config
import nbformat as nbf
from nbconvert.exporters import HTMLExporter
from nbconvert.preprocessors import TagRemovePreprocessor

c = Config()
c.TagRemovePreprocessor.enabled=True
# Configure our tag removal
c.TagRemovePreprocessor.remove_cell_tags = ("remove_cell",)
c.TagRemovePreprocessor.remove_all_outputs_tags = ('remove_output',)
c.TagRemovePreprocessor.remove_input_tags = ('remove_input',)

# Configure and run out exporter
c.HTMLExporter.preprocessors = ["TagRemovePreprocessor"]
# c.HTMLExporter.preprocessors = ["nbf.preprocessors.TagRemovePreprocessor"]
HTMLExporter(config=c).from_filename("./test.ipynb")
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-4-2a221250acbf> in <module>
     14 c.HTMLExporter.preprocessors = ["TagRemovePreprocessor"]
     15 # c.HTMLExporter.preprocessors = ["nbf.preprocessors.TagRemovePreprocessor"]
---> 16 HTMLExporter(config=c).from_filename("./test.ipynb")

~\anaconda3\envs\py3\lib\site-packages\nbconvert\exporters\templateexporter.py in __init__(self, config, **kw)
    323             Template to use when exporting.
    324         """
--> 325         super().__init__(config=config, **kw)
    326 
    327         self.observe(self._invalidate_environment_cache,

~\anaconda3\envs\py3\lib\site-packages\nbconvert\exporters\exporter.py in __init__(self, config, **kw)
    112         super().__init__(config=with_default_config, **kw)
    113 
--> 114         self._init_preprocessors()
    115 
    116 

~\anaconda3\envs\py3\lib\site-packages\nbconvert\exporters\templateexporter.py in _init_preprocessors(self)
    488 
    489     def _init_preprocessors(self):
--> 490         super()._init_preprocessors()
    491         conf = self._get_conf()
    492         preprocessors = conf.get('preprocessors', {})

~\anaconda3\envs\py3\lib\site-packages\nbconvert\exporters\exporter.py in _init_preprocessors(self)
    264         # Load user-specified preprocessors.  Enable by default.
    265         for preprocessor in self.preprocessors:
--> 266             self.register_preprocessor(preprocessor, enabled=True)
    267 
    268 

~\anaconda3\envs\py3\lib\site-packages\nbconvert\exporters\exporter.py in register_preprocessor(self, preprocessor, enabled)
    225             # Preprocessor is a string, import the namespace and recursively call
    226             # this register_preprocessor method
--> 227             preprocessor_cls = import_item(preprocessor)
    228             return self.register_preprocessor(preprocessor_cls, enabled)
    229 

~\anaconda3\envs\py3\lib\site-packages\traitlets\utils\importstring.py in import_item(name)
     36     else:
     37         # called with un-dotted string
---> 38         return __import__(parts[0])

ModuleNotFoundError: No module named 'TagRemovePreprocessor'

I have installed nbconvert, and TagRemovePreprocessor doesn't throw an error in its import statement.

I can run c.HTMLExporter.preprocessors = ["nbf.preprocessors.TagRemovePreprocessor"] instead of c.HTMLExporter.preprocessors = ["TagRemovePreprocessor"], and I get the same error thrown from the same line except it says there is no module named nbf.

Handshaker answered 3/4, 2021 at 21:35 Comment(6)
You should not install celltags in JupterLab 2.0+ in the first place. This functionality is available by default since. There is a huge banner on github.com/jupyterlab/jupyterlab-celltags telling that. You might have issues because of the conflict of the built-in and installed version. I am surprised it passed verification on 3.0.Atp
What error did you see when running nbconvert? Could you paste the code you run along with the full exception/traceback? Submitting images of code is su-boptimal in many was. For one I cannot see your code due to poor contrast, and even if I saw it I would not have time to type myself.Atp
@Atp Thanks for taking a look. Yeah, I saw that about celltags being built-in. I thought the extension might have been the "built-in" piece. In any case. I tried it with and without celltags installed. Same result.Handshaker
@Atp The question isn't really how to make the code work. It's how to make the cell tags work. I just mentioned the code since it appears in the pic. But, let me run that code, and I will update the question with it and the error.Handshaker
You cell tag is remove-input with a hyphen, but your code looks for remove_input with an underscore. I typically remove input cells using the command line interface with something like: jupyter nbconvert mynotebook.ipynb --TagRemovePreprocessor.enabled=True --TagRemovePreprocessor.remove_cell_tags remove_cell. More here: nbconvert.readthedocs.io/en/latest/removing_cells.htmlOpinion
Thanks, Richard! I'll have to try that next time. I'll post a solution here if it works, but it might be some time. I'm currently in an R-heavy institution.Handshaker

© 2022 - 2024 — McMap. All rights reserved.