How to hide one specific cell (input or output) in IPython Notebook?

This is now built into nbconvert (as of 5.3.0) using tags.

Here's an example removing a specific cell from the output. Using this notebook. The example has three cells: a markdown cell, a code cell that will be hidden, and a code cell that will not be hidden.

  1. Add the remove_cell tag to any cells you want to hide using the tag editor built into the notebook or JupyterLab (the specific name "remove_cell" doesn't matter)
  2. Convert with nbconvert
jupyter nbconvert nbconvert-example.ipynb --TagRemovePreprocessor.remove_cell_tags='{"remove_cell"}'

Any cells with the tag remove_cell will be removed from the output.

hidden

In addition to entire cells, you can filter just inputs or just outputs:

  • TagRemovePreprocessor.remove_input_tags
  • TagRemovePreprocessor.remove_single_output_tags
  • TagRemovePreprocessor.remove_all_outputs_tags

Here's a method that allows you to hide cells from the HTML/PDF output by editing the cell metadata only.

Versions I'm using:

$ jupyter notebook --version

4.1.0

$ jupyter nbconvert --version

4.2.0

  1. Download the ipython notebook extension templates by following install instructions on Github: pip install https://github.com/ipython-contrib/IPython-notebook-extensions/tarball/master
    • This just install files into your local jupyter data directory. Full details in the readme
  2. run jupyter notebook
  3. go to localhost:8888/nbextensions (or whatever port you started on) and activate Printview
  4. go back to localhost:8888/tree, create a new notebook and go into it
  5. create a code cell with some code in it that produces output e.g. print("You can see me") #but not me
  6. go to View > Cell Toolbar > Edit Metadata
  7. click the Edit Metadata button now showing to the top right of the cell
  8. add 'hide_input':True to the json e.g. mine looked like { "collapsed": false, "hide_input": true, "trusted": true } after
  9. save notebook
  10. go back to the terminal and execute jupyter nbconvert --to pdf --template printviewlatex.tplx notebookname.ipynb (if your notebook is called notebookname.ipynb.ipynb)

You should now have a document called notebookname.pdf in the directory. Hopefully it should have just the text You can see me in it...fingers crossed.


This is an extension of Mathmagician's answer, which enables you to:

  • toggle just a single cell (the JS function name has a random suffix, so if used more than one time, it would not conflict with other usages)
  • toggle the cell below the current cell - this is super handy in RISE presentations where you may want to show the code, but then hide it to display its output

demo of the toggle

What you need to do is run the following code first to define the hide_toggle function:

from IPython.display import HTML
import random

def hide_toggle(for_next=False):
    this_cell = """$('div.cell.code_cell.rendered.selected')"""
    next_cell = this_cell + '.next()'

    toggle_text = 'Toggle show/hide'  # text shown on toggle link
    target_cell = this_cell  # target cell to control with toggle
    js_hide_current = ''  # bit of JS to permanently hide code in current cell (only when toggling next cell)

    if for_next:
        target_cell = next_cell
        toggle_text += ' next cell'
        js_hide_current = this_cell + '.find("div.input").hide();'

    js_f_name = 'code_toggle_{}'.format(str(random.randint(1,2**64)))

    html = """
        <script>
            function {f_name}() {{
                {cell_selector}.find('div.input').toggle();
            }}

            {js_hide_current}
        </script>

        <a href="javascript:{f_name}()">{toggle_text}</a>
    """.format(
        f_name=js_f_name,
        cell_selector=target_cell,
        js_hide_current=js_hide_current, 
        toggle_text=toggle_text
    )

    return HTML(html)

And then use it in cells like this:

x = 1
y = 2
print('Result is {} + {}'.format(x, y))

hide_toggle()

Or this (if you want to toggle the next cell)

hide_toggle(for_next=True)

Your solution for hiding all input cells can be altered to affect just a single cell.

Change 'div.input' to 'div.cell.code_cell.rendered.selected div.input'.

HTML('''<script>
code_show=true; 
function code_toggle() {
    if (code_show){
        $('div.cell.code_cell.rendered.selected div.input').hide();
    } else {
        $('div.cell.code_cell.rendered.selected div.input').show();
    }
    code_show = !code_show
} 

$( document ).ready(code_toggle);
</script>

To show/hide this cell's raw code input, click <a href="javascript:code_toggle()">here</a>.''')

This works because when you click the "click here" prompt on a cell's output, that cell becomes the "selected" cell and thus becomes hidden.

If your JavaScript code executes a toggle within the <script></script> tags with a line of code like this

$( document ).ready(code_toggle);

then the block will automatically ("by default") be hidden when the input cell is executed.

Keep in mind that if you do make cell inputs hidden by default, you must run the cell with the Run Cells (Ctrl+Return) option, not the Run Cells and Select/Insert Below options. These will prompt the move of the "selected" label to the next cell before executing the JavaScript, so you may end up hiding a cell that doesn't have the "click here" toggle link in its output. In which case you will have to inspect the cell and navigate through the relevant tags and change display='none'; to display='block';.

Note that this must be put at the end of any code in your cell, and that you need to have imported HTML from IPython.display before executing this code. You can do so by executing

from IPython.display import HTML