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 --ClearOutputPreprocessor.enabled=True */*.ipynb
# 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!