Skip to content
Myr

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 file

Take it

convert from reST to MySTdenuowebbefore adoption29eb946

line 1why, one step back
1# Utilities

change extension to .mddenuowebbefore adoptionc002e08

line 2why, one step back
2

convert from reST to MySTdenuowebbefore adoption29eb946

lines 3–4why, one step back
3Besides the functionality that Click provides to interface with argument parsing and handling, it also provides a bunch
4of addon functionality that is useful for writing command line utilities.

change extension to .mddenuowebbefore adoptionc002e08

line 5why, one step back
5

convert from reST to MySTdenuowebbefore adoption29eb946

line 6why, one step back
6## Printing to Stdout

change extension to .mddenuowebbefore adoptionc002e08

line 7why, one step back
7

convert from reST to MySTdenuowebbefore adoption29eb946

lines 8–9why, one step back
8The most obvious helper is the {func}`click.echo` function, which in many ways works like the Python `print` statement or
9function. The main difference is that it works the same in many different terminal environments.

change extension to .mddenuowebbefore adoptionc002e08

line 10why, one step back
10

convert from reST to MySTdenuowebbefore adoption29eb946

line 11why, one step back
11Example:

change extension to .mddenuowebbefore adoptionc002e08

line 12why, one step back
12

convert from reST to MySTdenuowebbefore adoption29eb946

lines 13–14why, one step back
13```python
14import click

change extension to .mddenuowebbefore adoptionc002e08

line 15why, one step back
15

convert from reST to MySTdenuowebbefore adoption29eb946

lines 16–17why, one step back
16click.echo('Hello World!')
17```

change extension to .mddenuowebbefore adoptionc002e08

line 18why, one step back
18

convert from reST to MySTdenuowebbefore adoption29eb946

lines 19–20why, one step back
19It can output both text and binary data. It will emit a trailing newline by default, which needs to be suppressed by
20passing `nl=False`:

change extension to .mddenuowebbefore adoptionc002e08

line 21why, one step back
21

convert from reST to MySTdenuowebbefore adoption29eb946

lines 22–24why, one step back
22```python
23click.echo(b'\xe2\x98\x83', nl=False)24```

change extension to .mddenuowebbefore adoptionc002e08

line 25why, one step back
25

convert from reST to MySTdenuowebbefore adoption29eb946

lines 26–28why, one step back
26Last but not least {func}`click.echo` uses click's intelligent internal output streams to stdout and stderr which support
27unicode 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 back
29

convert from reST to MySTdenuowebbefore adoption29eb946

lines 30–31why, one step back
30```{versionadded} 6.0
31```

change extension to .mddenuowebbefore adoptionc002e08

line 32why, one step back
32

convert from reST to MySTdenuowebbefore adoption29eb946

lines 33–34why, one step back
33Click emulates output streams on Windows to support unicode to the Windows console through separate APIs. For more
34information see {doc}`wincmd`.

change extension to .mddenuowebbefore adoptionc002e08

line 35why, one step back
35

convert from reST to MySTdenuowebbefore adoption29eb946

lines 36–37why, one step back
36```{versionadded} 3.0
37```

change extension to .mddenuowebbefore adoptionc002e08

line 38why, one step back
38

convert from reST to MySTdenuowebbefore adoption29eb946

line 39why, one step back
39You can also easily print to standard error by passing `err=True`:

change extension to .mddenuowebbefore adoptionc002e08

line 40why, one step back
40

convert from reST to MySTdenuowebbefore adoption29eb946

lines 41–43why, one step back
41```python
42click.echo('Hello World!', err=True)43```

change extension to .mddenuowebbefore adoptionc002e08

line 44why, one step back
44

convert from reST to MySTdenuowebbefore adoption29eb946

line 45why, one step back
45(ansi-colors)=

change extension to .mddenuowebbefore adoptionc002e08

line 46why, one step back
46

convert from reST to MySTdenuowebbefore adoption29eb946

line 47why, one step back
47## ANSI Colors

change extension to .mddenuowebbefore adoptionc002e08

line 48why, one step back
48

convert from reST to MySTdenuowebbefore adoption29eb946

lines 49–50why, one step back
49```{versionadded} 2.0
50```

change extension to .mddenuowebbefore adoptionc002e08

line 51why, one step back
51

convert from reST to MySTdenuowebbefore adoption29eb946

line 52why, one step back
52The {func}`click.echo` function supports ANSI colors and styles. On Windows this uses [colorama].

change extension to .mddenuowebbefore adoptionc002e08

lines 53–55why, one step back
53
54Primarily this means that:55

convert from reST to MySTdenuowebbefore adoption29eb946

lines 56–58why, one step back
56- 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 back
59

convert from reST to MySTdenuowebbefore adoption29eb946

lines 60–61why, one step back
60On Windows, Click uses colorama without calling `colorama.init()`. You can still call that in your code, but it's not
61required for Click.

change extension to .mddenuowebbefore adoptionc002e08

line 62why, one step back
62

convert from reST to MySTdenuowebbefore adoption29eb946

line 63why, one step back
63For styling a string, the {func}`click.style` function can be used:

change extension to .mddenuowebbefore adoptionc002e08

line 64why, one step back
64

convert from reST to MySTdenuowebbefore adoption29eb946

lines 65–66why, one step back
65```python
66import click

change extension to .mddenuowebbefore adoptionc002e08

line 67why, one step back
67

convert from reST to MySTdenuowebbefore adoption29eb946

lines 68–71why, one step back
68click.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 back
72

convert from reST to MySTdenuowebbefore adoption29eb946

line 73why, one step back
73The 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 back
74

convert from reST to MySTdenuowebbefore adoption29eb946

lines 75–79why, one step back
75```python
76click.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 back
80

convert from reST to MySTdenuowebbefore adoption29eb946

line 81why, one step back
81## Pager Support

change extension to .mddenuowebbefore adoptionc002e08

line 82why, one step back
82

convert from reST to MySTdenuowebbefore adoption29eb946

lines 83–85why, one step back
83In some situations, you might want to show long texts on the terminal and let a user scroll through it. This can be
84achieved 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 back
86
87Example:88

convert from reST to MySTdenuowebbefore adoption29eb946

lines 89–93why, one step back
89```python
90@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 back
94

convert from reST to MySTdenuowebbefore adoption29eb946

lines 95–96why, one step back
95If 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 back
97

convert from reST to MySTdenuowebbefore adoption29eb946

lines 98–101why, one step back
98```python
99def _generate_output():100 for idx in range(50000):101 yield f"Line {idx}\n"

change extension to .mddenuowebbefore adoptionc002e08

line 102why, one step back
102

convert from reST to MySTdenuowebbefore adoption29eb946

lines 103–106why, one step back
103@click.command()
104def less():105 click.echo_via_pager(_generate_output())106```

change extension to .mddenuowebbefore adoptionc002e08

line 107why, one step back
107

convert from reST to MySTdenuowebbefore adoption29eb946

line 108why, one step back
108## Screen Clearing

change extension to .mddenuowebbefore adoptionc002e08

line 109why, one step back
109

convert from reST to MySTdenuowebbefore adoption29eb946

lines 110–111why, one step back
110```{versionadded} 2.0
111```

change extension to .mddenuowebbefore adoptionc002e08

line 112why, one step back
112

convert from reST to MySTdenuowebbefore adoption29eb946

lines 113–114why, one step back
113To clear the terminal screen, you can use the {func}`click.clear` function that is provided starting with Click 2.0. It does
114what the name suggests: it clears the entire visible screen in a platform-agnostic way:

change extension to .mddenuowebbefore adoptionc002e08

line 115why, one step back
115

convert from reST to MySTdenuowebbefore adoption29eb946

lines 116–119why, one step back
116```
117import click118click.clear()119```

change extension to .mddenuowebbefore adoptionc002e08

line 120why, one step back
120

convert from reST to MySTdenuowebbefore adoption29eb946

line 121why, one step back
121## Getting Characters from Terminal

change extension to .mddenuowebbefore adoptionc002e08

line 122why, one step back
122

convert from reST to MySTdenuowebbefore adoption29eb946

lines 123–124why, one step back
123```{versionadded} 2.0
124```

change extension to .mddenuowebbefore adoptionc002e08

line 125why, one step back
125

convert from reST to MySTdenuowebbefore adoption29eb946

lines 126–128why, one step back
126Normally, when reading input from the terminal, you would read from standard input. However, this is buffered input and
127will 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 back
129

convert from reST to MySTdenuowebbefore adoption29eb946

lines 130–131why, one step back
130For this, Click provides the {func}`click.getchar` function which reads a single character from the terminal buffer and
131returns it as a Unicode character.

change extension to .mddenuowebbefore adoptionc002e08

line 132why, one step back
132

convert from reST to MySTdenuowebbefore adoption29eb946

line 133why, one step back
133Note 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 back
134

convert from reST to MySTdenuowebbefore adoption29eb946

line 135why, one step back
135Example:

change extension to .mddenuowebbefore adoptionc002e08

line 136why, one step back
136

convert from reST to MySTdenuowebbefore adoption29eb946

lines 137–138why, one step back
137```python
138import click

change extension to .mddenuowebbefore adoptionc002e08

line 139why, one step back
139

convert from reST to MySTdenuowebbefore adoption29eb946

lines 140–149why, one step back
140click.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 back
150

convert from reST to MySTdenuowebbefore adoption29eb946

lines 151–153why, one step back
151Note that this reads raw input, which means that things like arrow keys will show up in the platform's native escape
152format. 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 that

change extension to .mddenuowebbefore adoptionc002e08

lines 154–155why, one step back
154cannot be properly exited.
155

convert from reST to MySTdenuowebbefore adoption29eb946

line 156why, one step back
156## Waiting for Key Press

change extension to .mddenuowebbefore adoptionc002e08

line 157why, one step back
157

convert from reST to MySTdenuowebbefore adoption29eb946

lines 158–159why, one step back
158```{versionadded} 2.0
159```

change extension to .mddenuowebbefore adoptionc002e08

line 160why, one step back
160

convert from reST to MySTdenuowebbefore adoption29eb946

lines 161–162why, one step back
161Sometimes, it's useful to pause until the user presses any key on the keyboard. This is especially useful on Windows
162where `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 back
163

convert from reST to MySTdenuowebbefore adoption29eb946

lines 164–166why, one step back
164In click, this can be accomplished with the {func}`click.pause` function. This function will print a quick message to the
165terminal (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 back
167

convert from reST to MySTdenuowebbefore adoption29eb946

line 168why, one step back
168Example:

change extension to .mddenuowebbefore adoptionc002e08

line 169why, one step back
169

convert from reST to MySTdenuowebbefore adoption29eb946

lines 170–173why, one step back
170```python
171import click172click.pause()173```

change extension to .mddenuowebbefore adoptionc002e08

line 174why, one step back
174

convert from reST to MySTdenuowebbefore adoption29eb946

line 175why, one step back
175## Launching Editors

change extension to .mddenuowebbefore adoptionc002e08

line 176why, one step back
176

convert from reST to MySTdenuowebbefore adoption29eb946

lines 177–178why, one step back
177```{versionadded} 2.0
178```

change extension to .mddenuowebbefore adoptionc002e08

line 179why, one step back
179

convert from reST to MySTdenuowebbefore adoption29eb946

lines 180–182why, one step back
180Click supports launching editors automatically through {func}`click.edit`. This is very useful for asking users for multi-line
181input. 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 back
183

convert from reST to MySTdenuowebbefore adoption29eb946

line 184why, one step back
184Example usage:

change extension to .mddenuowebbefore adoptionc002e08

line 185why, one step back
185

convert from reST to MySTdenuowebbefore adoption29eb946

lines 186–187why, one step back
186```python
187import click

change extension to .mddenuowebbefore adoptionc002e08

line 188why, one step back
188

convert from reST to MySTdenuowebbefore adoption29eb946

lines 189–194why, one step back
189def 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 back
195

convert from reST to MySTdenuowebbefore adoption29eb946

lines 196–197why, one step back
196Alternatively, the function can also be used to launch editors for files by a specific filename. In this case, the
197return value is always `None`.

change extension to .mddenuowebbefore adoptionc002e08

line 198why, one step back
198

convert from reST to MySTdenuowebbefore adoption29eb946

line 199why, one step back
199Example usage:

change extension to .mddenuowebbefore adoptionc002e08

line 200why, one step back
200

convert from reST to MySTdenuowebbefore adoption29eb946

lines 201–204why, one step back
201```python
202import click203click.edit(filename='/etc/passwd')204```

change extension to .mddenuowebbefore adoptionc002e08

line 205why, one step back
205

convert from reST to MySTdenuowebbefore adoption29eb946

line 206why, one step back
206## Launching Applications

change extension to .mddenuowebbefore adoptionc002e08

line 207why, one step back
207

convert from reST to MySTdenuowebbefore adoption29eb946

lines 208–209why, one step back
208```{versionadded} 2.0
209```

change extension to .mddenuowebbefore adoptionc002e08

line 210why, one step back
210

convert from reST to MySTdenuowebbefore adoption29eb946

lines 211–213why, one step back
211Click supports launching applications through {func}`click.launch`. This can be used to open the default application
212associated 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 back
214

convert from reST to MySTdenuowebbefore adoption29eb946

line 215why, one step back
215Example usage:

change extension to .mddenuowebbefore adoptionc002e08

line 216why, one step back
216

convert from reST to MySTdenuowebbefore adoption29eb946

lines 217–220why, one step back
217```python
218click.launch("https://click.palletsprojects.com/")219click.launch("/my/downloaded/file.txt", locate=True)220```

change extension to .mddenuowebbefore adoptionc002e08

line 221why, one step back
221

convert from reST to MySTdenuowebbefore adoption29eb946

line 222why, one step back
222## Printing Filenames

change extension to .mddenuowebbefore adoptionc002e08

line 223why, one step back
223

convert from reST to MySTdenuowebbefore adoption29eb946

line 224why, one step back
224Because filenames might not be Unicode, formatting them can be a bit tricky.

change extension to .mddenuowebbefore adoptionc002e08

line 225why, one step back
225

convert from reST to MySTdenuowebbefore adoption29eb946

lines 226–228why, one step back
226The way this works with click is through the {func}`click.format_filename` function. It does a best-effort conversion of the
227filename 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 back
229

convert from reST to MySTdenuowebbefore adoption29eb946

line 230why, one step back
230Example:

change extension to .mddenuowebbefore adoptionc002e08

line 231why, one step back
231

convert from reST to MySTdenuowebbefore adoption29eb946

lines 232–234why, one step back
232```python
233click.echo(f"Path: {click.format_filename(b'foo.txt')}")234```

change extension to .mddenuowebbefore adoptionc002e08

line 235why, one step back
235

convert from reST to MySTdenuowebbefore adoption29eb946

line 236why, one step back
236## Standard Streams

change extension to .mddenuowebbefore adoptionc002e08

line 237why, one step back
237

convert from reST to MySTdenuowebbefore adoption29eb946

lines 238–240why, one step back
238For command line utilities, it's very important to get access to input and output streams reliably. Python generally
239provides 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 back
241

convert from reST to MySTdenuowebbefore adoption29eb946

lines 242–243why, one step back
242Because of this, click provides the {func}`click.get_binary_stream` and {func}`click.get_text_stream` functions, which produce
243consistent results with different Python versions and for a wide variety of terminal configurations.

change extension to .mddenuowebbefore adoptionc002e08

line 244why, one step back
244

convert from reST to MySTdenuowebbefore adoption29eb946

lines 245–246why, one step back
245The end result is that these functions will always return a functional stream object (except in very odd cases; see
246{doc}`/unicode-support`).

change extension to .mddenuowebbefore adoptionc002e08

line 247why, one step back
247

convert from reST to MySTdenuowebbefore adoption29eb946

line 248why, one step back
248Example:

change extension to .mddenuowebbefore adoptionc002e08

line 249why, one step back
249

convert from reST to MySTdenuowebbefore adoption29eb946

lines 250–251why, one step back
250```python
251import click

change extension to .mddenuowebbefore adoptionc002e08

line 252why, one step back
252

convert from reST to MySTdenuowebbefore adoption29eb946

lines 253–255why, one step back
253stdin_text = click.get_text_stream('stdin')
254stdout_binary = click.get_binary_stream('stdout')255```

change extension to .mddenuowebbefore adoptionc002e08

line 256why, one step back
256

convert from reST to MySTdenuowebbefore adoption29eb946

lines 257–258why, one step back
257```{versionadded} 6.0
258```

change extension to .mddenuowebbefore adoptionc002e08

line 259why, one step back
259

convert from reST to MySTdenuowebbefore adoption29eb946

lines 260–261why, one step back
260Click now emulates output streams on Windows to support unicode to the Windows console through separate APIs. For more
261information see {doc}`wincmd`.

change extension to .mddenuowebbefore adoptionc002e08

line 262why, one step back
262

convert from reST to MySTdenuowebbefore adoption29eb946

line 263why, one step back
263## Intelligent File Opening

change extension to .mddenuowebbefore adoptionc002e08

line 264why, one step back
264

convert from reST to MySTdenuowebbefore adoption29eb946

lines 265–266why, one step back
265```{versionadded} 3.0
266```

change extension to .mddenuowebbefore adoptionc002e08

line 267why, one step back
267

convert from reST to MySTdenuowebbefore adoption29eb946

lines 268–269why, one step back
268Starting 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 back
270

convert from reST to MySTdenuowebbefore adoption29eb946

line 271why, one step back
271Example:

change extension to .mddenuowebbefore adoptionc002e08

line 272why, one step back
272

convert from reST to MySTdenuowebbefore adoption29eb946

lines 273–274why, one step back
273```python
274import click

change extension to .mddenuowebbefore adoptionc002e08

line 275why, one step back
275

convert from reST to MySTdenuowebbefore adoption29eb946

lines 276–278why, one step back
276stdout = click.open_file('-', 'w')
277test_file = click.open_file('test.txt', 'w')278```

change extension to .mddenuowebbefore adoptionc002e08

line 279why, one step back
279

convert from reST to MySTdenuowebbefore adoption29eb946

lines 280–281why, one step back
280If stdin or stdout are returned, the return value is wrapped in a special file where the context manager will prevent
281the 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 back
282

convert from reST to MySTdenuowebbefore adoption29eb946

lines 283–286why, one step back
283```python
284with click.open_file(filename, 'w') as f:285 f.write('Hello World!\n')286```

change extension to .mddenuowebbefore adoptionc002e08

line 287why, one step back
287

convert from reST to MySTdenuowebbefore adoption29eb946

line 288why, one step back
288## Finding Application Folders

change extension to .mddenuowebbefore adoptionc002e08

line 289why, one step back
289

convert from reST to MySTdenuowebbefore adoption29eb946

lines 290–291why, one step back
290```{versionadded} 2.0
291```

change extension to .mddenuowebbefore adoptionc002e08

line 292why, one step back
292

convert from reST to MySTdenuowebbefore adoption29eb946

lines 293–296why, one step back
293Very often, you want to open a configuration file that belongs to your application. However, different operating systems
294store 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 back
297

convert from reST to MySTdenuowebbefore adoption29eb946

line 298why, one step back
298Example usage:

change extension to .mddenuowebbefore adoptionc002e08

line 299why, one step back
299

convert from reST to MySTdenuowebbefore adoption29eb946

lines 300–303why, one step back
300```python
301import os302import click303import ConfigParser

change extension to .mddenuowebbefore adoptionc002e08

line 304why, one step back
304

convert from reST to MySTdenuowebbefore adoption29eb946

lines 305–315why, one step back
305APP_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 back
316

convert from reST to MySTdenuowebbefore adoption29eb946

line 317why, one step back
317## Showing Progress Bars

change extension to .mddenuowebbefore adoptionc002e08

line 318why, one step back
318

convert from reST to MySTdenuowebbefore adoption29eb946

lines 319–321why, one step back
319Sometimes, you have command line scripts that need to process a lot of data, but you want to quickly show the user some
320progress 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 back
322

convert from reST to MySTdenuowebbefore adoption29eb946

lines 323–324why, one step back
323```{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 back
325

convert from reST to MySTdenuowebbefore adoption29eb946

lines 326–327why, one step back
326The basic usage is very simple: the idea is that you have an iterable that you want to operate on. For each item in the
327iterable 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 back
328

convert from reST to MySTdenuowebbefore adoption29eb946

lines 329–332why, one step back
329```python
330for user in all_the_users_to_process:331 modify_the_user(user)332```

change extension to .mddenuowebbefore adoptionc002e08

line 333why, one step back
333

convert from reST to MySTdenuowebbefore adoption29eb946

line 334why, one step back
334To 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 back
335

convert from reST to MySTdenuowebbefore adoption29eb946

lines 336–337why, one step back
336```python
337import click

change extension to .mddenuowebbefore adoptionc002e08

line 338why, one step back
338

convert from reST to MySTdenuowebbefore adoption29eb946

lines 339–342why, one step back
339with 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 back
343

convert from reST to MySTdenuowebbefore adoption29eb946

lines 344–346why, one step back
344Click will then automatically print a progress bar to the terminal and calculate the remaining time for you. The
345calculation 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 back
347

convert from reST to MySTdenuowebbefore adoption29eb946

lines 348–353why, one step back
348```python
349with 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 back
354

convert from reST to MySTdenuowebbefore adoption29eb946

lines 355–356why, one step back
355Note that {func}`click.progressbar` updates the bar *after* each iteration of the loop. So code like this will render
356correctly:

change extension to .mddenuowebbefore adoptionc002e08

line 357why, one step back
357

convert from reST to MySTdenuowebbefore adoption29eb946

lines 358–359why, one step back
358```python
359import time

change extension to .mddenuowebbefore adoptionc002e08

line 360why, one step back
360

convert from reST to MySTdenuowebbefore adoption29eb946

lines 361–365why, one step back
361with 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 back
366

convert from reST to MySTdenuowebbefore adoption29eb946

line 367why, one step back
367Another 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 back
368

convert from reST to MySTdenuowebbefore adoption29eb946

lines 369–373why, one step back
369```python
370with 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 back
374 modify_the_user(user)

convert from reST to MySTdenuowebbefore adoption29eb946

line 375why, one step back
375```

change extension to .mddenuowebbefore adoptionc002e08

line 376why, one step back
376

convert from reST to MySTdenuowebbefore adoption29eb946

lines 377–379why, one step back
377Sometimes, one may need to iterate over an external iterator, and advance the progress bar irregularly. To do so, you
378need 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 back
380

convert from reST to MySTdenuowebbefore adoption29eb946

lines 381–387why, one step back
381```python
382with 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 back
388

convert from reST to MySTdenuowebbefore adoption29eb946

lines 389–390why, one step back
389[colorama]: https://pypi.org/project/colorama/
390[tqdm]: https://tqdm.github.io/