Robert Lugg
Robert Lugg

Reputation: 1192

Show last output of ipywidget Output widget

I am using ipywidget's 'Output' widget. As text is sent to it, scrollbars appear as the amount exceeds what can be shown. At that point, only the first lines of text are shown. Instead, as new text is added, I want it to auto-scroll such that the last line printed is always shown.

The current behavior: enter image description here How can I make the last line of output displayed as it is printed?

Upvotes: 3

Views: 694

Answers (2)

PNS Vinod Kumar
PNS Vinod Kumar

Reputation: 21

Try to click on the rows and see the scrolling happen automatically, without any flickering.

from ipydatagrid import DataGrid
import pandas as pd
from IPython.display import display, HTML
import ipywidgets as wid

data = {
    'ID': [1, 2, 3, 4, 5],
    'Name': ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve'],
    'Age': [25, 30, 35, 40, 45]
}
df = pd.DataFrame(data)

datagrid = DataGrid(
    df,
    selection_mode='row',
    editable=False,
)

out = wid.Output()
out.add_class("my-output")

app = wid.HBox([
    datagrid,
    out,
])
display(app)

display(HTML("""
<style>
.my-output {
    min-width: 40%;
    max-height: 500px;
    border: 1px solid black;
    border-radius: 10px;
    overflow: scroll;
}
</style>
<script>
var outputDiv = document.querySelector(".my-output")

var observer = new MutationObserver(() => {
    outputDiv.scrollTop = outputDiv.scrollHeight
})

observer.observe(outputDiv, {childList: true, characterData: true, subtree: true})
</script>
"""))

def on_selection_change(change):
    out.append_stdout(", ".join([str(x) for x in datagrid.selected_cell_values]))
    out.append_stdout("\n")

datagrid.observe(on_selection_change, names='selections')

Upvotes: 0

above_c_level
above_c_level

Reputation: 3949

Currently there is no general solution to the scroll_to_bottom problem. See: https://github.com/jupyter-widgets/ipywidgets/issues/1815

In the discussion there seems to be a linux specific solution which I cannot verify.

The issue is on the milestone list of ipywidgets https://github.com/jupyter-widgets/ipywidgets/milestone/2

Upvotes: 2

Related Questions