Created
August 27, 2019 23:10
-
-
Save Orfeous/1df82e99728b3f89b47d6a913df5cc8a to your computer and use it in GitHub Desktop.
Convert Jupyter notebooks to Python scripts without the need to install jupyter.
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 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