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?
Asked
Active
Viewed 4.2e+01k times
371
![](../../users/profiles/180783.webp)
astrofrog
- 26,293
- 28
- 81
- 123
-
3note when you can you should accept your answer, and then go check on your other questions and see if they have any acceptable answers. – Dan D. Jan 27 '12 at 10:38
4 Answers
898
I eventually figured it out. Place:
import warnings
warnings.filterwarnings('ignore')
inside ~/.ipython/profile_default/startup/disable-warnings.py
. 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:
warnings.filterwarnings(action='once')
-
9
-
7@FrozenFlame, you change it back to 'default' See: https://docs.python.org/2/library/warnings.html – AZhao Jul 13 '15 at 17:23
-
41You can also execute this in the a notebook, in order to suppress only (certain) warnings in one specific notebook – Vasco Nov 11 '15 at 14:13
-
Something odd happens to me, I work on Kaggle notebook, and even if I set `warnings.filterwarnings('ignore')` at the beginning of my script, I get warnings anyway. Should it be related to the fact that I use TPU accelerator, does TPU have a particular behaviour in this case ... I wonder. I do not understand. – Catalina Chircu Mar 05 '20 at 14:51
62
I hide the warnings in the pink boxes by running the following code in a cell:
from IPython.display import HTML
HTML('''<script>
code_show_err=false;
function code_toggle_err() {
if (code_show_err){
$('div.output_stderr').hide();
} else {
$('div.output_stderr').show();
}
code_show_err = !code_show_err
}
$( document ).ready(code_toggle_err);
</script>
To toggle on/off output_stderr, click <a href="javascript:code_toggle_err()">here</a>.''')
![](../../users/profiles/286795.webp)
matthiash
- 2,235
- 2
- 19
- 29
-
2Despite the downvote, still believe it was a useful tip, as it's easier to toggle, compared with the accepted answer. – matthiash Apr 10 '17 at 14:22
-
I love this solution. I saved in an external module and added to the imports. – Adam Oct 17 '18 at 07:32
-
3This no longer works with jupyter lab (although it would be cool if it would:) – Ajasja Jan 18 '19 at 00:55
-
1It worked ! the accepted answer didn't help me. Thank you @matthiash – Gonzalo Garcia Aug 30 '19 at 17:03
6
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.
%%javascript
(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");
e.click(function() {
var s='Showing';
cssrules.empty()
if(on) {
s='Hiding';
cssrules.append("div.output_stderr, div[data-mime-type*='.stderr'] { display:none; }");
}
e.text(s+' warnings (click to toggle)');
on=!on;
}).click();
$(element).append(e);
})(true);
![](../../users/profiles/1417396.webp)
robert
- 888
- 7
- 16
-
-
@Ben are you using jupyter in the browser? Notebooks in VSCode do not load $ (jQuery) – robert Feb 20 '20 at 16:31
-
-
-
Doesn't work for me either. I'm getting the same error as Ben (I'm using JupyterLab). – Tobias Bergkvist May 28 '20 at 03:36
2
For jupyter lab this should work (@Alasja)
from IPython.display import HTML
HTML('''<script>
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 => ele.style.display = 'block');
} else {
stderr.forEach(ele => ele.style.display = 'none');
}
code_show_err = !code_show_err
}
document.addEventListener('DOMContentLoaded', code_toggle_err);
</script>
To toggle on/off output_stderr, click <a onclick="javascript:code_toggle_err()">here</a>.''')
![](../../users/profiles/11868165.webp)
ottowg
- 21
- 1