Vic*_*tor 7 python jupyter jupyter-notebook nbconvert jupyter-lab
我有一个 Jupyter 笔记本,需要从命令行运行。为此,我有以下命令:
jupyter nbconvert --execute my_jupyter_notebook.ipynb --to python
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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?
小智 1
以下代码捕获 nbconvert 执行期间产生的警告和异常并将其传递给记录器。Jupyter 和 nbconvert 使用不同的方式处理异常。
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2084 次 |
| 最近记录: |