Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic scroll down to bottom of result in ipython notebook

Is there a way to configure the ipython notebook so that whenever I print a long list, I automatically see the bottom?

for example, in the terminal, if I run the following:

for i in range(1000):
  print i

It automatically scrolls to the bottom:

992
993
994
995
996
997
998
999

In [2]: 

But in the Python notebook, I see the beginning and I have to manually scroll down to the last numbers.

I am running a long loop that takes a few seconds for each iteration, and it is inconvenient to have to scroll down whenever I want to check how far along the program is,

thank you,

like image 867
dleal Avatar asked Jan 09 '17 01:01

dleal


People also ask

How do you scroll output in a Jupyter Notebook?

You can try Cell -> Current Outputs -> Toggle Scrolling in the Jupyter UI to enable the scrolling for the output of one cell.

How do I auto scroll to the bottom in HTML?

To auto scroll a page from top to bottom we can use scrollTop() and height() method in jquery. In this method pass the document's height in scrollTop method to scroll.

What does %% capture do?

Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable. By default, %%capture discards these streams. This is a simple way to suppress unwanted output.

What does %% do in Jupyter Notebook?

Both ! and % allow you to run shell commands from a Jupyter notebook. % is provided by the IPython kernel and allows you to run "magic commands", many of which include well-known shell commands. ! , provided by Jupyter, allows shell commands to be run within cells.


2 Answers

Recommend: Tampermonkey plugin

(Once for all action!)
Copy & paste the following codes to any cell (or console,F12), run it.
After execution, you can delete the cell, then just continue your work!

%%javascript

window.scroll_flag = true
window.scroll_exit = false
window.scroll_delay = 100

$(".output_scroll").each(function() {
    $(this)[0].scrollTop = $(this)[0].scrollHeight;
});

function callScrollToBottom() {
    setTimeout(scrollToBottom, window.scroll_delay);
}

function scrollToBottom() {
    if (window.scroll_exit) {
        return;
    }
    if (!window.scroll_flag) {
        callScrollToBottom();
        return;
    };
    
    $(".output_scroll").each(function() {
        if (!$(this).attr('scroll_checkbox')){
            window.scroll_flag = true;
            $(this).attr('scroll_checkbox',true);
            var div = document.createElement('div');
            var checkbox = document.createElement('input');
            checkbox.type = "checkbox";
            checkbox.onclick = function(){window.scroll_flag = checkbox.checked}
            checkbox.checked = "checked"
            div.append("Auto-Scroll-To-Bottom: ");
            div.append(checkbox);
            $(this).parent().before(div);
        }
        
        $(this)[0].scrollTop = $(this)[0].scrollHeight;
    });
    callScrollToBottom();
}
scrollToBottom();

Or you can try jupyter_contrib_nbextensions's 'scroll-down' function.

like image 168
scruel Avatar answered Sep 21 '22 15:09

scruel


I use jupyter-notebook alot and didnt like that it didnt auto scroll to the bottom, so what I use is progressbar2, then you can do something like:

import progressbar
with progressbar.ProgressBar(max_value=1000) as bar:
    for idx, val in enumerate(range(1000)):
        bar.update(idx)

Then you will see one line output with more useful info like percentage complete, elapsed time, ETA,::

100% (1000 of 1000) |####################| Elapsed Time: 0:00:00 Time:  0:00:00
like image 37
jupiar Avatar answered Sep 19 '22 15:09

jupiar