问题
Is there a way to mark a cell in the ipython 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?
回答1:
Yes, Use the steps below
- Select view on the menubar
- Point to Cell Toolbar and select Edit Metadata
- An "Edit Metadata" button will appear at the top-right corner of the cell.
- Click on that button and edit the json that pops up. Set the editable key to true of false to get the desired effect.
回答2:
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.
回答3:
Another alternative, which does not require any third party tool, is to edit the metadata of the notebook, as described here.
To edit the metadata of a cell, you have two options. What worked for me was to open the notebook in an editor and look for the cell I wanted to lock. This is not a very efficient procedure, though. A more straightforward option is suggested in @Richard Ackon's answer:
- Select
View
on the menu bar. - Point to
Cell Toolbar
and selectEdit Metadata
. - An
Edit Metadata
button will appear at the top-right corner of the cell. - Click on that button and edit the json that pops up.
I could not use it, however, as I was working with Jupyter Lab and those menus and options didn't appear there.
Once you get to the metadata of the cell in the json file (or section), just add the following lines:
"metadata": {
"trusted": true,
"editable": false,
"deletable": false
}
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.
来源:https://stackoverflow.com/questions/18162970/read-only-cells-in-ipython-notebook