一段脚本将ipython notebook
转化为py
文件。
It’s hard to make notebook
file to import
so it’s important to make notebook
importable.
#!/usr/bin/env python # coding: utf-8 import nbformat from nbconvert import PythonExporter def convertNotebook(notebookPath, modulePath): with open(notebookPath) as fh: nb = nbformat.reads(fh.read(), nbformat.NO_CONVERT) exporter = PythonExporter() source, meta = exporter.from_notebook_node(nb) with open(modulePath, 'w+') as fh: fh.writelines(source) def trans_all(): import os path = '.' list_dirs = os.listdir(path) for filename in list_dirs: if filename.endswith('.ipynb'): print(filename, filename[:-5] + 'py') convertNotebook(filename, filename[:-5] + 'py') trans_all()