8

When using Jupyter notebook, outputs (include error messages) from executing a cell can be useful and so I'd like to freeze the cell alone with its outputs, or in another word, to inactivate the cell in order to keep the output.

Is this doable?

H.C.Chen
  • 412
  • 4
  • 14
  • 1
    The way I freeze a cell with its output : (1) Pull down menu > File > Download as > Markdown , save to a .md file. (2) copy the interested cell alone with its outputs from the .md file to a new cell in the working notebook. Not so beautiful but it works anyway. Thanks to @pylang who actually brought the workaround to me. For a perfect solution, I wish there could be better ways to freeze or convert selected cells alone with there outputs into HTML cells. – H.C.Chen Jan 08 '18 at 11:03

3 Answers3

6

This freezes the cell, but not the output:

  1. Open the notebook in an editor.

  2. Look for the cell you want to lock.

  3. Add the following lines to the metadata of the cell:

    "metadata": {
        "trusted": true,
        "editable": false,
        "deletable": false
    }
    
  4. Save and reload the notebook in Jupyter and... Tadah!, your cell can't be modified or deleted.

Unfortunately, the outputs can still be cleared by intentionally selecting that option in the menu bar (Edit > Clear Ouputs). Of course that can only happen if you DO WANT to clear the outputs and not just update them by running the cell.

Source

Mr. Duhart
  • 927
  • 12
  • 11
4

Historically, such a feature has not been available in native Jupyter.

However, you might try a Jupyter extension called "Runtools" available that displays a button that should run all cells and ignore exceptions.

pylang
  • 40,867
  • 14
  • 129
  • 121
  • Thank you very much. "Runtools" is close. Now I am thinking that we probably can modify the .ipynb JSON forcibly to convert specific cells along with their outputs to HTML or do that through an extension. Hopefully that extension has been existing? – H.C.Chen Jan 08 '18 at 09:15
  • Perhaps you are interested in [`nbconvert`](https://nbconvert.readthedocs.io/en/latest/) then. There is a section on [handling exceptions](https://nbconvert.readthedocs.io/en/latest/execute_api.html#handling-errors-and-exceptions). – pylang Jan 08 '18 at 09:44
  • thank you very much. "nbconvert" really looks like a solution. I'll need to read its documents and try . . . . . – H.C.Chen Jan 08 '18 at 10:42
4

There is a now an extension called Freeze in the default suite of nbextensions.
pip install jupyter_contrib_nbextensions and open Edit/nbextensions config from any notebook to enable freeze.

Yesh
  • 976
  • 12
  • 15
  • 3
    After the pip install mentioned above, need to run "jupyter contrib nbextension install --user" in a DOSBox (Windows 10) then restart jupyter notebook and then a new tab "Nbextensions" appears > find and check the "Freeze" > then 3 new tool icons appear on any notebook's tool bar : normal, read-only, and freeze. – H.C.Chen Jul 23 '19 at 00:21