def export_to_rst(nb_runner, args): """Export notebook to rst format.""" if args.rst is None: # if --rst is given but no filename is provided, # come up with a sane output name based on the # input filename if args.input_file.endswith('.ipynb'): args.rst = args.input_file[:-6] + '.rst' else: args.rst = args.input_file + '.rst' exporter = RSTExporter() logging.info("Saving RST snapshot to %s" % args.rst) #output, resources = exporter.from_notebook_node(nb_runner.nb) #codecs.open(args.rst, "w", encoding="utf-8").write(output) with codecs.open(args.rst, "w", encoding="utf-8") as fh: for worksheet in nb_runner.nb.worksheets: output, resources = exporter.from_notebook_node(worksheet) fh.write(output)
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'``', '`')
# Insert a note about ipython notebooks with a download link note = u''' .. note:: This tutorial is written as an interactive notebook that should be run on your own computer. See the :doc:`tutorial overview page <index>` for more details. Download link for this tutorial: :download:`{tutorial}.ipynb`. '''.format(tutorial=basename) notebook.cells.insert(1, { u'cell_type': u'raw', u'metadata': {}, u'source': note }) exporter = RSTExporter() output, resources = exporter.from_notebook_node( notebook, resources={'unique_key': basename + '_image'}) codecs.open(output_rst_fname, 'w', encoding='utf-8').write(output) for image_name, image_data in resources['outputs'].iteritems(): open(os.path.join(target_dir, image_name), 'wb').write(image_data) print 'Generating index.rst' text = ''' .. This is a generated file, do not edit directly. (See dev/tools/docs/build_tutorials.py) Tutorials