Difficulty Editing Metadata Tags to Hide Cell Inputs and Outputs

I’ve consulted several forum and blog posts, and they all state that I can go into an individual cell’s metadata editor and add an appropriate tag to selectively hide a cell’s input or output.

For example, removing a cell’s input so that it won’t manifest either in the Jupyter-Lab environment or in an HTML/PDF report requires the tag,

{"tags": ["hide_input",]}

But whenever I do this, nothing happens. My Jupyter-Lab view doesn’t change. My HTML outputs still show the cells. No other blog post, article, or forum post I’ve seen so far either here or elsewhere can seem to explain to me what I’m missing.

Your title references ‘metadata’; however, you are editing the tags component for the cell.

The process can be demonstrated and tested in current JupyterLab. Click here to spin up a JupyterLab session using this repo for the content. The session is backed by MyBinder.org. The ‘index’ notebook will open. Click the blue bar next to the first code cell to hide the code cell. Save the notebook, and using the file navigator on the right open it in the editor by right-clicking on the file in the list, selecting open with ‘Editor’.

If you look in the editor, you’ll see cell the data for that cell is the following:

{
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "jupyter": {
     "source_hidden": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns"
   ]
  },

The key part in there is:

"metadata": {
    "jupyter": {
     "source_hidden": true
    },

The fact JupyterLab respects that setting can be demonstrated by clicking here where a JupyterLab session will open with the notebook I had saved already following what I suggested above. You’ll see the 2nd code cell, under 'Setup our imports, is the one visible when this notebook is reopened. (Notebook is here; however, you’ll note nbviewer, and github, doesn’t respect the hidden setting at this time).

This process of toggling settings and the fact JupyterLab respects the settings upon reopening is discussed here and here. You’ll want to pay special attention to the link at the end of that second listed link as it shows the proper syntax for the settings. You’ll note ‘hide_input’ does not appear on that page.

I believe you may have been finding information on the old classic notebook extension for the hide_code extension perhaps. I’m not sure what sources you are consulting as you don’t list them.

If you are using the editor to hand-edit the metadata direct in JupyterLab as you describe in your post, you’ll probably need to refresh the browser view in JupyterLab. It probably will note the change though in the backing code and let your reload with the new edit if you give it a chance.

There are (mis-specified) cells/<n>/metadata/source_hidden (and output_hidden, for code) which JupyterLab honors, but classic does not. As stated there, this is not intended for export mechanisms like nbconvert, and indeed, it doesn’t support any hard-coded value for tags for removing things, favoring a configuration-based approach. jupyter-book, nbsphinx and others have wildly-diverging plans for achieving this, and getting consensus is hard.

No other blog post, article, or forum post

Yep: the historic tribal knowledge of jupyter is stored in source code and maintained documentation itself, and the related GitHub issues and Pull Requests that made those come into being. If it’s not documented or in the code, it probably doesn’t exist.

I’m trying to leverage your insight about source_hidden and output_hidden
and here’s what I have seen

source_hidden

setting source_hidden kind of works; if I set the cell metadata to

{
  "hide_input": true,
  "tags": [
      "hide-input"
  ],
  "jupyter": {
      "source_hidden": true
  }
}

[[you may ignore the other 2 entries that aim at achieving the same result with notebook classic (with the hide_input extension enabled), and for jupyter-book, respectively]]
then the cell code is actually replaced with a three-dots thingy like so

however if I do click on these three dots then the metadata addition gets removed (wtf?) and I’m back to square one, the cell input is shown again… this feels odd, is that the intended behaviour ?

outputs_hidden

the right name actually in outputs plural; it does work too but in a similar way, it’s even wierder because you get to see the three dots to unhide the output, but only if the input is displayed…


the other 2 frontends that I am targetting (again, nb classic and jupyter notebook) will both behave differently

  • nbclassic will not show the input cell at all, no way to show it by clicking
  • jb will display a collapsible arrow (right arrow >) that when clicked will show the source but with the option to hide it back (clicking the arrow that is now downwards)

is there a way to get jlab to behave like any of these 2 ?