Debian Codegraph @

docs/nb_run_conv

raw symbols SCIP
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 """
4
5 Convert sphinx gallery notebook from empty to image filled
6
7 Created on Fri Sep 1 16:43:45 2017
8
9 @author: rflamary
10 """
11
12 import json
13 import glob
14 import hashlib
15 import subprocess
16
17 import os
18
19 cache_file = "cache_nbrun"
20
21 path_doc = "source/auto_examples/"
22 path_nb = "../notebooks/"
23
24
25 def load_json(fname):
26 try:
27 f = open(fname)
28 nb = json.load(f)
29 f.close()
30 except (OSError, IOError):
31 nb = {}
32 return nb
33
34
35 def save_json(fname, nb):
36 f = open(fname, "w")
37 f.write(json.dumps(nb))
38 f.close()
39
40
41 def md5(fname):
42 hash_md5 = hashlib.md5()
43 with open(fname, "rb") as f:
44 for chunk in iter(lambda: f.read(4096), b""):
45 hash_md5.update(chunk)
46 return hash_md5.hexdigest()
47
48
49 def to_update(fname, cache):
50 if fname in cache:
51 if md5(path_doc + fname) == cache[fname]:
52 res = False
53 else:
54 res = True
55 else:
56 res = True
57
58 return res
59
60
61 def update(fname, cache):
62 # jupyter nbconvert --to notebook --execute mynotebook.ipynb --output target
63 subprocess.check_call(["cp", path_doc + fname, path_nb])
64 print(
65 " ".join(
66 [
67 "jupyter",
68 "nbconvert",
69 "--to",
70 "notebook",
71 "--ExecutePreprocessor.timeout=600",
72 "--execute",
73 path_nb + fname,
74 "--inplace",
75 ]
76 )
77 )
78 subprocess.check_call(
79 [
80 "jupyter",
81 "nbconvert",
82 "--to",
83 "notebook",
84 "--ExecutePreprocessor.timeout=600",
85 "--execute",
86 path_nb + fname,
87 "--inplace",
88 ]
89 )
90 cache[fname] = md5(path_doc + fname)
91
92
93 cache = load_json(cache_file)
94
95 lst_file = glob.glob(path_doc + "*.ipynb")
96
97 lst_file = [os.path.basename(name) for name in lst_file]
98
99 for fname in lst_file:
100 if to_update(fname, cache):
101 print("Updating file: {}".format(fname))
102 update(fname, cache)
103 save_json(cache_file, cache)