4

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?

4
  • 1
    You could try kernel>restart & run all. Then scroll to the last cell and see what # is in the In[#]
    – Sam
    Aug 12, 2016 at 19:37
  • 1
    @Sam, that would work if all cells have valid code. If there is an error anywhere the run stops... if you are an user of the notebook, its not easy split notebooks, that I have found.
    – Merlin
    Aug 12, 2016 at 19:38
  • The notebook format is a JSON file. Parse it, then count the right part of the json structure.
    – nitind
    Aug 12, 2016 at 19:40
  • 1
    As an alternative approach, I make directories for my SO solutions. You may find just creating a single notebook per question in a directory works for you. Aug 12, 2016 at 19:58

4 Answers 4

6
  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'])    
2
  • 2
    This is with an older version of the notebook format; the current version has done away with 'worksheets'. If you use the nbformat library, it will automatically convert the notebook to a specified format version on load.
    – Thomas K
    Aug 14, 2016 at 17:10
  • Thanks, didn't know that.
    – exp1orer
    Aug 15, 2016 at 19:25
4

There's actually no need to parse the json. Just read it as text and count instances of, for example, "cell type":

with open(fname, 'r') as f:
    counter = 0
    for line in f:
        if '"cell_type":' in line:
            counter += 1

Or, even easier, just open your .ipynb notebook in a text editor, then highlight the same bit of text and see the count by hitting ctrl+F (or whatever the binding is for search).

If any cells have markdown and you want to avoid those, you can just search on "cell_type": "code", too.

Although as others have said, you're better off not storing your code this way. Or at least, I imagine you can store it in ways that will make it much easier to access in the future, if you want it for reference.

1
  • funny I did that prior to getting back to SO. [794] Its really not a problem, Just as long I save, close, exit and reopen.. This clears the memory., while the code and output remain.
    – Merlin
    Aug 12, 2016 at 20:35
2

You could execute your notebook from the command line by:

jupyter nbconvert --ExecutePreprocessor.allow_errors=True --to notebook --execute jupyter_notebook.ipynb

where: jupyter_notebook.ipynb should be replaced with your filename.ipynb.

With allow_errors=True, the notebook is executed until the end, regardless of any error encountered during the execution. The output notebook, will contain the stack-traces and error messages for all the cells raising exceptions.

2
  • Since this is running outside browser, is there away to find output memory usage -- not for this project but another. While in browser, i can get memory usage of objects.
    – Merlin
    Aug 12, 2016 at 20:01
  • BTW, you could also run it inside your browser through it's API Interface.< Refer: Docs>. I would recommend you having a closer look at the traitletsarguments passed to the ExecutePreprocessor which basically has various configurable options like timeout, version support etc. Aug 12, 2016 at 20:10
0
python -c "import sys, json; print(len(json.load(open(sys.argv[1],'r'))['cells']))" <notebook_filename.ipynb>

One-liner based on https://stackoverflow.com/a/38925464/588437.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.