From 4b51819d4ca525a8b68ddc3e3541c1bfcfc5a28c Mon Sep 17 00:00:00 2001 From: Will Handley Date: Mon, 5 Nov 2018 19:26:55 +0000 Subject: [PATCH] Added reverse script --- README.rst | 22 ++++++++++++---------- example.py | 7 +++++-- nb2py | 44 ++++++++++++++++++++++++++++++++++++++++++++ setup.py | 2 +- 4 files changed, 62 insertions(+), 13 deletions(-) create mode 100755 nb2py diff --git a/README.rst b/README.rst index d49c37b..d179aaa 100644 --- a/README.rst +++ b/README.rst @@ -3,7 +3,7 @@ py2nb: convert python scripts to jupyter notebooks ================================================== :py2nb: convert python scripts to jupyter notebooks :Author: Will Handley -:Version: 0.0.3 +:Version: 0.0.4 :Homepage: https://github.com/williamjameshandley/py2nb .. image:: https://badge.fury.io/py/py2nb.svg @@ -19,6 +19,8 @@ markdown to jupyter notebooks. Markdown cells are rendered from comments beginning with ``#|``, splits between code cells are created by comment lines beginning with ``#-`` +``nb2py`` converts from jupyter notebooks to python + Installation ============ @@ -52,26 +54,28 @@ If one has a script named ``example.py`` containing the code: #| a jupyter notebook with a simple additional markdown format. #| #| Code by default will be put into code cells - #| + #| #| * To make a markdown cell, prefix the comment line with with '#|' #| * To split a code cell, add a line beginning with '#-' - + import numpy import matplotlib.pyplot as plt %matplotlib inline - + #| Here is a markdown cell. #| Maths is also possible: $A=B$ #| #| There are code cells below, split by '#-': - + x = numpy.random.rand(5) - #--------------------------- + + #------------------------------- + y = numpy.random.rand(4) z = numpy.random.rand(3) - + #| Here are some plots - + x = numpy.linspace(-2,2,1000) y = x**3 fig, ax = plt.subplots() @@ -82,12 +86,10 @@ then running .. code :: bash py2nb example.py - produces the notebook `example.ipynb `_ To do ===== -- reverse script - evaluation option for script produced - vim syntax highlighting for markdown code blocks diff --git a/example.py b/example.py index 2d98736..fc3a9a3 100644 --- a/example.py +++ b/example.py @@ -3,7 +3,7 @@ #| a jupyter notebook with a simple additional markdown format. #| #| Code by default will be put into code cells -#| +#| #| * To make a markdown cell, prefix the comment line with with '#|' #| * To split a code cell, add a line beginning with '#-' @@ -17,7 +17,9 @@ #| There are code cells below, split by '#-': x = numpy.random.rand(5) -#--------------------------- + +#------------------------------- + y = numpy.random.rand(4) z = numpy.random.rand(3) @@ -27,3 +29,4 @@ y = x**3 fig, ax = plt.subplots() ax.plot(x,y) + diff --git a/nb2py b/nb2py new file mode 100755 index 0000000..38b7720 --- /dev/null +++ b/nb2py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 +"""Create a notebook containing code from a script. + +Run as: python to_noteebook.py my_script.py +""" +import os +import argparse +import json + + +def convert(notebook_name): + """ Convert the jupyter notebook to python script""" + script_name = os.path.splitext(notebook_name)[0] + '.py' + with open(notebook_name, 'r') as f_in: + with open(script_name, 'w') as f_out: + last_source = '' + f_in = json.load(f_in) + for cell in f_in['cells']: + if last_source == 'code' and cell['cell_type'] == 'code': + f_out.write('#-------------------------------\n\n') + for line in cell['source']: + if cell['cell_type'] == 'markdown': + line = '#| ' + line.lstrip() + line = line.rstrip() + '\n' + f_out.write(line) + f_out.write('\n') + last_source = cell['cell_type'] + + +def parse_args(): + """Argument parsing for py2nb""" + description = "Convert a python script to a jupyter notebook" + parser = argparse.ArgumentParser(description=description) + parser.add_argument("notebook_name", help="name of notebok (.ipynb) to convert to script (.py)") + return parser.parse_args() + + +def main(): + args = parse_args() + convert(args.notebook_name) + + +if __name__ == '__main__': + main() diff --git a/setup.py b/setup.py index adb2b59..0d69e82 100644 --- a/setup.py +++ b/setup.py @@ -27,7 +27,7 @@ def get_version(short=False): author='Will Handley', author_email='wh260@cam.ac.uk', url='https://github.com/williamjameshandley/py2nb', - scripts=['py2nb'], + scripts=['py2nb', 'nb2py'], install_requires=['nbformat'], include_package_data=True, license='GPL',