def go(source_dir): print os.listdir(source_dir) if not os.path.exists(source_dir): raise ValueError('source_dir doesnt exist') print '----- starting NB Evaluation and Conversion' logging.basicConfig(level=logging.DEBUG, format=log_format, datefmt=log_datefmt) nb_files = recursive_find_by_filter(source_dir, '*.ipynb') print 'source dir is %s' % source_dir for nb_file in nb_files: print nb_file basename = os.path.basename(nb_file) notebook_name = basename[:basename.rfind('.')] build_directory = os.path.dirname(nb_file) r = NotebookRunner(nb_file, pylab=True) r.run_notebook() r.save_notebook(nb_file) exporter = RSTExporter() writer = FilesWriter() resources = {} resources['output_files_dir'] = '%s_files' % notebook_name output, resources = exporter.from_notebook_node(r.nb, resources=resources) writer.build_directory = build_directory writer.write(output, resources, notebook_name=notebook_name) rst_file = nb_file[:nb_file.rfind('.')] + '.' + exporter.file_extension # this could be improved to only change the double quotes from # cross references (ie :.*:``.*`` -> :.*:`.*`) find_replace(rst_file, r'``', '`')
nb_name = os.path.splitext(os.path.basename(full_path))[0] nb_output_dirs = nb_name + args.outputs_dir_suffix[0] resources['output_files_dir'] = nb_output_dirs # Clear old output dir path if os.path.isdir(os.path.join(build_dir, nb_output_dirs)): shutil.rmtree(os.path.join(build_dir, nb_output_dirs)) exporter = RSTExporter() nb = nbformat.reads_json(open(full_path).read()) if execute: log.info("Execute notebook '{}'".format(rel_path)) nb = execute_notebook(nb, resources) if overwrite and len(nbformat.validate(nb)) == 0: with open(full_path, 'w') as f: nbformat.write(nb, f, 'ipynb') elif overwrite and len(nbformat.validate(nb)) > 0: log.error("Executed notebook is not a valid format. " "Original notebook won't be overwritten.") log.info("Export notebook '{}'".format(rel_path)) (output, resources) = exporter.from_notebook_node(nb, resources=resources) writer = FilesWriter() writer.build_directory = build_dir writer.write(output, resources, notebook_name=nb_name)