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
Provenance
- Commit
8d859dd- Tree
d124ac0- Author
Sebastián Ramírez <tiangolo@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
d056aac- Parent 2
e9afd24
7 files changed+215 added−1 removed
added:docs/src/progressbar/tutorial001.py+17 added
1+import time2+3+import typer4+5+6+def 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.")14+15+16+if __name__ == "__main__":17+ typer.run(main)added:docs/src/progressbar/tutorial002.py+23 added
1+import time2+3+import typer4+5+6+def iterate_user_ids():7+ # Let's imagine this is a web API, not a range()8+ for i in range(100):9+ yield i10+11+12+def 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.")20+21+22+if __name__ == "__main__":23+ typer.run(main)added:docs/src/progressbar/tutorial003.py+17 added
1+import time2+3+import typer4+5+6+def 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.")14+15+16+if __name__ == "__main__":17+ typer.run(main)added:docs/src/progressbar/tutorial004.py+17 added
1+import time2+3+import typer4+5+6+def 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.")14+15+16+if __name__ == "__main__":17+ typer.run(main)added:docs/tutorial/progressbar.md+139 added
1+If you are executing an operation that can take some time, you can inform it to the user with a progress bar.2+3+For this, you can use `typer.progressbar()`:4+5+```Python hl_lines="8"6+{!./src/progressbar/tutorial001.py!}7+```8+9+You use `typer.progressbar()` with a `with` statement, as in:10+11+```Python12+with typer.progressbar(something) as progress:13+ pass14+```15+16+And you pass as function argument to `typer.progressbar()` the thing that you would normally iterate over.17+18+So, if you have a list of users, this could be:19+20+```Python21+users = ["Camila", "Rick", "Morty"]22+23+with typer.progressbar(users) as progress:24+ pass25+```26+27+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.28+29+But by iterating over this object **Typer** (actually Click) will know to update the progress bar:30+31+```Python32+users = ["Camila", "Rick", "Morty"]33+34+with typer.progressbar(users) as progress:35+ for user in progress:36+ typer.echo(user)37+```38+39+!!! tip40+ Notice that there are 2 levels of code blocks. One for the `with` statement and one for the `for` statement.41+42+!!! info43+ This is mostly useful for operations that take some time.44+45+ In the example above we are faking it with `time.sleep()`.46+47+Check it:48+49+<div class="termy">50+51+```console52+$ python main.py53+54+---> 100%55+56+Processed 100 things.57+```58+59+</div>60+61+## Setting a Progress Bar `length`62+63+The progress bar is generated from the length of the iterable (e.g. the list of users).64+65+But 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()`.66+67+```Python hl_lines="14"68+{!./src/progressbar/tutorial002.py!}69+```70+71+Check it:72+73+<div class="termy">74+75+```console76+$ python main.py77+78+---> 100%79+80+Processed 100 user IDs.81+```82+83+</div>84+85+### About the function with `yield`86+87+If 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>".88+89+You can iterate over that function with a `for` and at each iteration it will give you the value at `yield`.90+91+`yield` is like a `return` that gives values multiple times and let's you use the function in a `for` loop.92+93+For example:94+95+```Python96+def iterate_user_ids():97+ # Let's imagine this is a web API, not a range()98+ for i in range(100):99+ yield i100+101+for i in iterate_user_ids():102+ print(i)103+```104+105+would print each of the "user IDs" (here it's just the numbers from `0` to `99`).106+107+## Add a `label`108+109+You can also set a `label`:110+111+```Python hl_lines="8"112+{!./src/progressbar/tutorial003.py!}113+```114+115+Check it:116+117+<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>122+123+## Iterate manually124+125+If 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()`.126+127+And then calling the `.update()` method in the object from the `with` statement:128+129+```Python hl_lines="8 12"130+{!./src/progressbar/tutorial004.py!}131+```132+133+Check it:134+135+<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 lines25 assert "Create a new user with USERNAME." in result.output26 assert "Some internal utility function to create." not in result.output27 28 29-def test_help_create():29+def 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.output22 unmodified linesmodified:mkdocs.yml+1 added
46 unmodified lines47484950515253545534 unmodified lines46 unmodified lines - Enum - Choices: 'tutorial/parameter-types/enum.md' - Path: 'tutorial/parameter-types/path.md' - File: 'tutorial/parameter-types/file.md' - Ask with Prompt: 'tutorial/prompt.md' - Progress Bar: 'tutorial/progressbar.md' - Alternatives, Inspiration and Comparisons: 'alternatives.md' - Help Typer - Get Help: 'help-typer.md' - Development - Contributing: 'contributing.md' - Release Notes: release-notes.md34 unmodified lines