summaryrefslogtreecommitdiff
path: root/docs/nb_run_conv
diff options
context:
space:
mode:
authorRémi Flamary <remi.flamary@gmail.com>2017-09-01 18:41:01 +0200
committerRémi Flamary <remi.flamary@gmail.com>2017-09-01 18:41:01 +0200
commit98ae0808417d0f82e761f85265e8dcfa9ebe7e5f (patch)
tree4a667f84abc1556d32b8a56ec931012f7668db9b /docs/nb_run_conv
parent8ea3504c3bfe9c9440982f8ad0d172a240d54aff (diff)
conversion scripts
Diffstat (limited to 'docs/nb_run_conv')
-rwxr-xr-xdocs/nb_run_conv84
1 files changed, 84 insertions, 0 deletions
diff --git a/docs/nb_run_conv b/docs/nb_run_conv
new file mode 100755
index 0000000..99dbbce
--- /dev/null
+++ b/docs/nb_run_conv
@@ -0,0 +1,84 @@
+#!/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 sys
+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 targte
+ print(' '.join(['jupyter','nbconvert','--to','notebook','--execute',path_doc+fname,'--output',path_nb+fname]))
+ subprocess.check_call(['jupyter','nbconvert','--to','notebook','--execute',path_doc+fname,'--output',path_nb+fname])
+ 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)
+
+
+
+
+update(lst_file[0],cache)
+
+
+save_json(cache_file,cache)