Search code examples
ipythonjupyter-notebook

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


Is there a way to selectively hide one specific input or output cell in IPython notebook?

I could only find the below code to show / hide all input cells.

http://blog.nextgenetics.net/?e=102

But what if I only want to hide the first input cell of a notebook?


Solution

  • 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, which is also included at the end of this post. 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 the full source for the notebook used in this example:

    {
     "cells": [
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "Here's an example of how to hide cells with nbconvert."
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 1,
       "metadata": {
        "tags": [
         "remove_cell"
        ]
       },
       "outputs": [],
       "source": [
        "# This cell is hidden\n",
        "import pandas as pd"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 2,
       "metadata": {},
       "outputs": [
        {
         "data": {
          "text/html": [
           "<div>\n",
           "<style scoped>\n",
           "    .dataframe tbody tr th:only-of-type {\n",
           "        vertical-align: middle;\n",
           "    }\n",
           "\n",
           "    .dataframe tbody tr th {\n",
           "        vertical-align: top;\n",
           "    }\n",
           "\n",
           "    .dataframe thead th {\n",
           "        text-align: right;\n",
           "    }\n",
           "</style>\n",
           "<table border=\"1\" class=\"dataframe\">\n",
           "  <thead>\n",
           "    <tr style=\"text-align: right;\">\n",
           "      <th></th>\n",
           "      <th>A</th>\n",
           "    </tr>\n",
           "  </thead>\n",
           "  <tbody>\n",
           "    <tr>\n",
           "      <th>0</th>\n",
           "      <td>1</td>\n",
           "    </tr>\n",
           "    <tr>\n",
           "      <th>1</th>\n",
           "      <td>2</td>\n",
           "    </tr>\n",
           "  </tbody>\n",
           "</table>\n",
           "</div>"
          ],
          "text/plain": [
           "   A\n",
           "0  1\n",
           "1  2"
          ]
         },
         "execution_count": 2,
         "metadata": {},
         "output_type": "execute_result"
        }
       ],
       "source": [
        "pd.DataFrame({\"A\": [1, 2]})"
       ]
      }
     ],
     "metadata": {
      "celltoolbar": "Tags",
      "kernelspec": {
       "display_name": "Python 3",
       "language": "python",
       "name": "python3"
      },
      "language_info": {
       "codemirror_mode": {
        "name": "ipython",
        "version": 3
       },
       "file_extension": ".py",
       "mimetype": "text/x-python",
       "name": "python",
       "nbconvert_exporter": "python",
       "pygments_lexer": "ipython3",
       "version": "3.6.1"
      }
     },