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

Prevent context manager to overwrite previous one in jupyter #2176

Closed
tzeitim opened this issue Apr 7, 2022 · 3 comments
Closed

Prevent context manager to overwrite previous one in jupyter #2176

tzeitim opened this issue Apr 7, 2022 · 3 comments

Comments

@tzeitim
Copy link

tzeitim commented Apr 7, 2022

When in Jupyter lab, if I call independent instances of a Progress(), for example by mapping a function that invokes a Progress() through an iterable, the context manager overwrites/deletes previous instances along the iteration.

For example (inside a Jupyter Lab notebook) inspired from code in the tutorial:

import time

from rich.progress import Progress

def run_snippet(iteration):
    with Progress() as progress:

        task1 = progress.add_task("[red]Downloading...", total=5)
        while not progress.finished:
            progress.update(task1, advance=0.5)
            #progress.log(f'this iteration {iteration}') <- uncomment for "in-context" effect
            time.sleep(0.2)
            
list(map(run_snippet, range(10)))

The code above will overwrite any print-out (or anything else like plots or rich.renderables) from previous iterations … even within the same context (for example by un-commenting the .log() line ). This behavior doesn't happen when executed from a console. There, anything that is printed or rendered, remains visible and piles up.

My ignorant guess is that when the next Process() context is invoked, the way jupyter handles it by just throwing clearing away what was displayed in the past and moves forward.

Is there a way to prevent his?

And finally, thanks so much for this wonderful tool!

(Edited some words for clarity.)
(Edit2: updated to specify that case has been tested only on Jupyter lab instead of Jupyter notebook)

@willmcgugan
Copy link
Collaborator

Jupyter lab and Jupyter notebook behave differently doing this, and we're not sure what the correct behaviour should be. We suspect a bug in one or the other.

@tzeitim
Copy link
Author

tzeitim commented Apr 13, 2022

Thanks for the clarification - I was unaware of this distinction. I am working solely on Jupyter Lab so this issue has only been tested on Jupyter Lab

Copy link

github-actions bot commented Jul 2, 2024

I hope we solved your problem.

If you like using Rich, you might also enjoy Textual

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

No branches or pull requests

2 participants