forked from juanjosegarciaripoll/seeq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexportnb.py
83 lines (79 loc) · 2.78 KB
/
exportnb.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#
# exportnb.py
#
# Library for exporting multiple Jupyter notebooks into a series of
# Python files. Main interface provided by function `export_notebooks`
# below.
#
# Author: Juan José García Ripoll
# License: See http://opensource.org/licenses/MIT
# Version: 1.0 (15/07/2018)
#
import sys, json, re, pathlib
def file_cell(lines):
#
# Determine whether a cell is to be saved as code. This
# is done by inspecting the lines of the cell and looking
# for a line with a comment of the form # file: xxxxx
# If so, it eliminates this line and collects the remaining
# text as code.
#
if len(lines):
ok = re.search('^#[ ]+file:[ ]+("[^\\"]*"|[^ \n]*)[ \n]*$', lines[0])
if ok:
return ok.group(1), lines[1:]
return False, lines
def register_cell(dictionary, cell_lines, add_newline=True):
#
# Input:
# - dictionary: a map from file names to lists of lines
# of code that will be written to the file
# - cell_lines: lines of a cell in a Jupyter notebook
# - add_newline: add empty line after each cell
#
# Output:
# - updated dictionary
#
file, lines = file_cell(cell_lines)
if file:
if file in dictionary:
lines = dictionary[file] + lines
if add_newline:
lines += ['\n']
dictionary[file] = lines
return dictionary
def read_notebook(dictionary, notebook, add_newline=True):
with open(notebook, 'r', encoding='utf8') as f:
j = json.load(f)
if j["nbformat"] >=4:
for i,cell in enumerate(j["cells"]):
dictionary = register_cell(dictionary, cell["source"], add_newline)
else:
for i,cell in enumerate(j["worksheets"][0]["cells"]):
dictionary = register_cell(dictionary, cell["input"], add_newline)
def write_notebooks(dictionary, root='', mkdirs=True):
#
# Input:
# - dictionary: a map from file names to list of lines of codes
# to be written
# - root: prefix to be added to all file names
# - mkdirs: create parent directories if they do not exist
#
for file in dictionary.keys():
path = pathlib.Path(file)
if mkdirs:
path.parent.mkdir(parents=True, exist_ok=True)
with path.open('w', encoding='utf8', newline='\n') as f:
for line in dictionary[file]:
f.write(line)
def export_notebooks(notebooks, root='', add_newline=True, mkdirs=True):
#
# Input:
# - notebooks: list of notebooks as file names
# - root: prefix for exporting all notebooks
# - add_linewline: add empty lines between cells
#
dictionary = {}
for nb in notebooks:
read_notebook(dictionary, nb, add_newline=add_newline)
write_notebooks(dictionary, root=root, mkdirs=mkdirs)