如何批量执行时将多个Jupyter笔记本记录到同一个日志文件中?

时间:2018-05-03 08:10:57

标签: python logging jupyter-notebook

我在documentation here之后一个接一个地以编程方式执行多个Jupyter Notebook。下面是简短的代码细节。

我能够为每个笔记本和我执行其他笔记本的“主”笔记本创建一个日志文件。 如何让所有笔记本记录到同一个文件?

非常感谢提前!

读取和执行Jupyter笔记本的功能:

def read_in_notebook(notebook_fp):
    with open(notebook_fp) as f:
        nb = nbformat.read(f, as_version=4)
    return nb

def execute_notebook(notebook_filename,timeout=60000, allow_errors=False):

    nb = read_in_notebook(notebook_filename)
    ep = ExecutePreprocessor(allow_errors = allow_errors, timeout=timeout, 
                            kernel_name='python3')
    try:
        logging.info('We are loading {0}.'.format(notebook_filename))
        out = ep.preprocess(nb, {})
    except CellExecutionError as error:
        logging.warning('We have run into {0} in file {1}.'.format(error, 
                       notebook_filename))
    finally:
        nbformat.write(nb, open(notebook_filename, mode='w'))

循环通过Jupyter笔记本并逐个执行:

for file in files_:
     execute_notebook(file)

0 个答案:

没有答案