Skip to content

Instantly share code, notes, and snippets.

@Smarker
Created October 23, 2019 17:57
Show Gist options
  • Save Smarker/5b49faf61dd6b1b4fd1857ebc10f7841 to your computer and use it in GitHub Desktop.
Save Smarker/5b49faf61dd6b1b4fd1857ebc10f7841 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -euo pipefail
IPYNB_FILE_PATHS=`git diff --staged --name-only | awk "/.ipynb/"`
PROJECT_ROOT_PATH=`git rev-parse --show-toplevel`
function get_script_extension() {
file_path=$1
echo $(cat $file_path | jq -r '.metadata.file_extension')
}
if [[ -n "$IPYNB_FILE_PATHS" ]]; then
echo "Saving changes to these notebooks: $IPYNB_FILE_PATHS"
for ipynb_file_path in $IPYNB_FILE_PATHS
do
file_no_extension="${ipynb_file_path%.*}"
# if file exists and is not a checkpoint file
if [[ -f "$ipynb_file_path" ]] && [[ "$ipynb_file_path" != *"checkpoint"* ]]; then
echo "Creating script from notebook..."
jupyter nbconvert --to script "$ipynb_file_path"
extension=$(get_script_extension $PROJECT_ROOT_PATH/$ipynb_file_path)
mv -f "$PROJECT_ROOT_PATH/$file_no_extension$extension" "$PROJECT_ROOT_PATH/$SCRIPTS_PATH"
echo "Creating html from notebook..."
jupyter nbconvert --to html "$ipynb_file_path"
mv -f "$PROJECT_ROOT_PATH/$file_no_extension.html" "$PROJECT_ROOT_PATH/$HTML_PATH"
echo "Clearing notebook output..."
jupyter nbconvert --ClearOutputPreprocessor.enabled=True --inplace "$ipynb_file_path"
fi
done
fi
# add the newly created files to the current commit
git add -A
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment