Create an extension to make the cell un-editable by just clicking a button

I’m new to JupyterLab and I know that we can make the cell un-editable by directly change the { metadata: editable:false, and/or deletable:false}. It seems a hassle to write it everytime we want to ‘lock’ the cell. So I want to create an extension to make the cell un-editable by clicking a new button on the main menu(for now).

I notice when I collapse a cell(by clicking the blue rectangle on the left side of cell), it directly autofill/change the metadata so my idea is to follow the same concept as the collapser and the widget just don’t know how and which lines of code to use it.

Is there a better way to do it? Or is there any other example or documentation I should be looking at?

Thank you for the help


Yes having a button (or a checkbox) for this is a great idea, thank you for working on it! I even think that it could go directly into the JupyterLab rather than being an extension (if you would like so).

I am assuming that by “main menu” you are referring to the toolbar in the notebook. Another option would be to add such a button/checkbox to the property inspector, maybe just under the cell tags?

Please see earlier discussions on ideas for improving the user experience for non-editable cells:

As for the code and documentation:

  • if this is the very first time writing an extension, following the tutorial might be helpful
  • there is an example on how to add a button in the toolbar
  • for adding things to property inspector you would want to require INotebookTools token and then call addItem on it if writing an extension, or possibly just directly add it in activateNotebookTools if sending a pull request to core

Following the pattern as used in collapser might be a good idea. I am no expert here so cannot say for certain if this is the best way, but can give you some pointers on how it works:

  1. On click the outputHidden of a CodeCell is set. Note that you would want to work with a more general Cell type as Markdown and Raw cells can be non-editable too.
  1. The setter handles the widgets visibility, updates private state (this._outputHidden) and syncs metadata (if syncing is enabled) via this.saveCollapseState()
  1. For editable similar method is already available - it is a readOnly setter and you would use it like this.readOnly = !this.readOnly to switch the state:

Hope it helps!

1 Like

Thank you @krassowski for your detail suggestions. I’ll be looking all of it thoroughly.

I thought doing a custom extension first because it seems easier and I don’t have enough experience changing the JupyterLab itself. But I can start from here thanks to your help.

I’ll post here if there are any progress or needed any advice.

1 Like