jverzani
jverzani

Reputation: 5700

read-only cells in ipython/jupyter notebook

Is there a way to mark a cell in the ipython/jupyter notebook readonly using the json format in the ipynb file? (E.g., a cell attribute "readonly":false or some such.) If not, is there a jquery hack to find suppress the double click event in the cell?

Upvotes: 33

Views: 33864

Answers (3)

Richard Ackon
Richard Ackon

Reputation: 741

Yes, Use the steps below:

  1. Select view on the menubar
  2. Point to Cell Toolbar and select Edit Metadata
  3. An "Edit Metadata" button will appear at the top-right corner of the cell.
  4. Click on that button and edit the json that pops up. Set the editable key to true or false to get the desired effect.

The JSON will look something like this:

{
  "deletable": false,
  "editable": false,
  "scrolled": true,
  "trusted": true
}

Upvotes: 24

mrbronkson
mrbronkson

Reputation: 1105

@Richard Ackon's answer requires adjustments for JupyterLab:

  1. Open the Property Inspector.

  2. Focus the cell you want to lock.

  3. Add the following lines to the Cell Metadata:

    {
        "trusted": true,
        "editable": false,
        "deletable": false
    }
    
  4. Click on the tick to save the metadata... Tadah!, your cell can't be modified or deleted.

The Property Inspector comes built-in since JupyterLab 2.0 (note it was moved to the right sidebar by default in JupyterLab 3.0). For older JupyterLab versions you would need to modify the notebook file manually.

enter image description here

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

Upvotes: 30

oz123
oz123

Reputation: 28878

There is an extension for IPython that is supposed to that:

Read Only Cell extension.

Getting it to work is something else, but it is there.

Upvotes: 3

Related Questions