python – Hide all warnings in ipython

The Question :

343 people think this question is useful

I need to produce a screencast of an ipython session, and to avoid confusing viewers, I want to disable all warnings emitted by warnings.warn calls from different packages. Is there a way to configure the ipythonrc file to automatically disable all such warnings?

The Question Comments :
  • note when you can you should accept your answer, and then go check on your other questions and see if they have any acceptable answers.

The Answer 1

822 people think this answer is useful

I eventually figured it out. Place:

import warnings

inside ~/.ipython/profile_default/startup/ I’m leaving this question and answer for the record in case anyone else comes across the same issue.

Quite often it is useful to see a warning once. This can be set by:


The Answer 2

61 people think this answer is useful

I hide the warnings in the pink boxes by running the following code in a cell:

from IPython.display import HTML
function code_toggle_err() {
 if (code_show_err){
 } else {
 code_show_err = !code_show_err
$( document ).ready(code_toggle_err);
To toggle on/off output_stderr, click <a href="javascript:code_toggle_err()">here</a>.''')

The Answer 3

5 people think this answer is useful

The accepted answer does not work in Jupyter (at least when using some libraries).

The Javascript solutions here only hide warnings that are already showing but not warnings that would be shown in the future.

To hide/unhide warnings in Jupyter and JupyterLab I wrote the following script that essentially toggles css to hide/unhide warnings.

(function(on) {
const e=$( "<a>Setup failed</a>" );
const ns="js_jupyter_suppress_warnings";
var cssrules=$("#"+ns);
if(!cssrules.length) cssrules = $("<style id='"+ns+"' type='text/css'>div.output_stderr { } </style>").appendTo("head"); {
    var s='Showing';  
    if(on) {
        cssrules.append("div.output_stderr, div[data-mime-type*='.stderr'] { display:none; }");
    e.text(s+' warnings (click to toggle)');

The Answer 4

2 people think this answer is useful

For jupyter lab this should work (@Alasja)

from IPython.display import HTML
var code_show_err = false; 
var code_toggle_err = function() {
 var stderrNodes = document.querySelectorAll('[data-mime-type="application/vnd.jupyter.stderr"]')
 var stderr = Array.from(stderrNodes)
 if (code_show_err){
     stderr.forEach(ele => = 'block');
 } else {
     stderr.forEach(ele => = 'none');
 code_show_err = !code_show_err
document.addEventListener('DOMContentLoaded', code_toggle_err);
To toggle on/off output_stderr, click <a onclick="javascript:code_toggle_err()">here</a>.''')

Add a Comment