How to count lines of code in jupyter notebook
I am currently using Jupyter ipython and the file I am working with contains a lot of code. I'm just wondering how many lines of code are in my file. Difficult to calculate as I have divided my code into many different blocks.
For anyone with experience with jupyter notebook, how can you calculate how many lines of code there are in total in a file?
Thank!
Edit: I figured out how to do this, albeit rather unclear. Here's how: download jupyter notebook as a .py file and then open the .py file in software like Xcode or whatever IDE you are using and count the lines of code there.
source to share
This will give you the total number of LOCs in one or more laptops that you pass to the script via the command line:
#!/usr/bin/env python
from json import load
from sys import argv
def loc(nb):
cells = load(open(nb))['cells']
return sum(len(c['source']) for c in cells if c['cell_type'] == 'code')
def run(ipynb_files):
return sum(loc(nb) for nb in ipynb_files)
if __name__ == '__main__':
print(run(argv[1:]))
So, you can do something like $ ./loc.py nb1.ipynb nb2.ipynb
to get the results.
source to share
The same can be done from the shell, if you have a useful jq utility :
jq '.cells[] | select(.cell_type == "code").source[]' nb1.ipynb nb2.ipynb | wc -l
Also you can use grep
to filter lines further, for example to remove empty lines:| grep -e ^\"\\\\n\"$ | wc -l
| grep -e ^\"\\\\n\"$ | wc -l
source to share
The answer from @Jessime Kirk is really good. But it looks like the ipynb file shouldn't have Chinese character. So I optimized the code as shown below.
#!/usr/bin/env python
from json import load
from sys import argv
def loc(nb):
with open(nb, encoding='utf-8') as data_file:
cells = load(data_file)['cells']
return sum(len(c['source']) for c in cells if c['cell_type'] == 'code')
def run(ipynb_files):
return sum(loc(nb) for nb in ipynb_files)
if __name__ == '__main__':
print(r"This file can count the code lines number in .ipynb files.")
print(r"usage:python countIpynbLine.py xxx.ipynb")
print(r"example:python countIpynbLine.py .\test_folder\test.ipynb")
print(r"it can also count multiple code.ipynb lines.")
print(r"usage:python countIpynbLine.py code_1.ipynb code_2.ipynb")
print(r"start to count line number")
print(run(argv[1:]))
source to share