Skip to content

Commit 13f0b43

Browse files
committed
Add script to find notebooks.
1 parent d327df5 commit 13f0b43

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

find_notebooks.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#! /usr/bin/env python
2+
from __future__ import print_function
3+
4+
import os
5+
from six.moves import shlex_quote
6+
7+
8+
def find_notebooks(base):
9+
"""Find Jupyter notebooks.
10+
11+
Parameters
12+
----------
13+
base : str
14+
Path to search for notebooks under.
15+
16+
Returns
17+
-------
18+
list of str
19+
Paths to discovered notebooks.
20+
"""
21+
notebooks = []
22+
for root, dirs, files in os.walk(base, topdown=True):
23+
for dir in dirs:
24+
if dir.startswith('.'):
25+
dirs.remove(dir)
26+
for fname in files:
27+
file_path = os.path.join(root, fname)
28+
if file_path.endswith('.ipynb'):
29+
notebooks.append(file_path)
30+
return notebooks
31+
32+
33+
def main():
34+
import argparse
35+
parser = argparse.ArgumentParser(
36+
description='Find Jupyter notebooks under a given base path')
37+
parser.add_argument('path', help='Path to search for notebooks.')
38+
parser.add_argument('--sort', action='store_true',
39+
help='Sort notebooks alphabetically.')
40+
41+
args = parser.parse_args()
42+
43+
notebooks = find_notebooks(args.path)
44+
45+
if args.sort:
46+
notebooks.sort()
47+
48+
# print(os.linesep.join([shlex_quote(nb) for nb in notebooks]))
49+
print(os.linesep.join([nb for nb in notebooks]))
50+
51+
52+
if __name__ == '__main__':
53+
main()

0 commit comments

Comments
 (0)