def run(self): import glob import os import os.path from nbconvert.exporters import python from traitlets.config import Config examples_dir = os.path.join(os.path.dirname(__file__), 'examples') script_dir = os.path.join(examples_dir, 'scripts') if not os.path.exists(script_dir): os.makedirs(script_dir) c = Config({'Exporter': {'template_file': 'examples/python-scripts.tpl'}}) exporter = python.PythonExporter(config=c) for fname in glob.glob(os.path.join(examples_dir, 'notebooks', '*.ipynb')): output, _ = exporter.from_filename(fname) out_fname = os.path.splitext(os.path.basename(fname))[0] out_name = os.path.join(script_dir, out_fname + '.py') print(fname, '->', out_name) with open(out_name, 'w') as outf: outf.write(output)
def nb_to_python(nb_path): """convert notebook to python script""" exporter = python.PythonExporter() output, resources = exporter.from_filename(nb_path) return output