From 45a9c33b9d27783297eb8513093d7b154feaa3ce Mon Sep 17 00:00:00 2001 From: Ondrej Martinsky Date: Sun, 30 Apr 2017 12:37:29 +0100 Subject: [PATCH] run_all_jupyter_notebooks --- __run_all_jupyter_notebooks.py | 49 +++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/__run_all_jupyter_notebooks.py b/__run_all_jupyter_notebooks.py index 649ba32..521fc2a 100644 --- a/__run_all_jupyter_notebooks.py +++ b/__run_all_jupyter_notebooks.py @@ -1,32 +1,37 @@ -import sys +import sys, os import nbformat from nbconvert.preprocessors import ExecutePreprocessor from nbconvert import HTMLExporter -def tohtml(input, output=None): - if output is None: - output = input + ".html" +def tohtml(working_directory, input, output=None): + prevcwd = os.getcwd() + try: + os.chdir(working_directory) + + if output is None: + output = input + ".html" - print("Converting %s --> %s" % (input, output)) - ep = ExecutePreprocessor(timeout=300, kernel_name='python3') + print("Converting %s --> %s" % (input, output)) + ep = ExecutePreprocessor(timeout=300, kernel_name='python3') - with open(input) as f: - nb = nbformat.read(f, as_version=4) - - ep.preprocess(nb, {'metadata': {'path': './'}}) + with open(input) as f: + nb = nbformat.read(f, as_version=4) + + ep.preprocess(nb, {'metadata': {'path': './'}}) - #filename_output = input + ".executed.ipynb" - #with open(filename_output, 'wt') as f: - # nbformat.write(nb, f) + #filename_output = input + ".executed.ipynb" + #with open(filename_output, 'wt') as f: + # nbformat.write(nb, f) - exportHTML = HTMLExporter() - (body, resources) = exportHTML.from_notebook_node(nb) + exportHTML = HTMLExporter() + (body, resources) = exportHTML.from_notebook_node(nb) - with open(output, 'wt') as f: - f.write(body) - - print("Done") + with open(output, 'wt') as f: + f.write(body) + + print("Done") + finally: + os.chdir(prevcwd) - -tohtml("binomial_option_pricing/binomial_option_pricing.ipynb") -tohtml("black_litterman/black_litterman.ipynb") \ No newline at end of file +tohtml("binomial_option_pricing", "binomial_option_pricing.ipynb") +tohtml("black_litterman", "black_litterman.ipynb")