Merlin Merlin - 3 months ago 25
Python Question

IPython Notebook: Count number of cells in notebook

Answering questions Stack Overflow, I use the same ipython notebook, which makes its easier to search previously given answers.

The notebook is starting to slow down. The question I have is: How do I count the numbers of cells in the notebook?

Answer
  1. I recommend you don't use the same ipython notebook for everything. If using multiple notebooks would lead to repeat code, you should be able to factor out common functionality into actual python modules which your notebooks can import.
  2. the notebook is just a json file, if you read the file as a json you can do it easily.

For example:

import json    
document = json.load(open(filepath,'r'))
for worksheet in document['worksheets']:
    print len(worksheet['cells'])    
Comments