Skip to content

Instantly share code, notes, and snippets.

@Orfeous
Created August 27, 2019 23:10
Show Gist options
  • Save Orfeous/1df82e99728b3f89b47d6a913df5cc8a to your computer and use it in GitHub Desktop.
Save Orfeous/1df82e99728b3f89b47d6a913df5cc8a to your computer and use it in GitHub Desktop.
Convert Jupyter notebooks to Python scripts without the need to install jupyter.
import sys,json
f = open(sys.argv[1], 'r', encoding="utf8") #input.ipynb
j = json.load(f)
of = open(sys.argv[2], 'w', encoding="utf8") #output.py
if j["nbformat"] >=4:
for i,cell in enumerate(j["cells"]):
of.write("#cell "+str(i)+"\n")
for line in cell["source"]:
of.write(line)
of.write('\n\n')
else:
for i,cell in enumerate(j["worksheets"][0]["cells"]):
of.write("#cell "+str(i)+"\n")
for line in cell["input"]:
of.write(line)
of.write('\n\n')
of.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment