Skip to content

Commit

Permalink
Added reverse script
Browse files Browse the repository at this point in the history
  • Loading branch information
williamjameshandley committed Nov 5, 2018
1 parent b9ee28b commit 4b51819
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 13 deletions.
22 changes: 12 additions & 10 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
============

Expand Down Expand Up @@ -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()
Expand All @@ -82,12 +86,10 @@ then running
.. code :: bash
py2nb example.py
produces the notebook `example.ipynb <https://github.com/williamjameshandley/py2nb/blob/master/example.ipynb>`_

To do
=====
- reverse script
- evaluation option for script produced
- vim syntax highlighting for markdown code blocks
7 changes: 5 additions & 2 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '#-'

Expand All @@ -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)

Expand All @@ -27,3 +29,4 @@
y = x**3
fig, ax = plt.subplots()
ax.plot(x,y)

44 changes: 44 additions & 0 deletions nb2py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def get_version(short=False):
author='Will Handley',
author_email='[email protected]',
url='https://github.com/williamjameshandley/py2nb',
scripts=['py2nb'],
scripts=['py2nb', 'nb2py'],
install_requires=['nbformat'],
include_package_data=True,
license='GPL',
Expand Down

0 comments on commit 4b51819

Please sign in to comment.