utils.md
This file
Path
docs/utils.md
390 lines · 12.2 KiB · Markdown
Last changed by
convert from reST to MySTdenuowebbefore adoption29eb946
History of this fileTake it
convert from reST to MySTdenuowebbefore adoption29eb946
line 1why, one step back1# Utilitieschange extension to .mddenuowebbefore adoptionc002e08
line 2why, one step back2convert from reST to MySTdenuowebbefore adoption29eb946
lines 3–4why, one step back3Besides the functionality that Click provides to interface with argument parsing and handling, it also provides a bunch4of addon functionality that is useful for writing command line utilities.change extension to .mddenuowebbefore adoptionc002e08
line 5why, one step back5convert from reST to MySTdenuowebbefore adoption29eb946
line 6why, one step back6## Printing to Stdoutchange extension to .mddenuowebbefore adoptionc002e08
line 7why, one step back7convert from reST to MySTdenuowebbefore adoption29eb946
lines 8–9why, one step back8The most obvious helper is the {func}`click.echo` function, which in many ways works like the Python `print` statement or9function. The main difference is that it works the same in many different terminal environments.change extension to .mddenuowebbefore adoptionc002e08
line 10why, one step back10convert from reST to MySTdenuowebbefore adoption29eb946
line 11why, one step back11Example:change extension to .mddenuowebbefore adoptionc002e08
line 12why, one step back12convert from reST to MySTdenuowebbefore adoption29eb946
lines 13–14why, one step back13```python14import clickchange extension to .mddenuowebbefore adoptionc002e08
line 15why, one step back15convert from reST to MySTdenuowebbefore adoption29eb946
lines 16–17why, one step back16click.echo('Hello World!')17```change extension to .mddenuowebbefore adoptionc002e08
line 18why, one step back18convert from reST to MySTdenuowebbefore adoption29eb946
lines 19–20why, one step back19It can output both text and binary data. It will emit a trailing newline by default, which needs to be suppressed by20passing `nl=False`:change extension to .mddenuowebbefore adoptionc002e08
line 21why, one step back21convert from reST to MySTdenuowebbefore adoption29eb946
lines 22–24why, one step back22```python23click.echo(b'\xe2\x98\x83', nl=False)24```change extension to .mddenuowebbefore adoptionc002e08
line 25why, one step back25convert from reST to MySTdenuowebbefore adoption29eb946
lines 26–28why, one step back26Last but not least {func}`click.echo` uses click's intelligent internal output streams to stdout and stderr which support27unicode output on the Windows console. This means for as long as you are using `click.echo` you can output unicode28characters (there are some limitations on the default font with regards to which characters can be displayed).change extension to .mddenuowebbefore adoptionc002e08
line 29why, one step back29convert from reST to MySTdenuowebbefore adoption29eb946
lines 30–31why, one step back30```{versionadded} 6.031```change extension to .mddenuowebbefore adoptionc002e08
line 32why, one step back32convert from reST to MySTdenuowebbefore adoption29eb946
lines 33–34why, one step back33Click emulates output streams on Windows to support unicode to the Windows console through separate APIs. For more34information see {doc}`wincmd`.change extension to .mddenuowebbefore adoptionc002e08
line 35why, one step back35convert from reST to MySTdenuowebbefore adoption29eb946
lines 36–37why, one step back36```{versionadded} 3.037```change extension to .mddenuowebbefore adoptionc002e08
line 38why, one step back38convert from reST to MySTdenuowebbefore adoption29eb946
line 39why, one step back39You can also easily print to standard error by passing `err=True`:change extension to .mddenuowebbefore adoptionc002e08
line 40why, one step back40convert from reST to MySTdenuowebbefore adoption29eb946
lines 41–43why, one step back41```python42click.echo('Hello World!', err=True)43```change extension to .mddenuowebbefore adoptionc002e08
line 44why, one step back44convert from reST to MySTdenuowebbefore adoption29eb946
line 45why, one step back45(ansi-colors)=change extension to .mddenuowebbefore adoptionc002e08
line 46why, one step back46convert from reST to MySTdenuowebbefore adoption29eb946
line 47why, one step back47## ANSI Colorschange extension to .mddenuowebbefore adoptionc002e08
line 48why, one step back48convert from reST to MySTdenuowebbefore adoption29eb946
lines 49–50why, one step back49```{versionadded} 2.050```change extension to .mddenuowebbefore adoptionc002e08
line 51why, one step back51convert from reST to MySTdenuowebbefore adoption29eb946
line 52why, one step back52The {func}`click.echo` function supports ANSI colors and styles. On Windows this uses [colorama].change extension to .mddenuowebbefore adoptionc002e08
lines 53–55why, one step back5354Primarily this means that:55convert from reST to MySTdenuowebbefore adoption29eb946
lines 56–58why, one step back56- Click's {func}`click.echo` function will automatically strip ANSI color codes if the stream is not connected to a terminal.57- the {func}`click.echo` function will transparently connect to the terminal on Windows and translate ANSI codes to terminal58 API calls. This means that colors will work on Windows the same way they do on other operating systems.change extension to .mddenuowebbefore adoptionc002e08
line 59why, one step back59convert from reST to MySTdenuowebbefore adoption29eb946
lines 60–61why, one step back60On Windows, Click uses colorama without calling `colorama.init()`. You can still call that in your code, but it's not61required for Click.change extension to .mddenuowebbefore adoptionc002e08
line 62why, one step back62convert from reST to MySTdenuowebbefore adoption29eb946
line 63why, one step back63For styling a string, the {func}`click.style` function can be used:change extension to .mddenuowebbefore adoptionc002e08
line 64why, one step back64convert from reST to MySTdenuowebbefore adoption29eb946
lines 65–66why, one step back65```python66import clickchange extension to .mddenuowebbefore adoptionc002e08
line 67why, one step back67convert from reST to MySTdenuowebbefore adoption29eb946
lines 68–71why, one step back68click.echo(click.style('Hello World!', fg='green'))69click.echo(click.style('Some more text', bg='blue', fg='white'))70click.echo(click.style('ATTENTION', blink=True, bold=True))71```change extension to .mddenuowebbefore adoptionc002e08
line 72why, one step back72convert from reST to MySTdenuowebbefore adoption29eb946
line 73why, one step back73The combination of {func}`click.echo` and {func}`click.style` is also available in a single function called {func}`click.secho`:change extension to .mddenuowebbefore adoptionc002e08
line 74why, one step back74convert from reST to MySTdenuowebbefore adoption29eb946
lines 75–79why, one step back75```python76click.secho('Hello World!', fg='green')77click.secho('Some more text', bg='blue', fg='white')78click.secho('ATTENTION', blink=True, bold=True)79```change extension to .mddenuowebbefore adoptionc002e08
line 80why, one step back80convert from reST to MySTdenuowebbefore adoption29eb946
line 81why, one step back81## Pager Supportchange extension to .mddenuowebbefore adoptionc002e08
line 82why, one step back82convert from reST to MySTdenuowebbefore adoption29eb946
lines 83–85why, one step back83In some situations, you might want to show long texts on the terminal and let a user scroll through it. This can be84achieved by using the {func}`click.echo_via_pager` function which works similarly to the {func}`click.echo` function, but always85writes to stdout and, if possible, through a pager.change extension to .mddenuowebbefore adoptionc002e08
lines 86–88why, one step back8687Example:88convert from reST to MySTdenuowebbefore adoption29eb946
lines 89–93why, one step back89```python90@click.command()91def less():92 click.echo_via_pager("\n".join(f"Line {idx}" for idx in range(200)))93```change extension to .mddenuowebbefore adoptionc002e08
line 94why, one step back94convert from reST to MySTdenuowebbefore adoption29eb946
lines 95–96why, one step back95If you want to use the pager for a lot of text, especially if generating everything in advance would take a lot of time,96you can pass a generator (or generator function) instead of a string:change extension to .mddenuowebbefore adoptionc002e08
line 97why, one step back97convert from reST to MySTdenuowebbefore adoption29eb946
lines 98–101why, one step back98```python99def _generate_output():100 for idx in range(50000):101 yield f"Line {idx}\n"change extension to .mddenuowebbefore adoptionc002e08
line 102why, one step back102convert from reST to MySTdenuowebbefore adoption29eb946
lines 103–106why, one step back103@click.command()104def less():105 click.echo_via_pager(_generate_output())106```change extension to .mddenuowebbefore adoptionc002e08
line 107why, one step back107convert from reST to MySTdenuowebbefore adoption29eb946
line 108why, one step back108## Screen Clearingchange extension to .mddenuowebbefore adoptionc002e08
line 109why, one step back109convert from reST to MySTdenuowebbefore adoption29eb946
lines 110–111why, one step back110```{versionadded} 2.0111```change extension to .mddenuowebbefore adoptionc002e08
line 112why, one step back112convert from reST to MySTdenuowebbefore adoption29eb946
lines 113–114why, one step back113To clear the terminal screen, you can use the {func}`click.clear` function that is provided starting with Click 2.0. It does114what the name suggests: it clears the entire visible screen in a platform-agnostic way:change extension to .mddenuowebbefore adoptionc002e08
line 115why, one step back115convert from reST to MySTdenuowebbefore adoption29eb946
lines 116–119why, one step back116```117import click118click.clear()119```change extension to .mddenuowebbefore adoptionc002e08
line 120why, one step back120convert from reST to MySTdenuowebbefore adoption29eb946
line 121why, one step back121## Getting Characters from Terminalchange extension to .mddenuowebbefore adoptionc002e08
line 122why, one step back122convert from reST to MySTdenuowebbefore adoption29eb946
lines 123–124why, one step back123```{versionadded} 2.0124```change extension to .mddenuowebbefore adoptionc002e08
line 125why, one step back125convert from reST to MySTdenuowebbefore adoption29eb946
lines 126–128why, one step back126Normally, when reading input from the terminal, you would read from standard input. However, this is buffered input and127will not show up until the line has been terminated. In certain circumstances, you might not want to do that and instead128read individual characters as they are being written.change extension to .mddenuowebbefore adoptionc002e08
line 129why, one step back129convert from reST to MySTdenuowebbefore adoption29eb946
lines 130–131why, one step back130For this, Click provides the {func}`click.getchar` function which reads a single character from the terminal buffer and131returns it as a Unicode character.change extension to .mddenuowebbefore adoptionc002e08
line 132why, one step back132convert from reST to MySTdenuowebbefore adoption29eb946
line 133why, one step back133Note that this function will always read from the terminal, even if stdin is instead a pipe.change extension to .mddenuowebbefore adoptionc002e08
line 134why, one step back134convert from reST to MySTdenuowebbefore adoption29eb946
line 135why, one step back135Example:change extension to .mddenuowebbefore adoptionc002e08
line 136why, one step back136convert from reST to MySTdenuowebbefore adoption29eb946
lines 137–138why, one step back137```python138import clickchange extension to .mddenuowebbefore adoptionc002e08
line 139why, one step back139convert from reST to MySTdenuowebbefore adoption29eb946
lines 140–149why, one step back140click.echo('Continue? [yn] ', nl=False)141c = click.getchar()142click.echo()143if c == 'y':144 click.echo('We will go on')145elif c == 'n':146 click.echo('Abort!')147else:148 click.echo('Invalid input :(')149```change extension to .mddenuowebbefore adoptionc002e08
line 150why, one step back150convert from reST to MySTdenuowebbefore adoption29eb946
lines 151–153why, one step back151Note that this reads raw input, which means that things like arrow keys will show up in the platform's native escape152format. The only characters translated are `^C` and `^D` which are converted into keyboard interrupts and end of file153exceptions respectively. This is done because otherwise, it's too easy to forget about that and to create scripts thatchange extension to .mddenuowebbefore adoptionc002e08
lines 154–155why, one step back154cannot be properly exited.155convert from reST to MySTdenuowebbefore adoption29eb946
line 156why, one step back156## Waiting for Key Presschange extension to .mddenuowebbefore adoptionc002e08
line 157why, one step back157convert from reST to MySTdenuowebbefore adoption29eb946
lines 158–159why, one step back158```{versionadded} 2.0159```change extension to .mddenuowebbefore adoptionc002e08
line 160why, one step back160convert from reST to MySTdenuowebbefore adoption29eb946
lines 161–162why, one step back161Sometimes, it's useful to pause until the user presses any key on the keyboard. This is especially useful on Windows162where `cmd.exe` will close the window at the end of the command execution by default, instead of waiting.change extension to .mddenuowebbefore adoptionc002e08
line 163why, one step back163convert from reST to MySTdenuowebbefore adoption29eb946
lines 164–166why, one step back164In click, this can be accomplished with the {func}`click.pause` function. This function will print a quick message to the165terminal (which can be customized) and wait for the user to press a key. In addition to that, it will also become a NOP166(no operation instruction) if the script is not run interactively.change extension to .mddenuowebbefore adoptionc002e08
line 167why, one step back167convert from reST to MySTdenuowebbefore adoption29eb946
line 168why, one step back168Example:change extension to .mddenuowebbefore adoptionc002e08
line 169why, one step back169convert from reST to MySTdenuowebbefore adoption29eb946
lines 170–173why, one step back170```python171import click172click.pause()173```change extension to .mddenuowebbefore adoptionc002e08
line 174why, one step back174convert from reST to MySTdenuowebbefore adoption29eb946
line 175why, one step back175## Launching Editorschange extension to .mddenuowebbefore adoptionc002e08
line 176why, one step back176convert from reST to MySTdenuowebbefore adoption29eb946
lines 177–178why, one step back177```{versionadded} 2.0178```change extension to .mddenuowebbefore adoptionc002e08
line 179why, one step back179convert from reST to MySTdenuowebbefore adoption29eb946
lines 180–182why, one step back180Click supports launching editors automatically through {func}`click.edit`. This is very useful for asking users for multi-line181input. It will automatically open the user's defined editor or fall back to a sensible default. If the user closes the182editor without saving, the return value will be `None`, otherwise the entered text.change extension to .mddenuowebbefore adoptionc002e08
line 183why, one step back183convert from reST to MySTdenuowebbefore adoption29eb946
line 184why, one step back184Example usage:change extension to .mddenuowebbefore adoptionc002e08
line 185why, one step back185convert from reST to MySTdenuowebbefore adoption29eb946
lines 186–187why, one step back186```python187import clickchange extension to .mddenuowebbefore adoptionc002e08
line 188why, one step back188convert from reST to MySTdenuowebbefore adoption29eb946
lines 189–194why, one step back189def get_commit_message():190 MARKER = '# Everything below is ignored\n'191 message = click.edit('\n\n' + MARKER)192 if message is not None:193 return message.split(MARKER, 1)[0].rstrip('\n')194```change extension to .mddenuowebbefore adoptionc002e08
line 195why, one step back195convert from reST to MySTdenuowebbefore adoption29eb946
lines 196–197why, one step back196Alternatively, the function can also be used to launch editors for files by a specific filename. In this case, the197return value is always `None`.change extension to .mddenuowebbefore adoptionc002e08
line 198why, one step back198convert from reST to MySTdenuowebbefore adoption29eb946
line 199why, one step back199Example usage:change extension to .mddenuowebbefore adoptionc002e08
line 200why, one step back200convert from reST to MySTdenuowebbefore adoption29eb946
lines 201–204why, one step back201```python202import click203click.edit(filename='/etc/passwd')204```change extension to .mddenuowebbefore adoptionc002e08
line 205why, one step back205convert from reST to MySTdenuowebbefore adoption29eb946
line 206why, one step back206## Launching Applicationschange extension to .mddenuowebbefore adoptionc002e08
line 207why, one step back207convert from reST to MySTdenuowebbefore adoption29eb946
lines 208–209why, one step back208```{versionadded} 2.0209```change extension to .mddenuowebbefore adoptionc002e08
line 210why, one step back210convert from reST to MySTdenuowebbefore adoption29eb946
lines 211–213why, one step back211Click supports launching applications through {func}`click.launch`. This can be used to open the default application212associated with a URL or filetype. This can be used to launch web browsers or picture viewers, for instance. In addition213to this, it can also launch the file manager and automatically select the provided file.change extension to .mddenuowebbefore adoptionc002e08
line 214why, one step back214convert from reST to MySTdenuowebbefore adoption29eb946
line 215why, one step back215Example usage:change extension to .mddenuowebbefore adoptionc002e08
line 216why, one step back216convert from reST to MySTdenuowebbefore adoption29eb946
lines 217–220why, one step back217```python218click.launch("https://click.palletsprojects.com/")219click.launch("/my/downloaded/file.txt", locate=True)220```change extension to .mddenuowebbefore adoptionc002e08
line 221why, one step back221convert from reST to MySTdenuowebbefore adoption29eb946
line 222why, one step back222## Printing Filenameschange extension to .mddenuowebbefore adoptionc002e08
line 223why, one step back223convert from reST to MySTdenuowebbefore adoption29eb946
line 224why, one step back224Because filenames might not be Unicode, formatting them can be a bit tricky.change extension to .mddenuowebbefore adoptionc002e08
line 225why, one step back225convert from reST to MySTdenuowebbefore adoption29eb946
lines 226–228why, one step back226The way this works with click is through the {func}`click.format_filename` function. It does a best-effort conversion of the227filename to Unicode and will never fail. This makes it possible to use these filenames in the context of a full Unicode228string.change extension to .mddenuowebbefore adoptionc002e08
line 229why, one step back229convert from reST to MySTdenuowebbefore adoption29eb946
line 230why, one step back230Example:change extension to .mddenuowebbefore adoptionc002e08
line 231why, one step back231convert from reST to MySTdenuowebbefore adoption29eb946
lines 232–234why, one step back232```python233click.echo(f"Path: {click.format_filename(b'foo.txt')}")234```change extension to .mddenuowebbefore adoptionc002e08
line 235why, one step back235convert from reST to MySTdenuowebbefore adoption29eb946
line 236why, one step back236## Standard Streamschange extension to .mddenuowebbefore adoptionc002e08
line 237why, one step back237convert from reST to MySTdenuowebbefore adoption29eb946
lines 238–240why, one step back238For command line utilities, it's very important to get access to input and output streams reliably. Python generally239provides access to these streams through `sys.stdout` and friends, but unfortunately, there are API differences between2402.x and 3.x, especially with regards to how these streams respond to Unicode and binary data.change extension to .mddenuowebbefore adoptionc002e08
line 241why, one step back241convert from reST to MySTdenuowebbefore adoption29eb946
lines 242–243why, one step back242Because of this, click provides the {func}`click.get_binary_stream` and {func}`click.get_text_stream` functions, which produce243consistent results with different Python versions and for a wide variety of terminal configurations.change extension to .mddenuowebbefore adoptionc002e08
line 244why, one step back244convert from reST to MySTdenuowebbefore adoption29eb946
lines 245–246why, one step back245The end result is that these functions will always return a functional stream object (except in very odd cases; see246{doc}`/unicode-support`).change extension to .mddenuowebbefore adoptionc002e08
line 247why, one step back247convert from reST to MySTdenuowebbefore adoption29eb946
line 248why, one step back248Example:change extension to .mddenuowebbefore adoptionc002e08
line 249why, one step back249convert from reST to MySTdenuowebbefore adoption29eb946
lines 250–251why, one step back250```python251import clickchange extension to .mddenuowebbefore adoptionc002e08
line 252why, one step back252convert from reST to MySTdenuowebbefore adoption29eb946
lines 253–255why, one step back253stdin_text = click.get_text_stream('stdin')254stdout_binary = click.get_binary_stream('stdout')255```change extension to .mddenuowebbefore adoptionc002e08
line 256why, one step back256convert from reST to MySTdenuowebbefore adoption29eb946
lines 257–258why, one step back257```{versionadded} 6.0258```change extension to .mddenuowebbefore adoptionc002e08
line 259why, one step back259convert from reST to MySTdenuowebbefore adoption29eb946
lines 260–261why, one step back260Click now emulates output streams on Windows to support unicode to the Windows console through separate APIs. For more261information see {doc}`wincmd`.change extension to .mddenuowebbefore adoptionc002e08
line 262why, one step back262convert from reST to MySTdenuowebbefore adoption29eb946
line 263why, one step back263## Intelligent File Openingchange extension to .mddenuowebbefore adoptionc002e08
line 264why, one step back264convert from reST to MySTdenuowebbefore adoption29eb946
lines 265–266why, one step back265```{versionadded} 3.0266```change extension to .mddenuowebbefore adoptionc002e08
line 267why, one step back267convert from reST to MySTdenuowebbefore adoption29eb946
lines 268–269why, one step back268Starting with Click 3.0 the logic for opening files from the {func}`click.File` type is exposed through the {func}`click.open_file`269function. It can intelligently open stdin/stdout as well as any other file.change extension to .mddenuowebbefore adoptionc002e08
line 270why, one step back270convert from reST to MySTdenuowebbefore adoption29eb946
line 271why, one step back271Example:change extension to .mddenuowebbefore adoptionc002e08
line 272why, one step back272convert from reST to MySTdenuowebbefore adoption29eb946
lines 273–274why, one step back273```python274import clickchange extension to .mddenuowebbefore adoptionc002e08
line 275why, one step back275convert from reST to MySTdenuowebbefore adoption29eb946
lines 276–278why, one step back276stdout = click.open_file('-', 'w')277test_file = click.open_file('test.txt', 'w')278```change extension to .mddenuowebbefore adoptionc002e08
line 279why, one step back279convert from reST to MySTdenuowebbefore adoption29eb946
lines 280–281why, one step back280If stdin or stdout are returned, the return value is wrapped in a special file where the context manager will prevent281the closing of the file. This makes the handling of standard streams transparent and you can always use it like this:change extension to .mddenuowebbefore adoptionc002e08
line 282why, one step back282convert from reST to MySTdenuowebbefore adoption29eb946
lines 283–286why, one step back283```python284with click.open_file(filename, 'w') as f:285 f.write('Hello World!\n')286```change extension to .mddenuowebbefore adoptionc002e08
line 287why, one step back287convert from reST to MySTdenuowebbefore adoption29eb946
line 288why, one step back288## Finding Application Folderschange extension to .mddenuowebbefore adoptionc002e08
line 289why, one step back289convert from reST to MySTdenuowebbefore adoption29eb946
lines 290–291why, one step back290```{versionadded} 2.0291```change extension to .mddenuowebbefore adoptionc002e08
line 292why, one step back292convert from reST to MySTdenuowebbefore adoption29eb946
lines 293–296why, one step back293Very often, you want to open a configuration file that belongs to your application. However, different operating systems294store these configuration files in different locations depending on their standards. Click provides a295{func}`click.get_app_dir` function which returns the most appropriate location for per-user config files for your application296depending on the OS.change extension to .mddenuowebbefore adoptionc002e08
line 297why, one step back297convert from reST to MySTdenuowebbefore adoption29eb946
line 298why, one step back298Example usage:change extension to .mddenuowebbefore adoptionc002e08
line 299why, one step back299convert from reST to MySTdenuowebbefore adoption29eb946
lines 300–303why, one step back300```python301import os302import click303import ConfigParserchange extension to .mddenuowebbefore adoptionc002e08
line 304why, one step back304convert from reST to MySTdenuowebbefore adoption29eb946
lines 305–315why, one step back305APP_NAME = 'My Application'306def read_config():307cfg = os.path.join(click.get_app_dir(APP_NAME), 'config.ini')308parser = ConfigParser.RawConfigParser()309parser.read([cfg])310rv = {}311for section in parser.sections():312 for key, value in parser.items(section):313 rv[f"{section}.{key}"] = value314return rv315```change extension to .mddenuowebbefore adoptionc002e08
line 316why, one step back316convert from reST to MySTdenuowebbefore adoption29eb946
line 317why, one step back317## Showing Progress Barschange extension to .mddenuowebbefore adoptionc002e08
line 318why, one step back318convert from reST to MySTdenuowebbefore adoption29eb946
lines 319–321why, one step back319Sometimes, you have command line scripts that need to process a lot of data, but you want to quickly show the user some320progress about how long that will take. Click supports simple progress bar rendering for that through the321{func}`click.progressbar` function.change extension to .mddenuowebbefore adoptionc002e08
line 322why, one step back322convert from reST to MySTdenuowebbefore adoption29eb946
lines 323–324why, one step back323```{note} If you find that you have requirements beyond what Click's progress bar supports, try using [tqdm].324```change extension to .mddenuowebbefore adoptionc002e08
line 325why, one step back325convert from reST to MySTdenuowebbefore adoption29eb946
lines 326–327why, one step back326The basic usage is very simple: the idea is that you have an iterable that you want to operate on. For each item in the327iterable it might take some time to do processing. So say you have a loop like this:change extension to .mddenuowebbefore adoptionc002e08
line 328why, one step back328convert from reST to MySTdenuowebbefore adoption29eb946
lines 329–332why, one step back329```python330for user in all_the_users_to_process:331 modify_the_user(user)332```change extension to .mddenuowebbefore adoptionc002e08
line 333why, one step back333convert from reST to MySTdenuowebbefore adoption29eb946
line 334why, one step back334To hook this up with an automatically updating progress bar, all you need to do is to change the code to this:change extension to .mddenuowebbefore adoptionc002e08
line 335why, one step back335convert from reST to MySTdenuowebbefore adoption29eb946
lines 336–337why, one step back336```python337import clickchange extension to .mddenuowebbefore adoptionc002e08
line 338why, one step back338convert from reST to MySTdenuowebbefore adoption29eb946
lines 339–342why, one step back339with click.progressbar(all_the_users_to_process) as bar:340 for user in bar:341 modify_the_user(user)342```change extension to .mddenuowebbefore adoptionc002e08
line 343why, one step back343convert from reST to MySTdenuowebbefore adoption29eb946
lines 344–346why, one step back344Click will then automatically print a progress bar to the terminal and calculate the remaining time for you. The345calculation of remaining time requires that the iterable has a length. If it does not have a length but you know the346length, you can explicitly provide it:change extension to .mddenuowebbefore adoptionc002e08
line 347why, one step back347convert from reST to MySTdenuowebbefore adoption29eb946
lines 348–353why, one step back348```python349with click.progressbar(all_the_users_to_process,350 length=number_of_users) as bar:351 for user in bar:352 modify_the_user(user)353```change extension to .mddenuowebbefore adoptionc002e08
line 354why, one step back354convert from reST to MySTdenuowebbefore adoption29eb946
lines 355–356why, one step back355Note that {func}`click.progressbar` updates the bar *after* each iteration of the loop. So code like this will render356correctly:change extension to .mddenuowebbefore adoptionc002e08
line 357why, one step back357convert from reST to MySTdenuowebbefore adoption29eb946
lines 358–359why, one step back358```python359import timechange extension to .mddenuowebbefore adoptionc002e08
line 360why, one step back360convert from reST to MySTdenuowebbefore adoption29eb946
lines 361–365why, one step back361with click.progressbar([1, 2, 3]) as bar:362 for x in bar:363 print(f"sleep({x})...")364 time.sleep(x)365```change extension to .mddenuowebbefore adoptionc002e08
line 366why, one step back366convert from reST to MySTdenuowebbefore adoption29eb946
line 367why, one step back367Another useful feature is to associate a label with the progress bar which will be shown preceding the progress bar:change extension to .mddenuowebbefore adoptionc002e08
line 368why, one step back368convert from reST to MySTdenuowebbefore adoption29eb946
lines 369–373why, one step back369```python370with click.progressbar(all_the_users_to_process,371 label='Modifying user accounts',372 length=number_of_users) as bar:373 for user in bar:change extension to .mddenuowebbefore adoptionc002e08
line 374why, one step back374 modify_the_user(user)convert from reST to MySTdenuowebbefore adoption29eb946
line 375why, one step back375```change extension to .mddenuowebbefore adoptionc002e08
line 376why, one step back376convert from reST to MySTdenuowebbefore adoption29eb946
lines 377–379why, one step back377Sometimes, one may need to iterate over an external iterator, and advance the progress bar irregularly. To do so, you378need to specify the length (and no iterable), and use the update method on the context return value instead of iterating379directly over it:change extension to .mddenuowebbefore adoptionc002e08
line 380why, one step back380convert from reST to MySTdenuowebbefore adoption29eb946
lines 381–387why, one step back381```python382with click.progressbar(length=total_size,383 label='Unzipping archive') as bar:384 for archive in zip_file:385 archive.extract()386 bar.update(archive.size)387```change extension to .mddenuowebbefore adoptionc002e08
line 388why, one step back388convert from reST to MySTdenuowebbefore adoption29eb946
lines 389–390why, one step back389[colorama]: https://pypi.org/project/colorama/390[tqdm]: https://tqdm.github.io/