rich icon indicating copy to clipboard operation
rich copied to clipboard

Prevent context manager to overwrite previous one in jupyter

Open tzeitim opened this issue 3 years ago • 2 comments

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)

tzeitim avatar Apr 07 '22 13:04 tzeitim

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.

willmcgugan avatar Apr 13 '22 14:04 willmcgugan

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

tzeitim avatar Apr 13 '22 14:04 tzeitim