Skip to content
Myr

utils.md

This file

Path

docs/utils.md

392 lines · 12.2 KiB · Markdown

Last changed by

restore “eval-rst … click:example::” directivesdenuowebbefore adoption3b5ee71

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

restore “eval-rst … click:example::” directivesdenuowebbefore adoption3b5ee71

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

convert from reST to MySTdenuowebbefore adoption29eb946

line 97why, one step back
97```

change extension to .mddenuowebbefore adoptionc002e08

line 98why, one step back
98

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 101why, one step back
101

restore “eval-rst … click:example::” directivesdenuowebbefore adoption3b5ee71

lines 102–106why, one step back
102```{eval-rst}
103.. click:example::104 def _generate_output():105 for idx in range(50000):106 yield f"Line {idx}\n"

change extension to .mddenuowebbefore adoptionc002e08

line 107why, one step back
107

restore “eval-rst … click:example::” directivesdenuowebbefore adoption3b5ee71

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

convert from reST to MySTdenuowebbefore adoption29eb946

line 111why, one step back
111```

change extension to .mddenuowebbefore adoptionc002e08

line 112why, one step back
112

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 114why, one step back
114

convert from reST to MySTdenuowebbefore adoption29eb946

lines 115–116why, one step back
115```{versionadded} 2.0
116```

change extension to .mddenuowebbefore adoptionc002e08

line 117why, one step back
117

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

line 118why, one step back
118To 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 119why, one step back
119what the name suggests: it clears the entire visible screen in a platform-agnostic way:

change extension to .mddenuowebbefore adoptionc002e08

line 120why, one step back
120

add python label in codeblockdenuowebbefore adoptionba20b21

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

convert from reST to MySTdenuowebbefore adoption29eb946

lines 122–124why, one step back
122import click
123click.clear()124```

change extension to .mddenuowebbefore adoptionc002e08

line 125why, one step back
125

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 127why, one step back
127

convert from reST to MySTdenuowebbefore adoption29eb946

lines 128–129why, one step back
128```{versionadded} 2.0
129```

change extension to .mddenuowebbefore adoptionc002e08

line 130why, one step back
130

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 134why, one step back
134

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

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

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 137why, one step back
137

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 139why, one step back
139

convert from reST to MySTdenuowebbefore adoption29eb946

line 140why, one step back
140Example:

change extension to .mddenuowebbefore adoptionc002e08

line 141why, one step back
141

convert from reST to MySTdenuowebbefore adoption29eb946

lines 142–143why, one step back
142```python
143import click

change extension to .mddenuowebbefore adoptionc002e08

line 144why, one step back
144

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 155why, one step back
155

convert from reST to MySTdenuowebbefore adoption29eb946

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

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 162why, one step back
162

convert from reST to MySTdenuowebbefore adoption29eb946

lines 163–164why, one step back
163```{versionadded} 2.0
164```

change extension to .mddenuowebbefore adoptionc002e08

line 165why, one step back
165

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 168why, one step back
168

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

line 169why, one step back
169In 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 170–171why, one step back
170terminal (which can be customized) and wait for the user to press a key. In addition to that, it will also become a NOP
171(no operation instruction) if the script is not run interactively.

change extension to .mddenuowebbefore adoptionc002e08

line 172why, one step back
172

convert from reST to MySTdenuowebbefore adoption29eb946

line 173why, one step back
173Example:

change extension to .mddenuowebbefore adoptionc002e08

line 174why, one step back
174

convert from reST to MySTdenuowebbefore adoption29eb946

lines 175–178why, one step back
175```python
176import click177click.pause()178```

change extension to .mddenuowebbefore adoptionc002e08

line 179why, one step back
179

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 181why, one step back
181

convert from reST to MySTdenuowebbefore adoption29eb946

lines 182–183why, one step back
182```{versionadded} 2.0
183```

change extension to .mddenuowebbefore adoptionc002e08

line 184why, one step back
184

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

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

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 188why, one step back
188

convert from reST to MySTdenuowebbefore adoption29eb946

line 189why, one step back
189Example usage:

change extension to .mddenuowebbefore adoptionc002e08

line 190why, one step back
190

convert from reST to MySTdenuowebbefore adoption29eb946

lines 191–192why, one step back
191```python
192import click

change extension to .mddenuowebbefore adoptionc002e08

line 193why, one step back
193

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 200why, one step back
200

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 203why, one step back
203

convert from reST to MySTdenuowebbefore adoption29eb946

line 204why, one step back
204Example usage:

change extension to .mddenuowebbefore adoptionc002e08

line 205why, one step back
205

convert from reST to MySTdenuowebbefore adoption29eb946

lines 206–209why, one step back
206```python
207import click208click.edit(filename='/etc/passwd')209```

change extension to .mddenuowebbefore adoptionc002e08

line 210why, one step back
210

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 212why, one step back
212

convert from reST to MySTdenuowebbefore adoption29eb946

lines 213–214why, one step back
213```{versionadded} 2.0
214```

change extension to .mddenuowebbefore adoptionc002e08

line 215why, one step back
215

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

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

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 219why, one step back
219

convert from reST to MySTdenuowebbefore adoption29eb946

line 220why, one step back
220Example usage:

change extension to .mddenuowebbefore adoptionc002e08

line 221why, one step back
221

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 226why, one step back
226

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 228why, one step back
228

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 230why, one step back
230

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

line 231why, one step back
231The 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 232–233why, one step back
232filename to Unicode and will never fail. This makes it possible to use these filenames in the context of a full Unicode
233string.

change extension to .mddenuowebbefore adoptionc002e08

line 234why, one step back
234

convert from reST to MySTdenuowebbefore adoption29eb946

line 235why, one step back
235Example:

change extension to .mddenuowebbefore adoptionc002e08

line 236why, one step back
236

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 240why, one step back
240

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 242why, one step back
242

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 246why, one step back
246

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

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

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 249why, one step back
249

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 252why, one step back
252

convert from reST to MySTdenuowebbefore adoption29eb946

line 253why, one step back
253Example:

change extension to .mddenuowebbefore adoptionc002e08

line 254why, one step back
254

convert from reST to MySTdenuowebbefore adoption29eb946

lines 255–256why, one step back
255```python
256import click

change extension to .mddenuowebbefore adoptionc002e08

line 257why, one step back
257

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 261why, one step back
261

convert from reST to MySTdenuowebbefore adoption29eb946

lines 262–263why, one step back
262```{versionadded} 6.0
263```

change extension to .mddenuowebbefore adoptionc002e08

line 264why, one step back
264

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 267why, one step back
267

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 269why, one step back
269

convert from reST to MySTdenuowebbefore adoption29eb946

lines 270–271why, one step back
270```{versionadded} 3.0
271```

change extension to .mddenuowebbefore adoptionc002e08

line 272why, one step back
272

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

line 273why, one step back
273Starting 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 274why, one step back
274function. It can intelligently open stdin/stdout as well as any other file.

change extension to .mddenuowebbefore adoptionc002e08

line 275why, one step back
275

convert from reST to MySTdenuowebbefore adoption29eb946

line 276why, one step back
276Example:

change extension to .mddenuowebbefore adoptionc002e08

line 277why, one step back
277

convert from reST to MySTdenuowebbefore adoption29eb946

lines 278–279why, one step back
278```python
279import click

change extension to .mddenuowebbefore adoptionc002e08

line 280why, one step back
280

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 284why, one step back
284

convert from reST to MySTdenuowebbefore adoption29eb946

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

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 292why, one step back
292

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 294why, one step back
294

convert from reST to MySTdenuowebbefore adoption29eb946

lines 295–296why, one step back
295```{versionadded} 2.0
296```

change extension to .mddenuowebbefore adoptionc002e08

line 297why, one step back
297

convert from reST to MySTdenuowebbefore adoption29eb946

lines 298–299why, one step back
298Very often, you want to open a configuration file that belongs to your application. However, different operating systems
299store 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 300why, one step back
300{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 301why, one step back
301depending on the OS.

change extension to .mddenuowebbefore adoptionc002e08

line 302why, one step back
302

convert from reST to MySTdenuowebbefore adoption29eb946

line 303why, one step back
303Example usage:

change extension to .mddenuowebbefore adoptionc002e08

line 304why, one step back
304

convert from reST to MySTdenuowebbefore adoption29eb946

lines 305–308why, one step back
305```python
306import os307import click308import ConfigParser

change extension to .mddenuowebbefore adoptionc002e08

line 309why, one step back
309

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 321why, one step back
321

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 323why, one step back
323

convert from reST to MySTdenuowebbefore adoption29eb946

lines 324–325why, one step back
324Sometimes, you have command line scripts that need to process a lot of data, but you want to quickly show the user some
325progress 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 326why, one step back
326{func}`progressbar` function.

change extension to .mddenuowebbefore adoptionc002e08

line 327why, one step back
327

move header to top ofJaron Rosenaubefore adoption8231bdf

line 328why, one step back
328```{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 329why, one step back
329```

change extension to .mddenuowebbefore adoptionc002e08

line 330why, one step back
330

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 333why, one step back
333

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 338why, one step back
338

convert from reST to MySTdenuowebbefore adoption29eb946

line 339why, one step back
339To 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 340why, one step back
340

convert from reST to MySTdenuowebbefore adoption29eb946

lines 341–342why, one step back
341```python
342import click

change extension to .mddenuowebbefore adoptionc002e08

line 343why, one step back
343

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 348why, one step back
348

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 352why, one step back
352

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 359why, one step back
359

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

line 360why, one step back
360Note 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 361why, one step back
361correctly:

change extension to .mddenuowebbefore adoptionc002e08

line 362why, one step back
362

convert from reST to MySTdenuowebbefore adoption29eb946

lines 363–364why, one step back
363```python
364import time

change extension to .mddenuowebbefore adoptionc002e08

line 365why, one step back
365

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 371why, one step back
371

convert from reST to MySTdenuowebbefore adoption29eb946

line 372why, one step back
372Another 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 373why, one step back
373

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

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

convert from reST to MySTdenuowebbefore adoption29eb946

line 380why, one step back
380```

change extension to .mddenuowebbefore adoptionc002e08

line 381why, one step back
381

convert from reST to MySTdenuowebbefore adoption29eb946

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

change extension to .mddenuowebbefore adoptionc002e08

line 385why, one step back
385

convert from reST to MySTdenuowebbefore adoption29eb946

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