tartartartartar
tartartartartar

Reputation: 21

Suppress Jupyter Notebook warning messages in output

I am using papermill and nbconvert to generate HTML files out of my jupyter notebooks.

I recently moved onto a new container (RHEL 8 linux). In this new infra, I continuously get a warning message in every cell, big and pink and ugly:

WARNING:monitoring_extension:Issue posting to monitoring service: Can't identify the notebook path.

I have tried to suppress the warning using libraries 'warnings' and 'shutup' and 'IPython.core.display' -> clear_output. Nothing works! This warning cannot be suppressed!

Anyone have any advice? It seems to only occur with nbconvert, if I run the notebook normally, no warning.

papermill==2.5.0 jupyter==1.0.0

Ran notebook, got HTML output, full of pink warning text. Tried:

import warnings
warnings.filterwarnings('ignore')
warnings.warn_explicit = warnings.warn = lambda *_, **__: None

Tried: shutup.please()

Tried: clear_output(wait=False) at the end of every cell.

Upvotes: 2

Views: 94

Answers (2)

tartartartartar
tartartartartar

Reputation: 21

I managed to hide the error messages by inserting the following CSS into the page:

.jp-RenderedText[data-mime-type='application/vnd.jupyter.stderr'] {
   display: none;
   visibility: hidden;
}

Obviously, this treats the symptom and not the root, alas.

Upvotes: 0

Wayne
Wayne

Reputation: 9954

Here's an idea to treat the symptom. (Treating the cause would be best unless you are happy other wise? jupyter==1.0.0 looks wrong to me or at least not providing much information. So if you wanted to consider that direction, how did you install Jupyter?) I wish I could post this code block below as a comment, but I need formatting.
Please try posting this at the top of your notebook to be run.

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())

Sadly, I cannot test. But it may help address it by filtering out that message from stderr. If it even seems to do anything it may be on the right track for ultimately filtering it.

Upvotes: 0

Related Questions