Skip to content
Myr

📝 Add docs for using Rich with Typer (#421)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sebastián Ramírez · committed by GitHub · parent 85bc9bc · Browse files · 10 files+195 added−36 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

10 files+195 added−36 removed

  • other:+195 added−36 removed

10 files+195 added−36 removed

modified:docs/tutorial/progressbar.md+99 added-10 removed
modified:docs/tutorial/prompt.md+22 added
74 unmodified lines
75Aborted!76```7778</div>7980## Prompt with Rich8182If you installed Rich as described in [Printing and Colors](printing.md){.internal-link target=_blank}, you can use Rich to prompt the user for input:8384```Python hl_lines="2 6"85{!../docs_src/prompt/tutorial004.py!}86```8788And when you run it, it will look like:8990<div class="termy">9192```console93$ python main.py9495# Enter your name 😎:$ Morty9697Hello Morty98```99100</div>
modified:docs_src/progressbar/tutorial001.py+5 added-5 removed
1import time23import typer4from rich.progress import track567def main():8 total = 08 with typer.progressbar(range(100)) as progress:9 for value in progress:10 # Fake processing time11 time.sleep(0.01)12 total += 19 for value in track(range(100), description="Processing..."):10 # Fake processing time11 time.sleep(0.01)12 total += 113 print(f"Processed {total} things.")141516if __name__ == "__main__":
1 unmodified line
modified:docs_src/progressbar/tutorial002.py+10 added-13 removed
1import time23import typer4from rich.progress import Progress, SpinnerColumn, TextColumn566def iterate_user_ids():7 # Let's imagine this is a web API, not a range()8 for i in range(100):9 yield i10117def main():13 total = 014 with typer.progressbar(iterate_user_ids(), length=100) as progress:15 for value in progress:16 # Fake processing time17 time.sleep(0.01)18 total += 119 print(f"Processed {total} user IDs.")8 with Progress(9 SpinnerColumn(),10 TextColumn("[progress.description]{task.description}"),11 transient=True,12 ) as progress:13 progress.add_task(description="Processing...", total=None)14 progress.add_task(description="Preparing...", total=None)15 time.sleep(5)16 print("Done!")171819if __name__ == "__main__":20 typer.run(main)
modified:docs_src/progressbar/tutorial003.py+1 added-1 removed
3 unmodified lines
456def main():7 total = 08 with typer.progressbar(range(100), label="Processing") as progress:8 with typer.progressbar(range(100)) as progress:9 for value in progress:10 # Fake processing time11 time.sleep(0.01)12 total += 1
5 unmodified lines
modified:docs_src/progressbar/tutorial004.py+12 added-6 removed
1 unmodified line
23import typer456def iterate_user_ids():7 # Let's imagine this is a web API, not a range()8 for i in range(100):9 yield i101112def main():7 total = 10008 with typer.progressbar(length=total) as progress:9 for batch in range(4):13 total = 014 with typer.progressbar(iterate_user_ids(), length=100) as progress:15 for value in progress:16 # Fake processing time11 time.sleep(1)12 progress.update(250)13 print(f"Processed {total} things in batches.")17 time.sleep(0.01)18 total += 119 print(f"Processed {total} user IDs.")202122if __name__ == "__main__":23 typer.run(main)
added:docs_src/progressbar/tutorial005.py+17 added
1import time23import typer456def main():7 total = 08 with typer.progressbar(range(100), label="Processing") as progress:9 for value in progress:10 # Fake processing time11 time.sleep(0.01)12 total += 113 print(f"Processed {total} things.")141516if __name__ == "__main__":17 typer.run(main)
added:docs_src/progressbar/tutorial006.py+17 added
1import time23import typer456def main():7 total = 10008 with typer.progressbar(length=total) as progress:9 for batch in range(4):10 # Fake processing time11 time.sleep(1)12 progress.update(250)13 print(f"Processed {total} things in batches.")141516if __name__ == "__main__":17 typer.run(main)
added:docs_src/prompt/tutorial004.py+11 added
1import typer2from rich.prompt import Prompt345def main():6 name = Prompt.ask("Enter your name :sunglasses:")7 print(f"Hey there {name}!")8910if __name__ == "__main__":11 typer.run(main)
modified:.pre-commit-config.yaml+1 added-1 removed
17 unmodified lines
18 args:19 - --py3-plus20 - --keep-runtime-typing21 # This file is more readable without yield from22 exclude: ^docs_src/progressbar/tutorial002\.py22 exclude: ^docs_src/progressbar/tutorial004\.py23- repo: https://github.com/myint/autoflake24 rev: v1.425 hooks:26 - id: autoflake
27 unmodified lines