-
Notifications
You must be signed in to change notification settings - Fork 0
/
count-jupyter-nb-words.py
32 lines (28 loc) · 1.3 KB
/
count-jupyter-nb-words.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import io
import os
from nbformat import current
total_markdown = 0
total_heading = 0
total_code = 0
for root, dirs, files in os.walk("."):
for file in files:
if file.endswith(".ipynb") and not file.endswith("checkpoint.ipynb") :
#print(os.path.join(root, file))
with io.open(os.path.join(root, file), 'r', encoding='utf-8') as f:
nb = current.read(f, 'json')
word_count_markdown = 0
word_count_heading = 0
word_count_code = 0
for cell in nb.worksheets[0].cells:
if cell.cell_type == "markdown":
word_count_markdown += len(cell['source'].replace('#', '').lstrip().split(' '))
elif cell.cell_type == "heading":
word_count_heading += len(cell['source'].replace('#', '').lstrip().split(' '))
elif cell.cell_type == "code":
word_count_code += len(cell['input'].replace('#', '').lstrip().split(' '))
total_markdown += word_count_markdown
total_heading += word_count_heading
total_code += word_count_code
print("{} Words in notebooks' markdown" .format(total_markdown))
print("{} Words in notebooks' heading" .format(total_heading))
print("{} Words in notebooks' code" .format(total_code))