read-only cells in ipython notebook

岁酱吖の 提交于 2019-12-30 04:03:25

问题


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

  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 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:

  1. Select View on the menu bar.
  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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!