diff --git a/docs/src/progressbar/tutorial001.py b/docs/src/progressbar/tutorial001.py new file mode 100644 index 0000000..b609a6e --- /dev/null +++ b/docs/src/progressbar/tutorial001.py @@ -0,0 +1,17 @@ +import time + +import typer + + +def main(): + total = 0 + with typer.progressbar(range(100)) as progress: + for value in progress: + # Fake processing time + time.sleep(0.01) + total += 1 + typer.echo(f"Processed {total} things.") + + +if __name__ == "__main__": + typer.run(main) diff --git a/docs/src/progressbar/tutorial002.py b/docs/src/progressbar/tutorial002.py new file mode 100644 index 0000000..45f40d9 --- /dev/null +++ b/docs/src/progressbar/tutorial002.py @@ -0,0 +1,23 @@ +import time + +import typer + + +def iterate_user_ids(): + # Let's imagine this is a web API, not a range() + for i in range(100): + yield i + + +def main(): + total = 0 + with typer.progressbar(iterate_user_ids(), length=100) as progress: + for value in progress: + # Fake processing time + time.sleep(0.01) + total += 1 + typer.echo(f"Processed {total} user IDs.") + + +if __name__ == "__main__": + typer.run(main) diff --git a/docs/src/progressbar/tutorial003.py b/docs/src/progressbar/tutorial003.py new file mode 100644 index 0000000..579a28d --- /dev/null +++ b/docs/src/progressbar/tutorial003.py @@ -0,0 +1,17 @@ +import time + +import typer + + +def main(): + total = 0 + with typer.progressbar(range(100), label="Processing") as progress: + for value in progress: + # Fake processing time + time.sleep(0.01) + total += 1 + typer.echo(f"Processed {total} things.") + + +if __name__ == "__main__": + typer.run(main) diff --git a/docs/src/progressbar/tutorial004.py b/docs/src/progressbar/tutorial004.py new file mode 100644 index 0000000..957630b --- /dev/null +++ b/docs/src/progressbar/tutorial004.py @@ -0,0 +1,17 @@ +import time + +import typer + + +def main(): + total = 1000 + with typer.progressbar(length=total) as progress: + for batch in range(4): + # Fake processing time + time.sleep(1) + progress.update(2500) + typer.echo(f"Processed {total} things in batches.") + + +if __name__ == "__main__": + typer.run(main) diff --git a/docs/tutorial/progressbar.md b/docs/tutorial/progressbar.md new file mode 100644 index 0000000..af72989 --- /dev/null +++ b/docs/tutorial/progressbar.md @@ -0,0 +1,139 @@ +If you are executing an operation that can take some time, you can inform it to the user with a progress bar. + +For this, you can use `typer.progressbar()`: + +```Python hl_lines="8" +{!./src/progressbar/tutorial001.py!} +``` + +You use `typer.progressbar()` with a `with` statement, as in: + +```Python +with typer.progressbar(something) as progress: + pass +``` + +And you pass as function argument to `typer.progressbar()` the thing that you would normally iterate over. + +So, if you have a list of users, this could be: + +```Python +users = ["Camila", "Rick", "Morty"] + +with typer.progressbar(users) as progress: + pass +``` + +And the `with` statement using `typer.progressbar()` gives you an object that you can iterate over, just like if it was the same thing that you would iterate over normally. + +But by iterating over this object **Typer** (actually Click) will know to update the progress bar: + +```Python +users = ["Camila", "Rick", "Morty"] + +with typer.progressbar(users) as progress: + for user in progress: + typer.echo(user) +``` + +!!! tip + Notice that there are 2 levels of code blocks. One for the `with` statement and one for the `for` statement. + +!!! info + This is mostly useful for operations that take some time. + + In the example above we are faking it with `time.sleep()`. + +Check it: + +