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'``', '`')
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'``','`')
def convert_nb_to_markdown(notebook_path): notebook_path = path.abspath(notebook_path) notebook_name = path.basename(notebook_path) notebook_base_name = notebook_name.replace(".ipynb", "") exporter = MarkdownExporter(template_path=[FOLDER], template_file="mk.tpl") filter_data_type = exporter.environment.filters['filter_data_type'] filter_data_type.display_data_priority.append("text/markdown") writer = FilesWriter(build_directory=BUILD_FULL_PATH) in_resources = { 'unique_key':notebook_base_name, 'output_files_dir': "%s_files" % notebook_base_name, 'worker': Worker() } book = read_notebook(notebook_path) output, resources = exporter.from_notebook_node(book, in_resources) writer.write(output, resources, notebook_name=notebook_base_name) worker = resources["worker"] for svg_fn in worker.svg_graphs: convert_svg_to_png(svg_fn) for merge in worker.graph_merges: merge.do_merge() def replace_images(g): fn = g.group(1) src = path.join(NOTEBOOK_DIR, "images", fn) dst_folder = path.join(BUILD_FULL_PATH, in_resources['output_files_dir']) if not path.exists(dst_folder): os.mkdir(dst_folder) dst = path.join(dst_folder, fn) shutil.copyfile(src, dst) return "{}/{}".format(path.basename(path.dirname(dst)), path.basename(dst)) def replace_svg(g): svg_fn = g.group(1) build_folder = BUILD_FULL_PATH abs_svg_fn = path.abspath(path.join(build_folder, svg_fn)) abs_png_fn = abs_svg_fn.replace(".svg", ".png") print "converting ", abs_svg_fn, abs_png_fn subprocess.call([INKSCAPE_PATH, '-d', '300', '-f', abs_svg_fn, '-e', abs_png_fn], shell=True) return "({})".format(svg_fn.replace(".svg", ".png")) md_fn = path.join(BUILD_FULL_PATH, notebook_base_name + ".md") with open(md_fn, "rb") as f: text = f.read() text = re.sub(r"/files/images/(.+\.png)", replace_images, text) image_folder = notebook_base_name + "_files" #text = re.sub(r"\((.+\.svg)\)", replace_svg, text) with open(md_fn, "wb") as f: f.write(text) return md_fn
resources = {} nb_name = os.path.splitext(os.path.basename(full_path))[0] nb_output_dirs = nb_name + args.outputs_dir_suffix 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)
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)