Skip to content

Instantly share code, notes, and snippets.

@mlgill
Last active July 23, 2018 04:04
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mlgill/5c55253a3bc84a96addf to your computer and use it in GitHub Desktop.
Save mlgill/5c55253a3bc84a96addf to your computer and use it in GitHub Desktop.
Bash/Zsh functions for viewing/grepping only the input code of an IPython notebook and for clearing output cells.
# Two very basic functions for searching just the input code of an IPython notebook
# Written because I often want to search notebooks for snippets but the giant output
# of embedded encoded images makes it difficult.
# Ipython's nbconvert can be used to extract just the input, but this requires
# writing to a separate file and can be quite slow when used with large notebooks.
# Additionally, find/xargs can be used with igrep when the name of the notebook isn't known.
# icat could be used to convert an IPython notebook to a standard python file if
# the notebook does not contain whole-cell magics.
# Michelle L. Gill, 2014/06/21
# Both functions require jq for parsing the IPython notebook json: http://stedolan.github.io/jq/manual/
# icat also optionally uses pygments, which is an IPython dependency http://pygments.org/docs/cmdline/
function icat {
jq '.worksheets[].cells[] | select(.cell_type=="code") | .input[]' $1 \
| sed 's/^"//g;s/"$//g;s/\\n$//g;s/\\"/"/g;s/\\\\/\\/g' \
| pygmentize -l python
}
# File name is specified last and all other arguments are assumed to belong to grep
# This approximates grep's standard behavior
# The slicing of $argv may not work in Bash. Honestly, you should be using zsh though.
function igrep {
jq '.worksheets[].cells[] | select(.cell_type=="code") | .input[]' $argv[-1] \
| sed 's/^"//g;s/"$//g;s/\\n$//g;s/\\"/"/g;s/\\\\/\\/g' \
| grep $argv[1,-2]
}
# This simple function will clear the output cells in a notebook. This can then be
# redirected into a new file from the command line
function clearoutput {
jq '. | .worksheets[].cells[].outputs=[] ' $1
}
@jbarratt
Copy link

Thank you for sharing this technique!

I often want to know "what the heck notebook did I figure out how to do such-and-such in?" So I wrote a little wrapper script that uses mdfind (to access the OSX spotlight database) to find all the .ipynb files, then searches their code (using your extractor) for a given pattern, and displaying matches.

https://gist.github.com/jbarratt/fa1d3473048e5f856aeb

@mlgill
Copy link
Author

mlgill commented Aug 22, 2014

Hello! Sorry for my slow reply--I've been in the process of moving and starting a new job and somehow I missed this post. Glad this was useful and your modifications are really nice!

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