forked from rasbt/python-machine-learning-book-2nd-edition
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
38 changed files
with
11,425 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Simple helper script to convert | ||
# a Jupyter notebook to Python | ||
# | ||
# Sebastian Raschka, 2017 | ||
|
||
|
||
import argparse | ||
import os | ||
import subprocess | ||
|
||
|
||
def convert(input_path, output_path): | ||
subprocess.call(['jupyter', 'nbconvert', '--to', 'script', | ||
input_path, '--output', output_path]) | ||
|
||
|
||
def cleanup(path): | ||
|
||
skip_lines_startwith = ('Image(filename=', | ||
'get_ipython()', | ||
'# <br>', | ||
'from __future__ import print_function') | ||
|
||
clean_content = [] | ||
with open(path, 'r') as f: | ||
for line in f: | ||
if line.startswith(skip_lines_startwith): | ||
if line.startswith('from __future__ import print_function'): | ||
clean_content.insert(0, | ||
'from __future__ import ' | ||
'print_function\n\n\n') | ||
continue | ||
else: | ||
clean_content.append(line) | ||
|
||
with open(path, 'w') as f: | ||
for line in clean_content: | ||
f.write(line) | ||
|
||
|
||
if __name__ == '__main__': | ||
|
||
parser = argparse.ArgumentParser( | ||
description='Convert Jupyter notebook to Python script.', | ||
formatter_class=argparse.RawTextHelpFormatter) | ||
|
||
parser.add_argument('-i', '--input', | ||
required=True, | ||
help='Path to the Jupyter Notebook file') | ||
|
||
parser.add_argument('-o', '--output', | ||
required=True, | ||
help='Path to the Python script file') | ||
|
||
parser.add_argument('-v', '--version', | ||
action='version', | ||
version='v. 0.1') | ||
|
||
args = parser.parse_args() | ||
|
||
convert(input_path=args.input, | ||
output_path=os.path.splitext(args.output)[0]) | ||
|
||
cleanup(args.output) |
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
Oops, something went wrong.