Skip to content

Instantly share code, notes, and snippets.

@jonahpearl
Last active March 20, 2023 19:18
Show Gist options
  • Save jonahpearl/5bd84ee032911ee0b0c32f773a9b511e to your computer and use it in GitHub Desktop.
Save jonahpearl/5bd84ee032911ee0b0c32f773a9b511e to your computer and use it in GitHub Desktop.
Re-indent directory of ipynb's
#!/bin/zsh
# Will convert all files in current dir, recursively. Could be modified to take args but I'm lazy!
for filename in ./**/*.ipynb; do
# Split the file name
extension="${filename##*.}";
filename_noext="${filename%.*}";
# Convert ipynb to text
jupytext --to py -o "${filename_noext}_script.py" "${filename}"
# Re-indent (eg 2 spaces per tab --> 4 spaces per tab)
reindent -rn "${filename_noext}_script.py" # recursive, no backup
# Re-make ipynb from re-indented script
jupytext --to ipynb -o "${filename_noext}.ipynb" "${filename_noext}_script.py"
# Remove intermediate output
rm "${filename_noext}_script.py"
done
@jonahpearl
Copy link
Author

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