Avoiding large output in notebooks
I am using the IPython notebook as a teaching tool (as do many others I imagine), and as part of this, students have to write simple programs using loops. Of course, as would be expected, students end up writing infinite loops, which if they contain a print statement will basically crash the web browser, and it's hard to explain to them how to kill the browser process, restart it, etc.
Would it be possible to have an option (or even have it by default) for the notebook that a single cell cannot print out more than a given number of lines or characters, to ensure that the notebook is more fool-proof against infinite loops? Of course, when not using print statements, students can simply interrupt the kernel, but if values are printed, then this can cause severe crashes.
I'd be fine with this being optional, and I could tell my students to enable it at the start of the course or notebook. Are there already ways to do this?
Probably not at baked in IPython,
Try the following in custom.js, which will drop (roughly) anything after 500 char/output. You could of course package it and refine. Would it suit you ?
require(["notebook/js/outputarea", "base/js/dialog","notebook/js/codecell"], function(oa, dialog, cc){
console.log(oa.OutputArea, oa.OutputArea.prototype)
oa.OutputArea.prototype._handle_output = oa.OutputArea.prototype.handle_output
console.log('[extension]: will drop ifmore than 10 messages/cell')
oa.OutputArea.prototype.handle_output = function (msg){
if(!this.count){this.count=0}
this.count = this.count+String(msg.content.data).length;
if(this.count>500){
if(!this.drop){
dialog.modal({
title : 'Infinite loop ? ',
body : "Oops, I got too many things from kernel ! I'll just drop them !",
buttons : {'OK' : { 'class' : 'btn-primary' },
}
}
)
}
this.drop=true;
return
}
return this._handle_output(msg);
}
cc.CodeCell.prototype._execute = cc.CodeCell.prototype.execute;
cc.CodeCell.prototype.execute = function(){
// reset counter on execution.
this.output_area.count = 0;
this.output_area.drop = false;
return this._execute();
}
});
@Carreau - thanks for the js code! I will try it out. However, this maybe be something to consider putting as a default menu option (like many terminals that have 'limit output to x lines'), since I'm not sure if I can easily get my students to install custom javascript if they are having issues with infinite loops ;) (and I suspect this is a common issue).
:+1: to @astrofrog's suggestion - when I was teaching with the notebook, the entire computing node sometimes froze on costly infinite loops, which froze everyone at the same time. It required me calling in the IT folks with superuser access to kill the offending Firefox instances.
I also think this belongs to IPython proper, instead of leaving it to users to deal with it. Qtconsole already has support to print up to a certain number of lines of output, which prevents apps like Spyder to freeze with infinite loops.
One of the issues we have right now with theses things is that we have no way to persist the configuration except custom.js. And until this is fixed, the answer to any of these question where the response might depend on use case will be no. So even if it makes sens to have a "reasonable" limit, I think we will have to postpone the implementation of this feature. It should push anyone interested in the project (us included) in implementing the config persistence. Then things like limiting output get easy.
In the meantime, if it is on nodes/clusters, as you (or your IT) is responsible for deployment, it should be relatively easy to add the custom extensions to all students. That's what @dsblank is doing for a course and IIRC they have highly customized notebook.
The multi-user notebook is taking shape so the need to ask student to install notebook get smaller. You can send profile in the folder that have material for the course and ask to launch notebook with the flag that user profile in CWD.
@Carreau - thanks - what is the preferred approach for adding a custom extension for all students if I have access to the IPython installation? (but not their .ipython directories)
We are using fairly straightforward notebooks, with an external kernel (using Scheme). But, we have provided some instruction for students to customize their notebooks [1]. Most of the customization I have needed for class, I just do in the kernel, and update in the installed codebase on the server. We are running the jupyterhub on our own servers [2]. This is really a nice way to go, once Jupyter is stable. Only one install... once it works, it mostly works for everyone.
I do think that IPython should not allow a user to crash the browser. Just like Python's max recursion limit, one shouldn't be allowed to output too much text in any one cell. I have had similar problems, but hadn't thought that it may have been caused by sheer volume of output.
If I wanted to install a particular nbextension, I could just copy it to their custom.js, as all of the accounts are on one computer. Or I could supply a very simple set of directions for them to copy paste, ala [1] instructions on installing the Spell Check (eg, combinations of !ipython install-nbextension and %%file.
Hope that helps! If not, I'd be glad to say more about our setup.
[1] - http://jupyter.cs.brynmawr.edu/hub/dblank/public/Jupyter%20Help.ipynb
[2] - http://jupyter.cs.brynmawr.edu/
Just a ping on this - I've had more students crash their notebooks this morning while learning loops (it's not easy for me to deploy a custom JS file to all of them)
Yeah, this really should be addressed.
@Carreau I am of the opinion that this should be in the notebook proper as well, and that there should be some high default limit -- I don't know, like 10000 lines of output -- that gets truncated past that. I think if anybody really wants that to be bigger than that, they are probably a power user, and should be able to go edit custom.js themselves. Students or anyone else just learning how to use the notebook are the most likely to make the mistake of creating an infinite for loop with output, which then crashes their browser, which is a horrible experience for them. Even worse, I've had students for whom this happened, and they had to restart their kernel, but in the process accidentally saved the notebook and then couldn't reopen it because there was too much output saved into it. That's somewhat of a separate issue, but still related -- it really shouldn't end up being the case that someone inadvertently saves the notebook while it's crashing and then can't open it again without manually going in a clearing the outputs.
Marking as 4.1. The limiting factor is likely appending to the DOM, we can probably count the bytes, and if greater than threshold, not append to the DOM, keep in a buffer, and append at the bottom of the cell "You seem to get a really long output, are you sure you want to display it ?" with 3 option "Yes, Append", "No, discard", "interrupt kernel", respectively for "I know what I am doing", "I just made a mistake, this is not an infinite loop", "Help me I don't know what I am doing".

Yes, please help this issue. See also comments on #6771
This is also an issue for our users at Quantopian, though I'd argue, as @ellisonbg mentions in #6771, that it should be the kernel's responsibility (with fallback in the frontend) to throttle output.
Part of the challenge here is that you probably want different size thresholds for different mimetypes. Sending back a 10MB PNG is much more reasonable than sending back 10MB of text output.
as @ellisonbg mentions in #6771, that it should be the kernel's responsibility (with fallback in the frontend) to throttle output.
Which I completely disagree with. The kernel is here to execute, and frontend include nbconvert. Kernel should not know what they are connected to, and should not be state-full as to wether or not they should retain information because this might not be what the user want.
When I go grocery shopping the cashier rarely tells me "this looks like too much food, I'll wait for your fridge to confirm me you have enough place".
Ans as you point out, even 100 Mb PNG can be nothing if it just go to /dev/null, because you are using the terminal console.
I disagree with how the title of #6771 was rewritten, but I don't want to bikeshed on how to name an issue.
The mixing of "We have talked" and the solution proposed in a single comment make it hard to distinguish who was "we", and wether the solution proposed is the reflexion of the team, or a single individual though on the problem raised by the team.
Wether it should be on the JS side, or the tornado is another question.
Ideally, the client would notify the server what it could handle. The server could then strip/truncate the cell output and replace them with references if need be. IIRC this is a bit tricky in canonical IPython since we treat the .ipynb as both document and datastore.
Wether it should be on the JS side, or the tornado is another question.
I could see an argument that this functionality belongs on the notebook server itself.
My concern with rate-limiting only in the frontend is that, in many cases, by the time messages make it to the frontend, it's already too late to prevent service degradation. Two cases where I might expect this to matter:
- In an environment with multiple notebook servers (e.g. tmpnb, or JupyterHub), outbound bandwidth can be a constrained resource. If the server is allowed to send arbitrarily large amounts of data back to the browser, not only to they crash their own frontend, they can degrade the experience of other users sharing the same infrastructure. This happens to a lesser extent in the simpler case of a single user running multiple notebooks concurrently.
- If the kernel produces a high volume of messages, the frontend might fall over even if it's dropping them as fast as it can.
This is probably one of those things where we should probably do it on both sides. The more robust version is on the kernel, because throttling there means that the traffic simply isn't produced. But that means all kernels need to implement a sensible scheme, and that simply won't happen. For this reason, we should also throttle on the frontend, since the first limiting factor is typically putting things on the DOM, and not network traffic. We should also be careful to take time and clearing into account. A notebook that produces 100MB of output over a very long at a reasonable pace with occasional clears should not get throttled.
I opened jupyter/notebook#506 with some server-side throttling and limiting the chunk-size of consecutive stream outputs that we consider for merging, which is typically the source of killing one's notebook.
As things currently stand, it is pretty easy to kill your browser with hasty print statements. When I use the notebook for teaching, this happens all the time. I agree with Min that we probably need multiple layers of this both in the kernel and the frontend.
Thanks for the PR on this @minrk
On Tue, Sep 29, 2015 at 4:11 AM, Min RK [email protected] wrote:
I opened jupyter/notebook#506 https://github.com/jupyter/notebook/pull/506 with some server-side throttling and limiting the chunk-size of consecutive stream outputs that we consider for merging, which is typically the source of killing one's notebook.
— Reply to this email directly or view it on GitHub https://github.com/ipython/ipython/issues/6516#issuecomment-144028540.
Brian E. Granger Associate Professor of Physics and Data Science Cal Poly State University, San Luis Obispo @ellisonbg on Twitter and GitHub [email protected] and [email protected]
@Carreau Thanks for your js code and it works for me exactly! I want to know whether it is valid for users to use this similar js code by nbextension? Thanks for your time!