Last active
August 18, 2019 16:44
-
-
Save heetbeet/7eb4482fe34a57b351e1fa1b9a95021c to your computer and use it in GitHub Desktop.
A Jupyter header to convert the following code into a .py file.
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
import __main__ as main | |
if not hasattr(main, '__file__'): | |
jupyter_name = 'your-jupyter-filename' | |
import os | |
import subprocess | |
subprocess.call(['jupyter', | |
'nbconvert', | |
'--to', | |
'script', | |
jupyter_name+'.ipynb']) | |
pytxt = open(jupyter_name+'.py').read() | |
with open(jupyter_name+'.py', 'w') as f: | |
strt = False | |
outtxt = [] | |
for line in pytxt.split('\n'): | |
if '### start py ###'.replace(' ','') in line: | |
strt = True | |
continue | |
if not strt: continue | |
if line.startswith('# In['): continue | |
if line.startswith('# Out['): continue | |
outtxt.append(line) | |
f.write('\n'.join(outtxt)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment