Avoid red background color for logging output in Ipython Notebook
This snippet worked for me. I hated the red background and wanted it to have another color. Here I change the background color to green, but you can set the CSS to whatever you want:
from IPython.core.display import HTML
HTML("""
<style>
div.output_stderr {
background: #0C0;
}
</style>
""")
The red background highlights output sent to stderr as opposed to stdout. To avoid it, you could send your logging to stdout:
ch = logging.StreamHandler(sys.stdout)
The highlighted answer did not work for me. Jupyter Notebook seems to set up the logger before any imports are done. To redirect any stderr to stdout, I had to first reload the logging module:
import sys
import logging
from importlib import reload
reload(logging)
logging.basicConfig(stream=sys.stdout, format='',
level=logging.INFO, datefmt=None)
log = logging.getLogger(__name__)
log.info("This should print as normal output in Jupyter, and not in a red box")
This can also be included in a package being imported to Jupyter Notebook, e.g. with import my_dev_package
- and prevent log.Info being shown as red box. To only run this in Jupyter Mode (not console) for the package, add a check: if not type(sys.stdout) == io.TextIOWrapper: # Jupyter Mode
.