Formatter for Jupyter notebooks
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.
So, we will be using a python library called nbQA along with code formatters like Black and isort.
Install the library using:
$ pip install nbqa
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.
Format the notebook using black as shown below:
$ nbqa black notebook.ipynb
All done! ✨ 🍰 ✨
1 files reformatted.
Similarly, format the notebook using isort:
$ nbqa isort notebook.ipynb
$ nbqa yapf --in-place notebook.ipynb
$ nbqa autopep8 -i notebook.ipynb
To format the markdown cells in your notebook, use:
$ nbqa mdformat notebook.ipynb --nbqa-md --nbqa-diff
To run tests for iPython notebooks using doctypes:
$ nbqa doctest notebook.ipynb
I hope you liked it. That's all for this time.