-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b9ee28b
commit 4b51819
Showing
4 changed files
with
62 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
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() |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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', | ||
|