Skip to content

Instantly share code, notes, and snippets.

@keineahnung2345
Created November 28, 2020 03:11
Show Gist options
  • Save keineahnung2345/38dda2c61c49169e775c9d80648e0d44 to your computer and use it in GitHub Desktop.
Save keineahnung2345/38dda2c61c49169e775c9d80648e0d44 to your computer and use it in GitHub Desktop.
A python script to parse commands and outputs from jupyter notebook
import json
nb_fname = "xxx.ipynb"
with open(nb_fname, "r", encoding="utf-8") as f:
nb = json.load(f)
cells = nb['cells']
print("There are", len(cells), "cells")
for cell in cells:
source = ''.join(cell['source'])
print(source)
outputs = cell['outputs']
if len(outputs) == 0:
continue
try:
text = ''.join(outputs[0]['data']['text/plain'])
except:
text = ''.join(outputs[0]['text'])
print(text)
print("===========================")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment