Created
February 28, 2012 22:47
Revisions
-
minrk revised this gist
May 31, 2012 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,8 +12,8 @@ def heading_to_md(cell): level = cell.pop('level', 1) cell.source = '#'*level + ' ' + cell.source def raw_to_md(cell): """let raw passthrough as markdown""" cell.cell_type = "markdown" def downgrade(nb): @@ -25,8 +25,8 @@ def downgrade(nb): for cell in ws.cells: if cell.cell_type == 'heading': heading_to_md(cell) elif cell.cell_type == 'raw': raw_to_md(cell) return nb def downgrade_ipynb(fname): -
minrk created this gist
Feb 28, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,43 @@ """Simple utility script for semi-gracefully downgrading v3 notebooks to v2""" import io import os import sys from IPython.nbformat import current def heading_to_md(cell): """turn heading cell into corresponding markdown""" cell.cell_type = "markdown" level = cell.pop('level', 1) cell.source = '#'*level + ' ' + cell.source def txt_to_md(cell): """let plaintext passthrough as markdown""" cell.cell_type = "markdown" def downgrade(nb): """downgrade a v3 notebook to v2""" if nb.nbformat != 3: return nb nb.nbformat = 2 for ws in nb.worksheets: for cell in ws.cells: if cell.cell_type == 'heading': heading_to_md(cell) elif cell.cell_type == 'plaintext': txt_to_md(cell) return nb def downgrade_ipynb(fname): base, ext = os.path.splitext(fname) newname = base+'.v2'+ext print "downgrading %s -> %s" % (fname, newname) with io.open(fname, 'r', encoding='utf8') as f: nb = current.read(f, 'json') nb = downgrade(nb) with open(newname, 'w') as f: current.write(nb, f, 'json') if __name__ == '__main__': map(downgrade_ipynb, sys.argv[1:])