def convert_to_rst(notebook, resources, target_directory): """Return a list of strings representing the rst of the given notebook.""" exporter = RSTExporter() rst, resources = exporter.from_notebook_node(notebook, resources) rst = rst.split('\n') # Convert linked resources (such as images) into actual files in the # source directory. for name, data in resources.get('outputs', {}).items(): if name.startswith('/'): name = target_directory + name if not os.path.exists(os.path.dirname(name)): os.makedirs(os.path.dirname(name)) with io.open(name, 'wb') as f: f.write(data) return rst
def extract_notebook_thumbnail(filepath, target_file): """ Returns a thumbnail using the last image found in the specified notebook. For now, just returns the image at it's original size. """ # RSTExporter saves images separately rst_exporter = RSTExporter() (body, resources) = rst_exporter.from_filename(filepath) # Find image entries extensions = ('png', 'jpg') images = [x for x in resources['outputs'].keys() if x.endswith(extensions)] # Save image key = images.pop() ext = key[-3:] image = Image(data=resources['outputs'][key], format=ext) # Write image to disk fp = open("{}.{}".format(target_file, ext), 'wb') fp.write(image.data) fp.close()
for notebook in notebooks: try: f = open(notebook, 'rt') example_nb = f.read() f.close() rst_path = os.path.join(cwd, 'doc', 'source') path_parts = os.path.split(notebook) nb_filename = path_parts[-1] nb_filename = nb_filename.split('.')[0] source_dir = nb_filename.split('_')[0] rst_filename = os.path.join(cwd, 'doc', 'source', source_dir, nb_filename + '.rst') example_nb = nbformat.reader.reads(example_nb) rst_export = RSTExporter() (body, resources) = rst_export.from_notebook_node(example_nb) with open(rst_filename, 'wt') as rst: rst.write(body) for key in resources['outputs'].keys(): if key.endswith('.png'): resource_filename = os.path.join(cwd, 'doc', 'source', source_dir, key) with open(resource_filename, 'wb') as resource: resource.write(resources['outputs'][key]) except: import warnings warnings.warn('Unable to convert {original} to {target}. This '
full_path = os.path.join(root, f) rel_path = os.path.relpath(full_path, source_dir) build_dir = os.path.dirname(full_path) rel_build_dir = os.path.relpath(build_dir, source_dir) 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))
full_path = os.path.join(root, f) rel_path = os.path.relpath(full_path, source_dir) build_dir = os.path.dirname(full_path) rel_build_dir = os.path.relpath(build_dir, source_dir) resources = {} 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))