[go: nahoru, domu]

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

[BUG] Lines are not properly overwritten in multiline Live panels from within a Jupyter cell #2929

Open
2 tasks done
Yomguithereal opened this issue Apr 19, 2023 · 5 comments

Comments

@Yomguithereal
Copy link
Yomguithereal commented Apr 19, 2023

I develop a CLI tool and display complex progress bars using a rich Live panel. A lot of people are using the CLI tool directly from jupyter notebook cells using the ! sigil e.g.:

!minet fetch url urls.csv > result.csv

This is what I observe:

minet-rich

The lines are not properly replaced and the progress bar rolls on.

I know this is a strange thing to ask but I cannot find a way around this. The console properly detects the shell env and the colors work properly. is_terminal and is_interactive is True from within the cli program. If I force_jupyter=True if fails and this is expected of course.

tqdm does not have this problem so I guess it revolves around the way lines are erased or replaced at the lowest level and maybe it cannot be safely done without some performance compromise.

Sorry if I did not find the proper solution by scouting the different console kwargs/docs etc.

Platform

  • Ubuntu 22
  • Python 3.8.16
  • rich 13.3.4
  • jupyter 1.0.0
  • ipywidgets 8.0.6
@github-actions
Copy link

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This is an automated reply, generated by FAQtory

@Yomguithereal Yomguithereal changed the title Line are not properly replaced in Live panels from within a Jupyter cell Lines are not properly overwritten in Live panels from within a Jupyter cell Apr 19, 2023
@willmcgugan
Copy link
Collaborator

To help you I will need example code to reproduce the issue. It would also help if you made an attempt to fill in the issue template.

@Yomguithereal Yomguithereal changed the title Lines are not properly overwritten in Live panels from within a Jupyter cell [BUG] Lines are not properly overwritten in Live panels from within a Jupyter cell Apr 19, 2023
@Yomguithereal
Copy link
Author
Yomguithereal commented Apr 19, 2023

@willmcgugan I have added the relevant information from the template. This case is a bit tricky because rich runs from a shell that is being piped within a notebook cell.

I have tested some other things to narrow the issue and it only happens with multiline Live panels and it's possible there is not solution because Jupyter shell emulation is not good enough.

Here is something that works:

Cell 1

CODE = """
import time
from rich.progress import track
from rich.console import Console

console = Console()

for i in track(range(20), description="Processing...", console=console):
    time.sleep(1)  # Simulate work being done
"""

with open('./test-rich.py', 'w') as f:
    f.write(CODE)

Cell 2

!python test-rich.py

Here is something that does not work:

Cell 1

CODE = """
import time

from rich.progress import Progress

with Progress() as progress:

    task1 = progress.add_task("[red]Downloading...", total=1000)
    task2 = progress.add_task("[green]Processing...", total=1000)

    while not progress.finished:
        progress.update(task1, advance=0.5)
        progress.update(task2, advance=0.3)
        time.sleep(0.02)
"""

with open('./test-rich.py', 'w') as f:
    f.write(CODE)

Cell 2

!python test-rich.py

I am trying to upload a notebook but GitHub does not let me do so from an issue.

@Yomguithereal Yomguithereal changed the title [BUG] Lines are not properly overwritten in Live panels from within a Jupyter cell [BUG] Lines are not properly overwritten in multiline Live panels from within a Jupyter cell Apr 19, 2023
@Yomguithereal
Copy link
Author
Yomguithereal commented Apr 19, 2023

As another reference I am having similar issues with multi line Live displays when the python program is piped into something else that might also print into stderr. I expect to see the error messages from the second command but they are swallowed by the Live render refresh. The related code in the rich source is here and one should notice that complex cursor repositioning only happens when height of the segment's shape is greater than 1 of course, hence the problem happening only in multi line Live displays.

I am of course ready to understand that there is no solution to this problem, in which case I will just add some kind of flag to my CLI forcing complex progress bar on a single line when people need to deal with piping jupyter cells.

@Yomguithereal
Copy link
Author

Here is an example of piped stderr print swallowing:

one.py

import time

from rich.console import Console
from rich.progress import Progress

console = Console(stderr=True)

with Progress(console=console) as progress:

    task1 = progress.add_task("[red]Downloading...", total=1000)
    # task2 = progress.add_task("[green]Processing...", total=1000)

    while not progress.finished:
        progress.update(task1, advance=0.5)
        progress.update(task2, advance=0.3)
        time.sleep(0.02)

two.py

import time
import sys

time.sleep(3)

print('There was an error', file=sys.stderr)
sys.exit(1)

Command to run

python one.py | python two.py

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

No branches or pull requests

2 participants