问题
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,
回答1:
(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.
回答2:
I don't think there is a way to do this with iPython, however, if you use Pandas, you can use the tail function to print only the last records.
You can even import your data into Pandas from Python native tuples:
import pandas as pd
df = pd.DataFrame.from_records(list(range(1000), columns = (id,))
If you don't want to use Pandas, you can collect all items into a list and then only print the last 10 records:
print(list(range(1000))[-10])
回答3:
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
来源:https://stackoverflow.com/questions/41539841/automatic-scroll-down-to-bottom-of-result-in-ipython-notebook