Here’s a little shell script I use to convert multiple Jupyter notebooks into formatted Python scripts:

# sanitise the HTML notebooks (not needed often)
jupyter nbconvert --sanitize-html --to notebook --inplace */*.ipynb

# convert Jupyter Notebooks to Python scripts
jupyter nbconvert --to script */*.ipynb

# remove "# In []" and multiple blank lines in converted scripts
for f in */*.py;
do sed -i -e '/^# In\[/d' $f
cat -s $f > $f.txt
mv $f.txt $f
done

# format scripts and notebooks (+ max 79 characters per line)
black -l 79 **/*.py **/*.ipynb

# sort imports
isort **/*.py

# move files to scripts directory
mkdir -p scripts
mv */*.py scripts

This script assumes that the Jupyter notebooks are stored in a sub-directory, e.g. notebooks. The formatted Python scripts are then moved to another sub-directory, i.e. scripts. Black is used to format the scripts and notebooks and isort is used to sort the imports, while the conversion itself is performed using nbconvert.

If anyone has a better solution to this, I’d love to hear it!

Leave a comment

Your email address will not be published. Required fields are marked *.

Loading...