Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging while nbconvert execute

I have a Jupyter notebook that needs to run from the command line. For this I have the following command:

jupyter nbconvert --execute my_jupyter_notebook.ipynb --to python

This command creates a python script and then executes it. However, I'm using the logging library in Python to log certain events. When it executes the script from the command above, nothing can be seen on the terminal.

However, when I execute manually the converted jupyter, like below, I can see all the logs on my terminal:

python3 my_jupyter_notebook.py

I've tried adding extra arguments like --debug and --stdout but those just output all the code, not just the logs. Is it possible to output on the terminal the results of logging while doing an nbconvert execute command?

like image 375
Victor Avatar asked Nov 08 '22 05:11

Victor


1 Answers

Here is a code that catch warning and exception produced during execution of nbconvert and pass them to the logger. Jupyter and nbconvert use a different way of handling exceptions.

from logging import getLogger
import sys
import traceback
import warnings
import IPython
import logging

logger = getLogger(name)
logging.basicConfig(stream=sys.stdout, level=logging.WARNING)

# Catch Traceback 
def showtraceback(self):
    traceback_lines = traceback.format_exception(*sys.exc_info())
    del traceback_lines[1]
    message = ''.join(traceback_lines)
    logger.error(traceback_lines[-1] + str(message))
IPython.core.interactiveshell.InteractiveShell.showtraceback = showtraceback

# Catch Warning 
def warning_on_one_line(message, category, filename, lineno, file=None, line=None):
    logger.warning(str(message) + '\n' + str(filename) + ' : ' + str(lineno))
    return '%s:%s: %s:%s\n' % (filename, lineno, category.__name__, message)
warnings.formatwarning = warning_on_one_line
like image 183
Pierre Avatar answered Nov 14 '22 22:11

Pierre