Skip to content

Instantly share code, notes, and snippets.

@jfeist
Last active December 20, 2017 13:26
Show Gist options
  • Save jfeist/cd00aa3b681092e1d5dc to your computer and use it in GitHub Desktop.
Save jfeist/cd00aa3b681092e1d5dc to your computer and use it in GitHub Desktop.

Small script to flatten an IPython notebook (much like https://gist.github.com/takluyver/bc8f3275c7d34abb68bf), using jq instead of python. It is significantly faster. Put it somewhere in your path and make it executable. You can call it with nbflatten.jq --arg show_output 0 to suppress showing outputs (by default they are shown).

#!/bin/sh
jq -r 'def banner: "\(.) " + (28-(.|length))*"-";
("Non-cell info" | banner), del(.cells), "",
(.cells[] | ("\(.cell_type) cell" | banner),
"\(.source|add)",
if ($show_output == "1") then
"",
( select(.cell_type=="code" and (.outputs|length)>0) |
("output" | banner),
(.outputs[] |
(select(.text) | "\(.text|add)" | rtrimstr("\n")),
(select(.traceback) | (.traceback|join("\n"))),
(select(.text or .traceback|not) | "(Non-plaintext output)")
),
""
)
else ""
end
)' "$@" --arg show_output 1
@axsk
Copy link

axsk commented Feb 24, 2016

this is amazing, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment