From 83800868c0f42d28cb988a2817216e06e9c23ef0 Mon Sep 17 00:00:00 2001 From: Dimitar Tasev Date: Tue, 14 Jan 2020 16:11:42 +0000 Subject: [PATCH 1/3] Adds support for '# -' and '# |' Use startswith instead of a string index. Changes how the markdown text is found so that it works for both syntaxes --- example.ipynb | 71 +++++++++++++++++++++++++++------------------------ example.py | 18 ++++++++++--- py2nb | 21 ++++++++++----- 3 files changed, 66 insertions(+), 44 deletions(-) diff --git a/example.ipynb b/example.ipynb index c79e021..3dcbe66 100644 --- a/example.ipynb +++ b/example.ipynb @@ -9,19 +9,20 @@ " a jupyter notebook with a simple additional markdown format.\n", "\n", " Code by default will be put into code cells\n", - " \n", - " * To make a markdown cell, prefix the comment line with with '#|'\n", - " * To split a code cell, add a line beginning with '#-'" + "\n", + " * To make a markdown cell, prefix the comment line with with '#|' or '# |'\n", + " * To split a code cell, add a line beginning with '#-' or '# -#" ] }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "import numpy\n", "import matplotlib.pyplot as plt\n", + "import numpy\n", + "\n", "%matplotlib inline" ] }, @@ -32,12 +33,19 @@ "Here is a markdown cell.\n", " Maths is also possible: $A=B$\n", "\n", - " There are code cells below, split by '#-':" + " There are code cells below, split by `'#-'`:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here is another markdown cell" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -46,7 +54,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -63,38 +71,33 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[]" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - }, - { - "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXYAAAD8CAYAAABjAo9vAAAABHNCSVQICAgIfA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/Nhe9bGI2tPw95nZRcC7QO/GWLGZtQLeBu50zpUc+XQdn+LZ9oqIYnfOjTnW82Z2I3AJcL4LTlAdIR84fM8lg9r7sYY1Vz3H2BZ8W2Bm71D753ZIxe5BrkbfXma208y6OOe2B//kLDjKGAe313ozm0Pt3o7XxV6fr//gMvlWe+vH1oT/T/7j5nLO7T7s3b9S+7pTJAjLz1SoDi9U59yHZvZnM2uaerMWAAABjElEQVTvnAvrdWTMLJ7aUn/FOTetjkXCur0ifirGzMYBvwAuc86VHWWxBUBvMzvZzBKofbErbEdU1JeZtTSzlIOPqX0huM5X7xuZH9vrfeDG4OMbga/8ZWFmbcwsMfi4PXAGsDIMWerz9R+e92pg9lF2Kho11xHzsJdRO38bCd4Hvh082mMUUHxw6s1PZtb54GsjZjaS2s7bfezPCnmdBjwHrHLOPXqUxcK7vRrz1eITfIV5LbVzUUuC/w4eqXAS8OERrzKvoXbvblIj5LqS2t+6FcBOYMaRuag9umFp8F9OpOTyaXu1Az4B8oJv2wY/ngU8G3w8Glge3F7LgZvDmOcrXz/wW2p3IACSgDeDP3/zgR7h3kb1zPV/wZ+lpcCnQL9GyvUasB2oCv583QzcAtwSfN6Ap4K5l3OMI8UaOddth22vL4HRjZDpTGqnVZYd1lsXNeb20pmnIiJRJuKnYkREpGFU7CIiUUbFLiISZVTsIiJRRsUuIhJlVOwiIlFGxS4iEmVU7CIiUeb/AbRzuQGggFlvAAAAAElFTkSuQmCC\n", - "text/plain": [ - "
" - ] - }, - "metadata": { - "needs_background": "light" - }, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "x = numpy.linspace(-2,2,1000)\n", "y = x**3\n", "fig, ax = plt.subplots()\n", "ax.plot(x,y)" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here is another plot" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = np.linspace(-np.pi, np.pi, 201)\n", + "fig, ax = plt.subplots()\n", + "ax.plot(x,np.sin(x))" + ] } ], "metadata": { @@ -113,7 +116,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.1" + "version": "3.6.7" } }, "nbformat": 4, diff --git a/example.py b/example.py index fc3a9a3..b8c91e7 100644 --- a/example.py +++ b/example.py @@ -4,17 +4,20 @@ #| #| 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 '#-' +#| * To make a markdown cell, prefix the comment line with with '#|' or '# |' +#| * To split a code cell, add a line beginning with '#-' or '# -# -import numpy import matplotlib.pyplot as plt +import numpy + %matplotlib inline #| Here is a markdown cell. #| Maths is also possible: $A=B$ #| -#| There are code cells below, split by '#-': +#| There are code cells below, split by `'#-'`: + +# | Here is another markdown cell x = numpy.random.rand(5) @@ -30,3 +33,10 @@ fig, ax = plt.subplots() ax.plot(x,y) +# ------------------------------- + +# | Here is another plot + +x = np.linspace(-np.pi, np.pi, 201) +fig, ax = plt.subplots() +ax.plot(x,np.sin(x)) diff --git a/py2nb b/py2nb index 5022ba7..7b84ae9 100755 --- a/py2nb +++ b/py2nb @@ -3,9 +3,14 @@ Run as: python to_noteebook.py my_script.py """ +import argparse import os + import nbformat.v4 -import argparse + +CODE_SPLIT_CHARS = ['#-', '# -'] +MARKDOWN_CHARS = ['#|', '# |'] + def new_cell(nb, cell, markdown=False): """ Create a new cell @@ -38,10 +43,12 @@ def convert(script_name): code_cell = '' nb = nbformat.v4.new_notebook() for line in f: - if line[:2] == '#-' or line[:2] == '#|': + if line.startswith((*CODE_SPLIT_CHARS, *MARKDOWN_CHARS)): code_cell = new_cell(nb, code_cell) - if line[:2] == '#|': - markdown_cell += line[2:] + if line.startswith((*MARKDOWN_CHARS, )): + # find the first occurence of | + # and add the rest of the line to the markdown cell + markdown_cell += line[line.index('|') + 1:] else: markdown_cell = new_cell(nb, markdown_cell, markdown=True) else: @@ -59,8 +66,10 @@ def parse_args(): """Argument parsing for py2nb""" description = "Convert a python script to a jupyter notebook" parser = argparse.ArgumentParser(description=description) - parser.add_argument("script_name", help="name of script (.py) to convert to jupyter notebook (.ipynb)") - return parser.parse_args() + parser.add_argument( + "script_name", + help="name of script (.py) to convert to jupyter notebook (.ipynb)") + return parser.parse_args() def main(): From 26ffae1282a95a8cf6162b14fcc12b65f726de9c Mon Sep 17 00:00:00 2001 From: Dimitar Tasev Date: Thu, 30 Jan 2020 08:33:09 +0000 Subject: [PATCH 2/3] Update README with new syntax --- README.rst | 82 +++++++++++++++++++++++++++++++----------------------- example.py | 2 +- 2 files changed, 48 insertions(+), 36 deletions(-) diff --git a/README.rst b/README.rst index d179aaa..276baf4 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.4 +:Version: 1.0.0 :Homepage: https://github.com/williamjameshandley/py2nb .. image:: https://badge.fury.io/py/py2nb.svg @@ -14,7 +14,7 @@ Description =========== ``py2nb`` is a python package for converting python scripts with minimal -markdown to jupyter notebooks. +markdown to jupyter notebooks. Markdown cells are rendered from comments beginning with ``#|``, splits between code cells are created by comment lines beginning with ``#-`` @@ -49,44 +49,56 @@ If one has a script named ``example.py`` containing the code: .. code:: python - #| # Testing ipython notebook - #| This is designed to demonstrate a simple script that converts a script into - #| 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() - ax.plot(x,y) + #| # Testing ipython notebook + #| This is designed to demonstrate a simple script that converts a script into + #| 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 '#|' or '# |' + #| * To split a code cell, add a line beginning with '#-' or '# -' + + import matplotlib.pyplot as plt + import numpy + + %matplotlib inline + + #| Here is a markdown cell. + #| Maths is also possible: $A=B$ + #| + #| There are code cells below, split by `'#-'`: + + # | Here is another markdown cell + + 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() + ax.plot(x,y) + + # ------------------------------- + + # | Here is another plot + + x = np.linspace(-np.pi, np.pi, 201) + fig, ax = plt.subplots() + ax.plot(x,np.sin(x)) + then running .. code :: bash - + py2nb example.py - + produces the notebook `example.ipynb `_ To do diff --git a/example.py b/example.py index b8c91e7..2a38272 100644 --- a/example.py +++ b/example.py @@ -5,7 +5,7 @@ #| Code by default will be put into code cells #| #| * To make a markdown cell, prefix the comment line with with '#|' or '# |' -#| * To split a code cell, add a line beginning with '#-' or '# -# +#| * To split a code cell, add a line beginning with '#-' or '# -' import matplotlib.pyplot as plt import numpy From 092494d4eb46bfe6de56f3ee435e3fcecc0133e6 Mon Sep 17 00:00:00 2001 From: Dimitar Tasev Date: Thu, 30 Jan 2020 08:44:38 +0000 Subject: [PATCH 3/3] Makes this Python 2 and 3 compatible, small typo fix Python 2 wasn't happy with list unpacking so that's removed in favour of a simple iteration --- example.ipynb | 22 ++-------------------- py2nb | 13 +++++++++---- 2 files changed, 11 insertions(+), 24 deletions(-) diff --git a/example.ipynb b/example.ipynb index 3dcbe66..c09fe64 100644 --- a/example.ipynb +++ b/example.ipynb @@ -11,7 +11,7 @@ " Code by default will be put into code cells\n", "\n", " * To make a markdown cell, prefix the comment line with with '#|' or '# |'\n", - " * To split a code cell, add a line beginning with '#-' or '# -#" + " * To split a code cell, add a line beginning with '#-' or '# -'" ] }, { @@ -100,25 +100,7 @@ ] } ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.6.7" - } - }, + "metadata": {}, "nbformat": 4, "nbformat_minor": 2 } diff --git a/py2nb b/py2nb index 7b84ae9..53e3ea6 100755 --- a/py2nb +++ b/py2nb @@ -8,9 +8,9 @@ import os import nbformat.v4 -CODE_SPLIT_CHARS = ['#-', '# -'] +ACCEPTED_CHARS = ['#-', '# -'] MARKDOWN_CHARS = ['#|', '# |'] - +ACCEPTED_CHARS.extend(MARKDOWN_CHARS) def new_cell(nb, cell, markdown=False): """ Create a new cell @@ -35,6 +35,11 @@ def new_cell(nb, cell, markdown=False): nb.cells.append(cell) return '' +def str_starts_with(string, options): + for opt in options: + if string.startswith(opt): + return True + def convert(script_name): """ Convert the python script to jupyter notebook""" @@ -43,9 +48,9 @@ def convert(script_name): code_cell = '' nb = nbformat.v4.new_notebook() for line in f: - if line.startswith((*CODE_SPLIT_CHARS, *MARKDOWN_CHARS)): + if str_starts_with(line, ACCEPTED_CHARS): code_cell = new_cell(nb, code_cell) - if line.startswith((*MARKDOWN_CHARS, )): + if str_starts_with(line, MARKDOWN_CHARS): # find the first occurence of | # and add the rest of the line to the markdown cell markdown_cell += line[line.index('|') + 1:]