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?
Jupyter modifies the sys.stderr
and sys.stdout
streams so they no longer point to the "standard" stderr
and stdout
(pun intended). However, they store a copy to the original file handle under _original_stdstream_copy
, and you can create a log handler that explicitly writes to the original stream.
def console_handler(stream='stdout'):"""Create a handler for logging to the original console."""assert stream in {'stdout', 'stderr'}, "stream must be one of 'stdin' or 'stdout'"# Get the file handle of the original std stream.fh = getattr(sys, stream)._original_stdstream_copy# Create a writable IO stream.stream = io.TextIOWrapper(io.FileIO(fh, 'w'))# Set up a stream handler.return logging.StreamHandler(stream)
You can use the function as follows.
import logginglogging.basicConfig(...) # Or some more sophisticated setup.
logger = logging.getLogger('myLogger')
logger.add_handler(console_handler())
logger.error('something went wrong') # This will write to stderr.