'How to selectively print only WARNING and/or ERROR in a Jupyter Notebook (as well as CoLab Notebook) cell?

Users can get frustrated with a lot of text output after executing a Jupyter Notebook cell. They can suppress the entire output by using magic command %%capture. However, sometime they are interested in ERROR/WARNING messages (if any), and disregard the rest.

Is it possible to selectively display ERROR/WARNING messages only? The trick should also work with Google CoLab's Notebook.

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source