How to use rich.progress without with
or for
code block?
#1744
-
rich.progress is very great. thanks to developers. I found the offical demo, two ways usage: A. for .. track() from rich.progress import track
for n in track(range(n), description="Processing..."):
do_work(n) B. with Progress() 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)
task3 = progress.add_task("[cyan]Cooking...", total=1000)
while not progress.finished:
progress.update(task1, advance=0.5)
progress.update(task2, advance=0.3)
progress.update(task3, advance=0.9)
time.sleep(0.02) they all need one code indent level for Is that possible? I current use trick with import time
from rich.progress import Progress
# with Progress() as progress:
# 免 with 的调用方法
progress = Progress()
progress.__enter__()
print('hello1')
task1 = progress.add_task("[red]Downloading...1111", total=80)
task2 = progress.add_task("[green]Processing...", total=150)
task3 = progress.add_task("[cyan]Cooking...", total=100)
print('hello2')
i = 0
while not progress.finished:
progress.update(task1, advance=0.5)
progress.update(task2, advance=0.8)
progress.update(task3, advance=0.9)
i+=1
print(f'hello {i}')
time.sleep(0.02)
progress.__exit__(None, None, None) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You can call |
Beta Was this translation helpful? Give feedback.
You can call
progress.start()
andprogress.stop()
. But I wouldn't advise it. If your code throws an exception you must ensure thatstop()
is called.