Here’s a little shell script I use to convert multiple Jupyter notebooks into formatted Python scripts:
# convert Jupyter Notebooks to Python scripts jupyter nbconvert --to script */*.ipynb # remove "# In " and multiple blank lines for f in */*.py; do sed -i -e '/^# In\[/d' $f cat -s $f > $f.txt mv $f.txt $f done # format scripts (+ max 79 characters per line) black -l 79 */*.py # move files to scripts directory mkdir -p temp 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, 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 *.