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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Convert sphinx gallery notebook from empty to image filled
Created on Fri Sep 1 16:43:45 2017
@author: rflamary
"""
import json
import glob
import hashlib
import subprocess
import os
cache_file = "cache_nbrun"
path_doc = "source/auto_examples/"
path_nb = "../notebooks/"
def load_json(fname):
try:
f = open(fname)
nb = json.load(f)
f.close()
except (OSError, IOError):
nb = {}
return nb
def save_json(fname, nb):
f = open(fname, "w")
f.write(json.dumps(nb))
f.close()
def md5(fname):
hash_md5 = hashlib.md5()
with open(fname, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
def to_update(fname, cache):
if fname in cache:
if md5(path_doc + fname) == cache[fname]:
res = False
else:
res = True
else:
res = True
return res
def update(fname, cache):
# jupyter nbconvert --to notebook --execute mynotebook.ipynb --output target
subprocess.check_call(["cp", path_doc + fname, path_nb])
print(
" ".join(
[
"jupyter",
"nbconvert",
"--to",
"notebook",
"--ExecutePreprocessor.timeout=600",
"--execute",
path_nb + fname,
"--inplace",
]
)
)
subprocess.check_call(
[
"jupyter",
"nbconvert",
"--to",
"notebook",
"--ExecutePreprocessor.timeout=600",
"--execute",
path_nb + fname,
"--inplace",
]
)
cache[fname] = md5(path_doc + fname)
cache = load_json(cache_file)
lst_file = glob.glob(path_doc + "*.ipynb")
lst_file = [os.path.basename(name) for name in lst_file]
for fname in lst_file:
if to_update(fname, cache):
print("Updating file: {}".format(fname))
update(fname, cache)
save_json(cache_file, cache)
|