0

Is it possible to extract the content of a Jupyter notebook input cell programatically? Be that raw cell / code / Markdown, does not matter really. I was thinking of tools like nbconvert or papermill but could not find exactly what I am looking for... I would like to write a script which will essentially parse a notebook...

Is is possible to parse the output cells too?

maciek
  • 1,807
  • 2
  • 18
  • 30
  • The notebook format is just JSON, so yes, it's possible. – MattDMo Nov 09 '22 at 01:33
  • This is possible. In fact, jupyter/python does it every time it opens a notebook. See [here](https://ipython.org/ipython-doc/3/notebook/nbformat.html) for the format spec of the notebook files. I note that this is much easier to parse if you make and open a simple notebook file at the same time and compare. – davidlowryduda Nov 09 '22 at 01:37

1 Answers1

1

The Jupyter ecosystem includes nbformat for this this task.

The intro at the top of here will probably help you see how nbformat is the tool you seek. Importantly, the abstractions of the notebook & cells & types of cells is all baked in so that you don't have to worry about json parsing really.

I have several examples with code among questions/answers linked here and here.

Wayne
  • 6,607
  • 8
  • 36
  • 93