Skip to content

Instantly share code, notes, and snippets.

@heetbeet
Last active August 18, 2019 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heetbeet/7eb4482fe34a57b351e1fa1b9a95021c to your computer and use it in GitHub Desktop.
Save heetbeet/7eb4482fe34a57b351e1fa1b9a95021c to your computer and use it in GitHub Desktop.
A Jupyter header to convert the following code into a .py file.
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