def test_tutorials(testfolder, tutorials=('ligand-binding-analysis', 'protein-folding-analysis')): startdir = os.path.dirname(os.path.realpath(__file__)) pyexp = PythonExporter() pyexp.template_file = os.path.join(startdir, 'simplepython.tpl') # For each tutorial noteboook for tut in glob(os.path.join(startdir, '*.ipynb')): name = os.path.splitext(os.path.basename(tut))[0] if name not in tutorials: continue testsubf = os.path.abspath(os.path.join(testfolder, name)) if not os.path.exists(testsubf): os.makedirs(testsubf) # Read notebook in with open(tut, 'r') as fi: notebook = nbformat.read(fi, nbformat.NO_CONVERT) output, resources = pyexp.from_notebook_node(notebook, ) # Write it out in .py format with open(os.path.join(testsubf, 'test_{}.py'.format(name)), 'w') as fo: fo.writelines(format_script(output, testsubf, name)) return pytest.main([testfolder])
def convert_to_python(notebook, template_file=None): exporter = PythonExporter() exporter.template_file = template_file notebook_code, meta = exporter.from_notebook_node(notebook) return notebook_code, meta