Ensure that cell ids persist after save #5928
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR ensures that if an
'id'
field exists for a cell when the notebook is loaded, the same'id'
value is saved for the given cell when the notebook is saved.The previous behaviour was that a new random
'id'
value was generated every time for each cell.Details
As of nbformat schema 4.5, cells now have a mandatory
'id'
field for each cell:If this field does not exist for a given cell, nbformat will generate a new value for that field.
When a cell is loaded by notebook using the
fromJson
function, this'id'
field is ignored and when the cell is saved, nbformat sees there is no'id'
and randomly generates a new one.With this PR, the
'id'
field is saved to the cell data and then sent back to nbformat when the cell is saved so the cell id remains consistent between saves.This PR also ensures that when cells are copied, the cell
'id'
is removed, meaning that nbformat will generate a new id for it when it is saved. This is required because cell ids must be unique within a notebook.Why this is important
Tools, plugins, etc. that use cell ids will expect the cell ids to remain consistent between saves. Use of ids in plugins is one of the main reasons for adding the id field in the first place: