nbformat icon indicating copy to clipboard operation
nbformat copied to clipboard

Should read and reads handle the case where `as_version` is not specified?

Open SylvainCorlay opened this issue 7 years ago • 3 comments
trafficstars

Since the nbformat information is part of the notebook document, when not specified, could nbformat use that instead?

SylvainCorlay avatar Aug 08 '18 09:08 SylvainCorlay

The structure of a notebook object in memory is part of the nbformat API, so the as_version parameter exists to separate that from the contents of a file on disk. There's very deliberately no default, because the obvious default (don't convert it at all) means that the API depends on what's in the file you're loading.

E.g. with a previous version, lots of code did things like this:

nb = nbformat.read('some_notebook.ipynb')
for cell in nb.worksheets[0].cells:
    ...

All that code broke when we revised the notebook format and got rid of worksheets. If the as_version mechanism had been in place at the time, notebooks would have been converted back to the old structure and the code would continue working. Then the developer can update that code to the new API whenever suits them.

takluyver avatar Aug 09 '18 19:08 takluyver

Just chiming in here because I was just thinking the same thing. I agree that "explicit is better than implicit" when it comes to making assumptions about the notebook version. That said, I can say that I have never used anything other than passing nbf.NO_CONVERT. From a user's perspective, it feels strange because I probably don't even realize that there are "versions" of the notebook specification.

I don't know what's the right way to satisfy both perspectives because I agree it's also confusing to read a notebook and then get unexpected behavior, but I just wanted to add a vote that I also feel typing "NO_CONVERT" every time feels cumbersome (and is being designed around by tools like Jupytext: https://jupytext.readthedocs.io/en/latest/using-library.html)

choldgraf avatar Jan 28 '20 20:01 choldgraf

I second @choldgraf's comment and also request making NO_CONVERT the default. In my opinion, good defaults are meant to make code cleaner and more concise under the most common use cases. They're not expected to cover all edge cases or bugs caused by users not taking them into account.

carlosgmartin avatar Feb 10 '21 22:02 carlosgmartin