Skip to content

Instantly share code, notes, and snippets.

@kinnala
Last active November 29, 2021 10:33
Show Gist options
  • Save kinnala/576d1bb00426afd6299aec6875f8025a to your computer and use it in GitHub Desktop.
Save kinnala/576d1bb00426afd6299aec6875f8025a to your computer and use it in GitHub Desktop.
Quickly parametrize Jupyter Notebooks and extract images and text outputs
# Add these lines to the first cell of Jupyter Notebook
# for argument support
try:
from _run_extract_args import arg
except Exception as e:
arg = [
"0", # nrefs
"P1", # udeg
"P0", # lamdeg
"stab", # "stab" enables stabilization
]
arg
# Call example:
# sh run_extract.sh notebook_name 1 P1 P0 stab
# Do not include the extension .ipynb!
#!/bin/bash
mkdir -p output
ARGU=`echo "${@:2:99}" | tr " " "_"`
rm output/$1$ARGU.celltext* || true
rm output/$1$ARGU.cellimage* || true
echo "arg = '${@:2:99}'.split(' ')" > _run_extract_args.py
cat _run_extract_args.py
jupyter nbconvert --to notebook --ExecutePreprocessor.timeout=600 --execute $1.ipynb
jq -r '.cells[].outputs[].data."text/plain" | select( . != null) | to_entries[] | .value' $1.nbconvert.ipynb | grep . > output/$1$ARGU.alltexts
jq -r '.cells[].outputs[].data."image/png" | select( . != null)' $1.nbconvert.ipynb | grep . > output/$1$ARGU.allimages
split -l 1 output/$1$ARGU.alltexts output/$1$ARGU.celltext
split -l 1 output/$1$ARGU.allimages output/$1$ARGU.cellimage
find . | grep output/$1$ARGU.cellimage | xargs -I{} sh -c 'base64 -d "{}" > "{}.png"; rm "{}"'
rm output/$1$ARGU.alltexts || true
rm output/$1$ARGU.allimages || true
mv $1.nbconvert.ipynb output/$1$ARGU.nbconvert.ipynb
rm _run_extract_args.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment