抑制输出中的 Jupyter Notebook 警告消息

问题描述 投票:0回答:1

我正在使用 papermill 和 nbconvert 从我的 jupyter 笔记本中生成 HTML 文件。

我最近转移到一个新容器(RHEL 8 linux)。在这个新的基础设施中,我在每个单元格中不断收到一条警告消息,大的、粉红色的、丑陋的:

警告:monitoring_extension:发布到监控服务的问题:无法识别笔记本路径。

我尝试使用库“warnings”和“shutup”以及“IPython.core.display”->clear_output 来抑制警告。什么都不起作用!该警告无法被抑制!

有人有什么建议吗?它似乎只发生在 nbconvert 上,如果我正常运行笔记本,没有警告。

造纸厂==2.5.0 jupyter==1.0.0

运行笔记本,得到 HTML 输出,充满粉红色警告文本。 尝试过: 进口警告 warnings.filterwarnings('忽略') warnings.warn_explicit = warnings.warn = lambda *_, **__: 无 尝试过: 请闭嘴() 尝试过: 每个单元格末尾的clear_output(wait=False)。

python jupyter nbconvert papermill
1个回答
0
投票

这里有一个治疗症状的方法。 (处理原因是最好的,除非你在其他方面感到高兴?

jupyter==1.0.0
在我看来是错误的,或者至少没有提供太多信息。所以如果你想考虑这个方向,你是如何安装Jupyter的?)我希望我可以发布下面的代码块作为注释,但我需要格式化。
请尝试将其发布到要运行的笔记本顶部。

import sys
from IPython import get_ipython

class WarningFilter:
    def __init__(self):
        self.old_stderr = sys.stderr
        sys.stderr = self

    def write(self, output):
        if "WARNING:monitoring_extension:Issue posting to monitoring service: Can't identify the notebook path." not in output:
            self.old_stderr.write(output)

    def flush(self):
        self.old_stderr.flush()

# Install the custom stderr handler
WarningFilter()

# Ensure the filter is used in all new cells
ip = get_ipython()
if ip is not None:
    ip.events.register('pre_run_cell', lambda: WarningFilter())

遗憾的是,我无法测试。但它可以通过过滤掉来自 stderr 的消息来帮助解决这个问题。如果它似乎做了任何事情,那么它可能走在最终过滤它的正确轨道上。

© www.soinside.com 2019 - 2024. All rights reserved.