Reputation: 1563
Is it possible to avoid the red background color when the notebook prints logging output? For an example see this sample notebook.
http://nbviewer.ipython.org/gist/bjonen/1bd52a7d8a4761a74074
Upvotes: 22
Views: 8064
Reputation: 3431
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
.
Upvotes: 7
Reputation: 3457
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>
""")
Upvotes: 1
Reputation: 27803
That is because logging is logged to stderr
If you install the Stylus browser plugin you can change the CSS style to use a different color.
.jp-RenderedText[data-mime-type='application/vnd.jupyter.stderr'] {
background: var(--jp-cell-editor-background);
}
Is this helpful?
Upvotes: 0
Reputation: 40370
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)
Upvotes: 18