3

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?

1

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.