nbclassic icon indicating copy to clipboard operation
nbclassic copied to clipboard

fix: trust new notebooks and notebooks with new cells

Open maartenbreddels opened this issue 9 months ago • 0 comments

Problem

When a new notebook cell is created from the front end (this includes creating a new notebook), the notebook is not trusted after a page reload. The core issue is that nbclassic creates an invalid notebook on the front end, which does not follow the nbformat 4.5 schema.

This problem only happens when Jupyter notebook 6.x/nbclassic is used in combination with nbformat >=5.1.0 (where nbformat schema 4.5 was introduced) or higher. Before nbformat 5.1.0, there were no cell-ID's in the notebook format JEP 62. Starting from nbformat 5.1.0, Cell-ID's are added to notebook data https://github.com/jupyter/nbformat/pull/189.

Following JEP 62, raw_cell, markdown and code_cell should include an id field. When it is not present, nbformat will add the id field to the cell to make it valid according to the schema.

However, this happens after the notebook is 'trusted'. I am not an expert on the notary system, but mutation of the notebook after it is marked trusted will basically turn the notebook into an untrusted notebook.

Therefore, if we want to make use of the notary system as intended, we should send valid notebooks from the front end to the back end.

Steps to reproduce

  • Install and run
$ pip install "nbclassic<7" "nbformat>=5.1.0"
$ jupyter nbclassic
  • Create a new notebook
  • Add this to a cell:
import IPython.display
IPython.display.Javascript("window.javascriptExecuted = true")
  • Execute the cell
  • Save the notebook
  • Reload the notebook

Now, the notebook is seen as not trusted.

One could argue, this is not that bad, since it's a one time thing, but it is not: After trusting the notebook, add a new cell to the notebook, save, reload. The notebook is now again seen as untrusted.

Expected behavior

A new notebook and a notebook after a cell is added should be trusted after a reload.

Related issues / PRs

  • https://github.com/jupyter/notebook/issues/6828 This behaviour is also seen in notebook v7 (I did not validate this)
  • https://github.com/jupyter/nbformat/issues/359
  • https://github.com/jupyter/notebook/pull/5928

Solution

We should send valid notebooks from the front end to the back end. This means adding the id field to the raw_cell, markdown, and code_cell when they are created.

This PR is modeled after https://github.com/jupyterlab/jupyterlab/pull/10018. Although the id field should only be set for the raw_cell, markdown and code_cell, the JupyterLab PR sets the id field for all cell types, we only do this for the raw_cell, markdown and code_cell as per the JEP 62.

As explained in the code, we use a uuid trimmed to 8 characters, like nbformat.

maartenbreddels avatar Apr 26 '24 19:04 maartenbreddels