Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a vertical_overflow='crop_above' as an option to Live() #3637

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

cpsievert
Copy link

Type of changes

  • Bug fix
  • New feature
  • Documentation / docstrings
  • Tests
  • Other

Checklist

  • I've run the latest black with default args on new code.
  • I've updated CHANGELOG.md and CONTRIBUTORS.md where appropriate.
  • I've added tests for new code.
  • I accept that @willmcgugan may be pedantic in the code review.

Description

As described in #3263, Live(vertical_overflow="visible") has the unfortunate behavior of duplicating content when the content update exceeds the height of the console. This is especially unfortunate for a few reasons:

  • It seems there is no way to get rid of the duplicated content (i.e., transient=True doesn't help to avoid this).
  • There are no other vertical_overflow options that allows for the "newest" content to be visible.
  • In today's Gen AI world where markdown often is received in chunks, it's really useful to have a live display where a (possibly long) markdown string accumulates over time.

This PR proposes a new option vertical_overflow="crop_above" which does the reverse of vertical_overflow="crop" (it displays only the bottom portion of the content instead of the top). It has the nice behavior of always making the "newest" content visible, but without the downside of duplicated content. Here's a demo:

import requests
import time

from rich.live import Live
from rich.markdown import Markdown

readme = requests.get(
    "https://raw.githubusercontent.com/posit-dev/py-shiny/refs/heads/main/README.md"
)
readme_chunks = readme.text.replace("\n", " \n ").split(" ")[: 200]

content = ""
with Live(auto_refresh=False, vertical_overflow="crop_above") as live:
    for chunk in readme_chunks:
        content += chunk + " "
        time.sleep(0.01)
        live.update(Markdown(content), refresh=True)
rich-crop-above.mp4

And note that if you change vertical_overflow="crop_above" to vertical_overflow="visible", this is the behavior:

rich-visible.mp4

I'm happy to write tests or anything else you need if you like this overall direction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant