Skip to content
Myr

utils.md

This file

Path

docs/utils.md

390 lines · 12.1 KiB · Markdown

Last changed by

move header to top ofJaron Rosenaubefore adoption8231bdf

History of this file

Take it

move header to top ofJaron Rosenaubefore adoption8231bdf

lines 1–2why, one step back
1# Utilities
2

add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e

lines 3–5why, one step back
3```{currentmodule} click
4```5

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 8why, one step back
8

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 10why, one step back
10

add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e

line 11why, one step back
11The most obvious helper is the {func}`echo` function, which in many ways works like the Python `print` statement or

convert from reST to MySTdenuowebbefore adoption29eb946

line 12why, one step back
12function. The main difference is that it works the same in many different terminal environments.

change extension to .mddenuowebbefore adoptionc002e08

line 13why, one step back
13

convert from reST to MySTdenuowebbefore adoption29eb946

line 14why, one step back
14Example:

change extension to .mddenuowebbefore adoptionc002e08

line 15why, one step back
15

convert from reST to MySTdenuowebbefore adoption29eb946

lines 16–17why, one step back
16```python
17import click

change extension to .mddenuowebbefore adoptionc002e08

line 18why, one step back
18

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 21why, one step back
21

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 24why, one step back
24

convert from reST to MySTdenuowebbefore adoption29eb946

lines 25–27why, one step back
25```python
26click.echo(b'\xe2\x98\x83', nl=False)27```

change extension to .mddenuowebbefore adoptionc002e08

line 28why, one step back
28

add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e

line 29why, one step back
29Last but not least {func}`echo` uses click's intelligent internal output streams to stdout and stderr which support

convert from reST to MySTdenuowebbefore adoption29eb946

lines 30–31why, one step back
30unicode output on the Windows console. This means for as long as you are using `click.echo` you can output unicode
31characters (there are some limitations on the default font with regards to which characters can be displayed).

change extension to .mddenuowebbefore adoptionc002e08

line 32why, one step back
32

convert from reST to MySTdenuowebbefore adoption29eb946

lines 33–34why, one step back
33```{versionadded} 6.0
34```

change extension to .mddenuowebbefore adoptionc002e08

line 35why, one step back
35

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 38why, one step back
38

convert from reST to MySTdenuowebbefore adoption29eb946

lines 39–40why, one step back
39```{versionadded} 3.0
40```

change extension to .mddenuowebbefore adoptionc002e08

line 41why, one step back
41

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 43why, one step back
43

convert from reST to MySTdenuowebbefore adoption29eb946

lines 44–46why, one step back
44```python
45click.echo('Hello World!', err=True)46```

change extension to .mddenuowebbefore adoptionc002e08

line 47why, one step back
47

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 49why, one step back
49

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 51why, one step back
51

convert from reST to MySTdenuowebbefore adoption29eb946

lines 52–53why, one step back
52```{versionadded} 2.0
53```

change extension to .mddenuowebbefore adoptionc002e08

line 54why, one step back
54

move header to top ofJaron Rosenaubefore adoption8231bdf

line 55why, one step back
55The {func}`echo` function supports ANSI colors and styles. On Windows this uses [colorama](https://pypi.org/project/colorama/).

change extension to .mddenuowebbefore adoptionc002e08

lines 56–58why, one step back
56
57Primarily this means that:58

add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e

lines 59–60why, one step back
59- Click's {func}`echo` function will automatically strip ANSI color codes if the stream is not connected to a terminal.
60- the {func}`echo` function will transparently connect to the terminal on Windows and translate ANSI codes to terminal

convert from reST to MySTdenuowebbefore adoption29eb946

line 61why, one step back
61 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 62why, one step back
62

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 65why, one step back
65

add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e

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

change extension to .mddenuowebbefore adoptionc002e08

line 67why, one step back
67

convert from reST to MySTdenuowebbefore adoption29eb946

lines 68–69why, one step back
68```python
69import click

change extension to .mddenuowebbefore adoptionc002e08

line 70why, one step back
70

convert from reST to MySTdenuowebbefore adoption29eb946

lines 71–74why, one step back
71click.echo(click.style('Hello World!', fg='green'))
72click.echo(click.style('Some more text', bg='blue', fg='white'))73click.echo(click.style('ATTENTION', blink=True, bold=True))74```

change extension to .mddenuowebbefore adoptionc002e08

line 75why, one step back
75

add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e

line 76why, one step back
76The combination of {func}`echo` and {func}`style` is also available in a single function called {func}`secho`:

change extension to .mddenuowebbefore adoptionc002e08

line 77why, one step back
77

convert from reST to MySTdenuowebbefore adoption29eb946

lines 78–82why, one step back
78```python
79click.secho('Hello World!', fg='green')80click.secho('Some more text', bg='blue', fg='white')81click.secho('ATTENTION', blink=True, bold=True)82```

change extension to .mddenuowebbefore adoptionc002e08

line 83why, one step back
83

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 85why, one step back
85

convert from reST to MySTdenuowebbefore adoption29eb946

line 86why, one step back
86In some situations, you might want to show long texts on the terminal and let a user scroll through it. This can be

add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e

line 87why, one step back
87achieved by using the {func}`echo_via_pager` function which works similarly to the {func}`echo` function, but always

convert from reST to MySTdenuowebbefore adoption29eb946

line 88why, one step back
88writes to stdout and, if possible, through a pager.

change extension to .mddenuowebbefore adoptionc002e08

lines 89–91why, one step back
89
90Example:91

convert from reST to MySTdenuowebbefore adoption29eb946

lines 92–96why, one step back
92```python
93@click.command()94def less():95 click.echo_via_pager("\n".join(f"Line {idx}" for idx in range(200)))96```

change extension to .mddenuowebbefore adoptionc002e08

line 97why, one step back
97

convert from reST to MySTdenuowebbefore adoption29eb946

lines 98–99why, one step back
98If you want to use the pager for a lot of text, especially if generating everything in advance would take a lot of time,
99you can pass a generator (or generator function) instead of a string:

change extension to .mddenuowebbefore adoptionc002e08

line 100why, one step back
100

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 105why, one step back
105

convert from reST to MySTdenuowebbefore adoption29eb946

lines 106–109why, one step back
106@click.command()
107def less():108 click.echo_via_pager(_generate_output())109```

change extension to .mddenuowebbefore adoptionc002e08

line 110why, one step back
110

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 112why, one step back
112

convert from reST to MySTdenuowebbefore adoption29eb946

lines 113–114why, one step back
113```{versionadded} 2.0
114```

change extension to .mddenuowebbefore adoptionc002e08

line 115why, one step back
115

add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e

line 116why, one step back
116To clear the terminal screen, you can use the {func}`clear` function that is provided starting with Click 2.0. It does

convert from reST to MySTdenuowebbefore adoption29eb946

line 117why, one step back
117what the name suggests: it clears the entire visible screen in a platform-agnostic way:

change extension to .mddenuowebbefore adoptionc002e08

line 118why, one step back
118

add python label in codeblockdenuowebbefore adoptionba20b21

line 119why, one step back
119```python

convert from reST to MySTdenuowebbefore adoption29eb946

lines 120–122why, one step back
120import click
121click.clear()122```

change extension to .mddenuowebbefore adoptionc002e08

line 123why, one step back
123

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 125why, one step back
125

convert from reST to MySTdenuowebbefore adoption29eb946

lines 126–127why, one step back
126```{versionadded} 2.0
127```

change extension to .mddenuowebbefore adoptionc002e08

line 128why, one step back
128

convert from reST to MySTdenuowebbefore adoption29eb946

lines 129–131why, one step back
129Normally, when reading input from the terminal, you would read from standard input. However, this is buffered input and
130will not show up until the line has been terminated. In certain circumstances, you might not want to do that and instead131read individual characters as they are being written.

change extension to .mddenuowebbefore adoptionc002e08

line 132why, one step back
132

add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e

line 133why, one step back
133For this, Click provides the {func}`getchar` function which reads a single character from the terminal buffer and

convert from reST to MySTdenuowebbefore adoption29eb946

line 134why, one step back
134returns it as a Unicode character.

change extension to .mddenuowebbefore adoptionc002e08

line 135why, one step back
135

convert from reST to MySTdenuowebbefore adoption29eb946

line 136why, one step back
136Note that this function will always read from the terminal, even if stdin is instead a pipe.

change extension to .mddenuowebbefore adoptionc002e08

line 137why, one step back
137

convert from reST to MySTdenuowebbefore adoption29eb946

line 138why, one step back
138Example:

change extension to .mddenuowebbefore adoptionc002e08

line 139why, one step back
139

convert from reST to MySTdenuowebbefore adoption29eb946

lines 140–141why, one step back
140```python
141import click

change extension to .mddenuowebbefore adoptionc002e08

line 142why, one step back
142

convert from reST to MySTdenuowebbefore adoption29eb946

lines 143–152why, one step back
143click.echo('Continue? [yn] ', nl=False)
144c = click.getchar()145click.echo()146if c == 'y':147 click.echo('We will go on')148elif c == 'n':149 click.echo('Abort!')150else:151 click.echo('Invalid input :(')152```

change extension to .mddenuowebbefore adoptionc002e08

line 153why, one step back
153

convert from reST to MySTdenuowebbefore adoption29eb946

lines 154–156why, one step back
154Note that this reads raw input, which means that things like arrow keys will show up in the platform's native escape
155format. The only characters translated are `^C` and `^D` which are converted into keyboard interrupts and end of file156exceptions 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 157–158why, one step back
157cannot be properly exited.
158

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 160why, one step back
160

convert from reST to MySTdenuowebbefore adoption29eb946

lines 161–162why, one step back
161```{versionadded} 2.0
162```

change extension to .mddenuowebbefore adoptionc002e08

line 163why, one step back
163

convert from reST to MySTdenuowebbefore adoption29eb946

lines 164–165why, one step back
164Sometimes, it's useful to pause until the user presses any key on the keyboard. This is especially useful on Windows
165where `cmd.exe` will close the window at the end of the command execution by default, instead of waiting.

change extension to .mddenuowebbefore adoptionc002e08

line 166why, one step back
166

add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e

line 167why, one step back
167In click, this can be accomplished with the {func}`pause` function. This function will print a quick message to the

convert from reST to MySTdenuowebbefore adoption29eb946

lines 168–169why, one step back
168terminal (which can be customized) and wait for the user to press a key. In addition to that, it will also become a NOP
169(no operation instruction) if the script is not run interactively.

change extension to .mddenuowebbefore adoptionc002e08

line 170why, one step back
170

convert from reST to MySTdenuowebbefore adoption29eb946

line 171why, one step back
171Example:

change extension to .mddenuowebbefore adoptionc002e08

line 172why, one step back
172

convert from reST to MySTdenuowebbefore adoption29eb946

lines 173–176why, one step back
173```python
174import click175click.pause()176```

change extension to .mddenuowebbefore adoptionc002e08

line 177why, one step back
177

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 179why, one step back
179

convert from reST to MySTdenuowebbefore adoption29eb946

lines 180–181why, one step back
180```{versionadded} 2.0
181```

change extension to .mddenuowebbefore adoptionc002e08

line 182why, one step back
182

add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e

line 183why, one step back
183Click supports launching editors automatically through {func}`edit`. This is very useful for asking users for multi-line

convert from reST to MySTdenuowebbefore adoption29eb946

lines 184–185why, one step back
184input. It will automatically open the user's defined editor or fall back to a sensible default. If the user closes the
185editor without saving, the return value will be `None`, otherwise the entered text.

change extension to .mddenuowebbefore adoptionc002e08

line 186why, one step back
186

convert from reST to MySTdenuowebbefore adoption29eb946

line 187why, one step back
187Example usage:

change extension to .mddenuowebbefore adoptionc002e08

line 188why, one step back
188

convert from reST to MySTdenuowebbefore adoption29eb946

lines 189–190why, one step back
189```python
190import click

change extension to .mddenuowebbefore adoptionc002e08

line 191why, one step back
191

convert from reST to MySTdenuowebbefore adoption29eb946

lines 192–197why, one step back
192def get_commit_message():
193 MARKER = '# Everything below is ignored\n'194 message = click.edit('\n\n' + MARKER)195 if message is not None:196 return message.split(MARKER, 1)[0].rstrip('\n')197```

change extension to .mddenuowebbefore adoptionc002e08

line 198why, one step back
198

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 201why, one step back
201

convert from reST to MySTdenuowebbefore adoption29eb946

line 202why, one step back
202Example usage:

change extension to .mddenuowebbefore adoptionc002e08

line 203why, one step back
203

convert from reST to MySTdenuowebbefore adoption29eb946

lines 204–207why, one step back
204```python
205import click206click.edit(filename='/etc/passwd')207```

change extension to .mddenuowebbefore adoptionc002e08

line 208why, one step back
208

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 210why, one step back
210

convert from reST to MySTdenuowebbefore adoption29eb946

lines 211–212why, one step back
211```{versionadded} 2.0
212```

change extension to .mddenuowebbefore adoptionc002e08

line 213why, one step back
213

add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e

line 214why, one step back
214Click supports launching applications through {func}`launch`. This can be used to open the default application

convert from reST to MySTdenuowebbefore adoption29eb946

lines 215–216why, one step back
215associated with a URL or filetype. This can be used to launch web browsers or picture viewers, for instance. In addition
216to this, it can also launch the file manager and automatically select the provided file.

change extension to .mddenuowebbefore adoptionc002e08

line 217why, one step back
217

convert from reST to MySTdenuowebbefore adoption29eb946

line 218why, one step back
218Example usage:

change extension to .mddenuowebbefore adoptionc002e08

line 219why, one step back
219

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 224why, one step back
224

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 226why, one step back
226

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 228why, one step back
228

add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e

line 229why, one step back
229The way this works with click is through the {func}`format_filename` function. It does a best-effort conversion of the

convert from reST to MySTdenuowebbefore adoption29eb946

lines 230–231why, one step back
230filename to Unicode and will never fail. This makes it possible to use these filenames in the context of a full Unicode
231string.

change extension to .mddenuowebbefore adoptionc002e08

line 232why, one step back
232

convert from reST to MySTdenuowebbefore adoption29eb946

line 233why, one step back
233Example:

change extension to .mddenuowebbefore adoptionc002e08

line 234why, one step back
234

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 238why, one step back
238

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 240why, one step back
240

convert from reST to MySTdenuowebbefore adoption29eb946

lines 241–243why, one step back
241For command line utilities, it's very important to get access to input and output streams reliably. Python generally
242provides access to these streams through `sys.stdout` and friends, but unfortunately, there are API differences between2432.x and 3.x, especially with regards to how these streams respond to Unicode and binary data.

change extension to .mddenuowebbefore adoptionc002e08

line 244why, one step back
244

add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e

line 245why, one step back
245Because of this, click provides the {func}`get_binary_stream` and {func}`get_text_stream` functions, which produce

convert from reST to MySTdenuowebbefore adoption29eb946

line 246why, one step back
246consistent results with different Python versions and for a wide variety of terminal configurations.

change extension to .mddenuowebbefore adoptionc002e08

line 247why, one step back
247

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 250why, one step back
250

convert from reST to MySTdenuowebbefore adoption29eb946

line 251why, one step back
251Example:

change extension to .mddenuowebbefore adoptionc002e08

line 252why, one step back
252

convert from reST to MySTdenuowebbefore adoption29eb946

lines 253–254why, one step back
253```python
254import click

change extension to .mddenuowebbefore adoptionc002e08

line 255why, one step back
255

convert from reST to MySTdenuowebbefore adoption29eb946

lines 256–258why, one step back
256stdin_text = click.get_text_stream('stdin')
257stdout_binary = click.get_binary_stream('stdout')258```

change extension to .mddenuowebbefore adoptionc002e08

line 259why, one step back
259

convert from reST to MySTdenuowebbefore adoption29eb946

lines 260–261why, one step back
260```{versionadded} 6.0
261```

change extension to .mddenuowebbefore adoptionc002e08

line 262why, one step back
262

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 265why, one step back
265

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 267why, one step back
267

convert from reST to MySTdenuowebbefore adoption29eb946

lines 268–269why, one step back
268```{versionadded} 3.0
269```

change extension to .mddenuowebbefore adoptionc002e08

line 270why, one step back
270

add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e

line 271why, one step back
271Starting with Click 3.0 the logic for opening files from the {func}`File` type is exposed through the {func}`open_file`

convert from reST to MySTdenuowebbefore adoption29eb946

line 272why, one step back
272function. It can intelligently open stdin/stdout as well as any other file.

change extension to .mddenuowebbefore adoptionc002e08

line 273why, one step back
273

convert from reST to MySTdenuowebbefore adoption29eb946

line 274why, one step back
274Example:

change extension to .mddenuowebbefore adoptionc002e08

line 275why, one step back
275

convert from reST to MySTdenuowebbefore adoption29eb946

lines 276–277why, one step back
276```python
277import click

change extension to .mddenuowebbefore adoptionc002e08

line 278why, one step back
278

convert from reST to MySTdenuowebbefore adoption29eb946

lines 279–281why, one step back
279stdout = click.open_file('-', 'w')
280test_file = click.open_file('test.txt', 'w')281```

change extension to .mddenuowebbefore adoptionc002e08

line 282why, one step back
282

convert from reST to MySTdenuowebbefore adoption29eb946

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

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 290why, one step back
290

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 292why, one step back
292

convert from reST to MySTdenuowebbefore adoption29eb946

lines 293–294why, one step back
293```{versionadded} 2.0
294```

change extension to .mddenuowebbefore adoptionc002e08

line 295why, one step back
295

convert from reST to MySTdenuowebbefore adoption29eb946

lines 296–297why, one step back
296Very often, you want to open a configuration file that belongs to your application. However, different operating systems
297store these configuration files in different locations depending on their standards. Click provides a

add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e

line 298why, one step back
298{func}`get_app_dir` function which returns the most appropriate location for per-user config files for your application

convert from reST to MySTdenuowebbefore adoption29eb946

line 299why, one step back
299depending on the OS.

change extension to .mddenuowebbefore adoptionc002e08

line 300why, one step back
300

convert from reST to MySTdenuowebbefore adoption29eb946

line 301why, one step back
301Example usage:

change extension to .mddenuowebbefore adoptionc002e08

line 302why, one step back
302

convert from reST to MySTdenuowebbefore adoption29eb946

lines 303–306why, one step back
303```python
304import os305import click306import ConfigParser

change extension to .mddenuowebbefore adoptionc002e08

line 307why, one step back
307

convert from reST to MySTdenuowebbefore adoption29eb946

lines 308–318why, one step back
308APP_NAME = 'My Application'
309def read_config():310cfg = os.path.join(click.get_app_dir(APP_NAME), 'config.ini')311parser = ConfigParser.RawConfigParser()312parser.read([cfg])313rv = {}314for section in parser.sections():315 for key, value in parser.items(section):316 rv[f"{section}.{key}"] = value317return rv318```

change extension to .mddenuowebbefore adoptionc002e08

line 319why, one step back
319

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 321why, one step back
321

convert from reST to MySTdenuowebbefore adoption29eb946

lines 322–323why, one step back
322Sometimes, you have command line scripts that need to process a lot of data, but you want to quickly show the user some
323progress about how long that will take. Click supports simple progress bar rendering for that through the

add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e

line 324why, one step back
324{func}`progressbar` function.

change extension to .mddenuowebbefore adoptionc002e08

line 325why, one step back
325

move header to top ofJaron Rosenaubefore adoption8231bdf

line 326why, one step back
326```{note} If you find that you have requirements beyond what Click's progress bar supports, try using [tqdm](https://tqdm.github.io/).

convert from reST to MySTdenuowebbefore adoption29eb946

line 327why, one step back
327```

change extension to .mddenuowebbefore adoptionc002e08

line 328why, one step back
328

convert from reST to MySTdenuowebbefore adoption29eb946

lines 329–330why, one step back
329The basic usage is very simple: the idea is that you have an iterable that you want to operate on. For each item in the
330iterable it might take some time to do processing. So say you have a loop like this:

change extension to .mddenuowebbefore adoptionc002e08

line 331why, one step back
331

convert from reST to MySTdenuowebbefore adoption29eb946

lines 332–335why, one step back
332```python
333for user in all_the_users_to_process:334 modify_the_user(user)335```

change extension to .mddenuowebbefore adoptionc002e08

line 336why, one step back
336

convert from reST to MySTdenuowebbefore adoption29eb946

line 337why, one step back
337To 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 338why, one step back
338

convert from reST to MySTdenuowebbefore adoption29eb946

lines 339–340why, one step back
339```python
340import click

change extension to .mddenuowebbefore adoptionc002e08

line 341why, one step back
341

convert from reST to MySTdenuowebbefore adoption29eb946

lines 342–345why, one step back
342with click.progressbar(all_the_users_to_process) as bar:
343 for user in bar:344 modify_the_user(user)345```

change extension to .mddenuowebbefore adoptionc002e08

line 346why, one step back
346

convert from reST to MySTdenuowebbefore adoption29eb946

lines 347–349why, one step back
347Click will then automatically print a progress bar to the terminal and calculate the remaining time for you. The
348calculation of remaining time requires that the iterable has a length. If it does not have a length but you know the349length, you can explicitly provide it:

change extension to .mddenuowebbefore adoptionc002e08

line 350why, one step back
350

convert from reST to MySTdenuowebbefore adoption29eb946

lines 351–356why, one step back
351```python
352with click.progressbar(all_the_users_to_process,353 length=number_of_users) as bar:354 for user in bar:355 modify_the_user(user)356```

change extension to .mddenuowebbefore adoptionc002e08

line 357why, one step back
357

add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e

line 358why, one step back
358Note that {func}`progressbar` updates the bar *after* each iteration of the loop. So code like this will render

convert from reST to MySTdenuowebbefore adoption29eb946

line 359why, one step back
359correctly:

change extension to .mddenuowebbefore adoptionc002e08

line 360why, one step back
360

convert from reST to MySTdenuowebbefore adoption29eb946

lines 361–362why, one step back
361```python
362import time

change extension to .mddenuowebbefore adoptionc002e08

line 363why, one step back
363

convert from reST to MySTdenuowebbefore adoption29eb946

lines 364–368why, one step back
364with click.progressbar([1, 2, 3]) as bar:
365 for x in bar:366 print(f"sleep({x})...")367 time.sleep(x)368```

change extension to .mddenuowebbefore adoptionc002e08

line 369why, one step back
369

convert from reST to MySTdenuowebbefore adoption29eb946

line 370why, one step back
370Another 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 371why, one step back
371

convert from reST to MySTdenuowebbefore adoption29eb946

lines 372–376why, one step back
372```python
373with click.progressbar(all_the_users_to_process,374 label='Modifying user accounts',375 length=number_of_users) as bar:376 for user in bar:

change extension to .mddenuowebbefore adoptionc002e08

line 377why, one step back
377 modify_the_user(user)

convert from reST to MySTdenuowebbefore adoption29eb946

line 378why, one step back
378```

change extension to .mddenuowebbefore adoptionc002e08

line 379why, one step back
379

convert from reST to MySTdenuowebbefore adoption29eb946

lines 380–382why, one step back
380Sometimes, one may need to iterate over an external iterator, and advance the progress bar irregularly. To do so, you
381need to specify the length (and no iterable), and use the update method on the context return value instead of iterating382directly over it:

change extension to .mddenuowebbefore adoptionc002e08

line 383why, one step back
383

convert from reST to MySTdenuowebbefore adoption29eb946

lines 384–390why, one step back
384```python
385with click.progressbar(length=total_size,386 label='Unzipping archive') as bar:387 for archive in zip_file:388 archive.extract()389 bar.update(archive.size)390```