Skip to content

Instantly share code, notes, and snippets.

@EricSchles
Created September 10, 2020 16:37
Show Gist options
  • Select an option

  • Save EricSchles/f86b874c3840a6619aff0c5752c114c0 to your computer and use it in GitHub Desktop.

Select an option

Save EricSchles/f86b874c3840a6619aff0c5752c114c0 to your computer and use it in GitHub Desktop.
count the number of lines in your jupyter notebooks
from glob import glob
from json import load
def loc(nb):
cells = load(open(nb))["cells"]
return sum(len(c["source"]) for c in cells)
root_folder = "~"
summation = 0
for File in glob(root_folder+"/**/*.ipynb", recursive=True):
summation += loc(File)
print(summation)
@paulrougieux
Copy link

Thanks very much for this useful way to count the number of code line in Jupyter notebooks. The version below uses the glob method of pathlib Path objects to list files. It prints the number of lines for each files in the directory and the total number as well.

from json import load
from pathlib import Path

def count_lines(notebook):
    """Count the number of source code lines in a Jupyter notebook."""
    cells = load(open(notebook))["cells"]
    return sum(len(c["source"]) for c in cells)

def count_lines_in_dir(directory):
    """Count the number of source code lines in all Jupyter notebook in the directory."""    
    print("Base directory", directory)
    n_lines_total = 0
    for file in directory.rglob("*.ipynb"):
        print(count_lines(file), "lines in", file)
        n_lines_total += count_lines(file)
    return n_lines_total

count_lines_in_dir(Path.cwd())

@EricSchles
Copy link
Author

Nice! This is a pretty neat way to do this as well :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment