Introduction
If you are into Data Science or Machine Learning, you have probably come across jupyter notebooks (.ipynb files). The problem I faced when using jupyter notebooks was that the black formatter didn't work on them. I had tried using the
$ black notebook.ipynb
command many times. This article is meant to help with code formatting in Python Notebooks.
nbQA
So, we will be using a python library called nbQA along with code formatters like Black and isort.
Installation
Install the library using:
$ pip install nbqa
Usage
You can use various formatters along with nqba and I will demonstrate how to use a few of them. before trying the formatters, make sure you have installed them already.
black
Format the notebook using black as shown below:
$ nbqa black notebook.ipynb
reformatted notebook.ipynb
All done! ✨ 🍰 ✨
1 files reformatted.
isort
Similarly, format the notebook using isort:
$ nbqa isort notebook.ipynb
Fixing notebook.ipynb
yapf
$ nbqa yapf --in-place notebook.ipynb
autopep8
$ nbqa autopep8 -i notebook.ipynb
mdformat
To format the markdown cells in your notebook, use:
$ nbqa mdformat notebook.ipynb --nbqa-md --nbqa-diff
doctest
To run tests for iPython notebooks using doctypes:
$ nbqa doctest notebook.ipynb
Top comments (2)
Hey Siddhesh thanks so much for sharing this tip! I had no idea this tool existed but will definitely check it out
I looove Jupyter for sketching out ideas, exploring data, and communicating POC's but have always struggled to integrate it into my production workflows because I always found it fairly unmaintainable -
nbQA
is exactly the type of thing I've needed to up my Jupyter game, cheers :DI can't tell how much this tool has helped me. Do try it. Will try to post about more tools in the future.