Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion 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: 1.1.1
:Version: 1.2.0
:Homepage: https://github.com/williamjameshandley/py2nb

.. image:: https://badge.fury.io/py/py2nb.svg
Expand Down Expand Up @@ -145,9 +145,11 @@ Command Line Options
py2nb script.py --execute # Convert and execute notebook
py2nb script.py --output workshop # Custom output name
py2nb script.py --output workshop --execute # Custom name + execution
py2nb --version # Show version number

nb2py notebook.ipynb # Convert notebook to script
nb2py notebook.ipynb --output script # Custom output script name
nb2py --version # Show version number

Command Blocks
==============
Expand Down
23 changes: 22 additions & 1 deletion nb2py.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@
# Export main functions for module use
__all__ = ['convert']

def get_version():
"""Get version from README.rst file."""
try:
readme_path = os.path.join(os.path.dirname(__file__), 'README.rst')
with open(readme_path, 'r') as f:
for line in f:
if ':Version:' in line:
return line.split(':')[2].strip()
except (FileNotFoundError, IndexError):
pass
return "unknown"


def convert(notebook_name, output_name=None):
""" Convert the jupyter notebook to python script"""
Expand Down Expand Up @@ -59,15 +71,24 @@ def parse_args():
"""Argument parsing for nb2py"""
description = "Convert a jupyter notebook to a python script"
parser = argparse.ArgumentParser(description=description)
parser.add_argument("notebook_name", help="name of notebok (.ipynb) to convert to script (.py)")
parser.add_argument("notebook_name", nargs='?', help="name of notebok (.ipynb) to convert to script (.py)")
parser.add_argument(
"--output",
help="specify output script filename (default: notebook_name.py)")
parser.add_argument(
"--version",
action="version",
version=f"nb2py {get_version()}")
return parser.parse_args()


def main():
args = parse_args()

if args.notebook_name is None:
print("Error: notebook_name is required")
return 1

script_name = convert(args.notebook_name, output_name=args.output)
print(f"✓ Successfully converted {args.notebook_name} to {script_name}")
return script_name
Expand Down
21 changes: 21 additions & 0 deletions py2nb.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@
# Export main functions for module use
__all__ = ['convert', 'execute_notebook', 'validate_notebook', 'CELL_SPLIT_CHARS', 'MARKDOWN_CHARS', 'COMMAND_CHARS']

def get_version():
"""Get version from README.rst file."""
try:
readme_path = os.path.join(os.path.dirname(__file__), 'README.rst')
with open(readme_path, 'r') as f:
for line in f:
if ':Version:' in line:
return line.split(':')[2].strip()
except (FileNotFoundError, IndexError):
pass
return "unknown"

# Comment syntax patterns
CELL_SPLIT_CHARS = ['#-', '# -']
MARKDOWN_CHARS = ['#|', '# |']
Expand Down Expand Up @@ -262,6 +274,7 @@ def parse_args():
parser = argparse.ArgumentParser(description=description)
parser.add_argument(
"script_name",
nargs='?',
help="name of script (.py) to convert to jupyter notebook (.ipynb)")
parser.add_argument(
"--no-validate",
Expand All @@ -274,13 +287,21 @@ def parse_args():
parser.add_argument(
"--output",
help="specify output notebook filename (default: script_name.ipynb)")
parser.add_argument(
"--version",
action="version",
version=f"py2nb {get_version()}")
return parser.parse_args()


def main():
"""Main conversion function."""
args = parse_args()

if args.script_name is None:
print("Error: script_name is required")
return 1

if not os.path.exists(args.script_name):
print(f"Error: File {args.script_name} not found")
return 1
Expand Down