Skip to content
Myr

Merge pull request #23 from tiangolo/progressbar

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

:memo: Add docs for progress bars.

authored by Sebastián Ramírez · committed by GitHub · parents d056aac, e9afd24 · Browse files · 7 files+215 added−1 removed
This commit

State

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

Diff stats

7 files+215 added−1 removed

  • tests:+1 added−1 removed
  • other:+214 added−0 removed

7 files+215 added−1 removed

added:docs/src/progressbar/tutorial001.py+17 added
1import time23import typer456def main():7 total = 08 with typer.progressbar(range(100)) as progress:9 for value in progress:10 # Fake processing time11 time.sleep(0.01)12 total += 113 typer.echo(f"Processed {total} things.")141516if __name__ == "__main__":17 typer.run(main)
added:docs/src/progressbar/tutorial002.py+23 added
1import time23import 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():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 typer.echo(f"Processed {total} user IDs.")202122if __name__ == "__main__":23 typer.run(main)
added:docs/src/progressbar/tutorial003.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 typer.echo(f"Processed {total} things.")141516if __name__ == "__main__":17 typer.run(main)
added:docs/src/progressbar/tutorial004.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(2500)13 typer.echo(f"Processed {total} things in batches.")141516if __name__ == "__main__":17 typer.run(main)
added:docs/tutorial/progressbar.md+139 added
1If you are executing an operation that can take some time, you can inform it to the user with a progress bar.23For this, you can use `typer.progressbar()`:45```Python hl_lines="8"6{!./src/progressbar/tutorial001.py!}7```89You use `typer.progressbar()` with a `with` statement, as in:1011```Python12with typer.progressbar(something) as progress:13 pass14```1516And you pass as function argument to `typer.progressbar()` the thing that you would normally iterate over.1718So, if you have a list of users, this could be:1920```Python21users = ["Camila", "Rick", "Morty"]2223with typer.progressbar(users) as progress:24 pass25```2627And 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.2829But by iterating over this object **Typer** (actually Click) will know to update the progress bar:3031```Python32users = ["Camila", "Rick", "Morty"]3334with typer.progressbar(users) as progress:35 for user in progress:36 typer.echo(user)37```3839!!! tip40 Notice that there are 2 levels of code blocks. One for the `with` statement and one for the `for` statement.4142!!! info43 This is mostly useful for operations that take some time.4445 In the example above we are faking it with `time.sleep()`.4647Check it:4849<div class="termy">5051```console52$ python main.py5354---> 100%5556Processed 100 things.57```5859</div>6061## Setting a Progress Bar `length`6263The progress bar is generated from the length of the iterable (e.g. the list of users).6465But if the length is not available (for example, with something that fetches a new user from a web API each time) you can pass an explicit `length` to `typer.progressbar()`.6667```Python hl_lines="14"68{!./src/progressbar/tutorial002.py!}69```7071Check it:7273<div class="termy">7475```console76$ python main.py7778---> 100%7980Processed 100 user IDs.81```8283</div>8485### About the function with `yield`8687If you hadn't seen something like that `yield` above, that's a "<a href="https://docs.python.org/3/glossary.html#term-generator" target="_blank">generator</a>".8889You can iterate over that function with a `for` and at each iteration it will give you the value at `yield`.9091`yield` is like a `return` that gives values multiple times and let's you use the function in a `for` loop.9293For example:9495```Python96def iterate_user_ids():97 # Let's imagine this is a web API, not a range()98 for i in range(100):99 yield i100101for i in iterate_user_ids():102 print(i)103```104105would print each of the "user IDs" (here it's just the numbers from `0` to `99`).106107## Add a `label`108109You can also set a `label`:110111```Python hl_lines="8"112{!./src/progressbar/tutorial003.py!}113```114115Check it:116117<div class="use-termynal">118<span data-ty="input">python main.py</span>119<span data-ty="progress" data-ty-prompt="Processing"></span>120<span data-ty>Processed 100 things.</span>121</div>122123## Iterate manually124125If you need to manually iterate over something and update the progress bar irregularly, you can do it by not passing an iterable but just a `length` to `typer.progressbar()`.126127And then calling the `.update()` method in the object from the `with` statement:128129```Python hl_lines="8 12"130{!./src/progressbar/tutorial004.py!}131```132133Check it:134135<div class="use-termynal">136<span data-ty="input">python main.py</span>137<span data-ty="progress" data-ty-prompt="Batches"></span>138<span data-ty>Processed 100 things in batches.</span>139</div>
modified:tests/test_tutorial/test_commands/test_help/test_tutorial002.py+1 added-1 removed
24 unmodified lines
25 assert "Create a new user with USERNAME." in result.output26 assert "Some internal utility function to create." not in result.output272829def test_help_create():29def test_help_delete():30 result = runner.invoke(app, ["delete", "--help"])31 assert result.exit_code == 032 assert "Delete a user with USERNAME." in result.output33 assert "Some internal utility function to delete." not in result.output
22 unmodified lines
modified:mkdocs.yml+1 added