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 fileTake it
move header to top ofJaron Rosenaubefore adoption8231bdf
lines 1–2why, one step back1# Utilities2add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e
lines 3–5why, one step back3```{currentmodule} click4```5convert from reST to MySTdenuowebbefore adoption29eb946
lines 6–7why, one step back6Besides the functionality that Click provides to interface with argument parsing and handling, it also provides a bunch7of addon functionality that is useful for writing command line utilities.change extension to .mddenuowebbefore adoptionc002e08
line 8why, one step back8convert from reST to MySTdenuowebbefore adoption29eb946
line 9why, one step back9## Printing to Stdoutchange extension to .mddenuowebbefore adoptionc002e08
line 10why, one step back10add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e
line 11why, one step back11The most obvious helper is the {func}`echo` function, which in many ways works like the Python `print` statement orconvert from reST to MySTdenuowebbefore adoption29eb946
line 12why, one step back12function. The main difference is that it works the same in many different terminal environments.change extension to .mddenuowebbefore adoptionc002e08
line 13why, one step back13convert from reST to MySTdenuowebbefore adoption29eb946
line 14why, one step back14Example:change extension to .mddenuowebbefore adoptionc002e08
line 15why, one step back15convert from reST to MySTdenuowebbefore adoption29eb946
lines 16–17why, one step back16```python17import clickchange extension to .mddenuowebbefore adoptionc002e08
line 18why, one step back18convert from reST to MySTdenuowebbefore adoption29eb946
lines 19–20why, one step back19click.echo('Hello World!')20```change extension to .mddenuowebbefore adoptionc002e08
line 21why, one step back21convert from reST to MySTdenuowebbefore adoption29eb946
lines 22–23why, one step back22It can output both text and binary data. It will emit a trailing newline by default, which needs to be suppressed by23passing `nl=False`:change extension to .mddenuowebbefore adoptionc002e08
line 24why, one step back24convert from reST to MySTdenuowebbefore adoption29eb946
lines 25–27why, one step back25```python26click.echo(b'\xe2\x98\x83', nl=False)27```change extension to .mddenuowebbefore adoptionc002e08
line 28why, one step back28add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e
line 29why, one step back29Last but not least {func}`echo` uses click's intelligent internal output streams to stdout and stderr which supportconvert from reST to MySTdenuowebbefore adoption29eb946
lines 30–31why, one step back30unicode output on the Windows console. This means for as long as you are using `click.echo` you can output unicode31characters (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 back32convert from reST to MySTdenuowebbefore adoption29eb946
lines 33–34why, one step back33```{versionadded} 6.034```change extension to .mddenuowebbefore adoptionc002e08
line 35why, one step back35convert from reST to MySTdenuowebbefore adoption29eb946
lines 36–37why, one step back36Click emulates output streams on Windows to support unicode to the Windows console through separate APIs. For more37information see {doc}`wincmd`.change extension to .mddenuowebbefore adoptionc002e08
line 38why, one step back38convert from reST to MySTdenuowebbefore adoption29eb946
lines 39–40why, one step back39```{versionadded} 3.040```change extension to .mddenuowebbefore adoptionc002e08
line 41why, one step back41convert from reST to MySTdenuowebbefore adoption29eb946
line 42why, one step back42You can also easily print to standard error by passing `err=True`:change extension to .mddenuowebbefore adoptionc002e08
line 43why, one step back43convert from reST to MySTdenuowebbefore adoption29eb946
lines 44–46why, one step back44```python45click.echo('Hello World!', err=True)46```change extension to .mddenuowebbefore adoptionc002e08
line 47why, one step back47convert from reST to MySTdenuowebbefore adoption29eb946
line 48why, one step back48(ansi-colors)=change extension to .mddenuowebbefore adoptionc002e08
line 49why, one step back49convert from reST to MySTdenuowebbefore adoption29eb946
line 50why, one step back50## ANSI Colorschange extension to .mddenuowebbefore adoptionc002e08
line 51why, one step back51convert from reST to MySTdenuowebbefore adoption29eb946
lines 52–53why, one step back52```{versionadded} 2.053```change extension to .mddenuowebbefore adoptionc002e08
line 54why, one step back54move header to top ofJaron Rosenaubefore adoption8231bdf
line 55why, one step back55The {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 back5657Primarily this means that:58add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e
lines 59–60why, one step back59- 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 terminalconvert from reST to MySTdenuowebbefore adoption29eb946
line 61why, one step back61 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 back62convert from reST to MySTdenuowebbefore adoption29eb946
lines 63–64why, one step back63On Windows, Click uses colorama without calling `colorama.init()`. You can still call that in your code, but it's not64required for Click.change extension to .mddenuowebbefore adoptionc002e08
line 65why, one step back65add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e
line 66why, one step back66For styling a string, the {func}`style` function can be used:change extension to .mddenuowebbefore adoptionc002e08
line 67why, one step back67convert from reST to MySTdenuowebbefore adoption29eb946
lines 68–69why, one step back68```python69import clickchange extension to .mddenuowebbefore adoptionc002e08
line 70why, one step back70convert from reST to MySTdenuowebbefore adoption29eb946
lines 71–74why, one step back71click.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 back75add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e
line 76why, one step back76The 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 back77convert from reST to MySTdenuowebbefore adoption29eb946
lines 78–82why, one step back78```python79click.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 back83convert from reST to MySTdenuowebbefore adoption29eb946
line 84why, one step back84## Pager Supportchange extension to .mddenuowebbefore adoptionc002e08
line 85why, one step back85convert from reST to MySTdenuowebbefore adoption29eb946
line 86why, one step back86In some situations, you might want to show long texts on the terminal and let a user scroll through it. This can beadd currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e
line 87why, one step back87achieved by using the {func}`echo_via_pager` function which works similarly to the {func}`echo` function, but alwaysconvert from reST to MySTdenuowebbefore adoption29eb946
line 88why, one step back88writes to stdout and, if possible, through a pager.change extension to .mddenuowebbefore adoptionc002e08
lines 89–91why, one step back8990Example:91convert from reST to MySTdenuowebbefore adoption29eb946
lines 92–96why, one step back92```python93@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 back97convert from reST to MySTdenuowebbefore adoption29eb946
lines 98–99why, one step back98If 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 back100convert from reST to MySTdenuowebbefore adoption29eb946
lines 101–104why, one step back101```python102def _generate_output():103 for idx in range(50000):104 yield f"Line {idx}\n"change extension to .mddenuowebbefore adoptionc002e08
line 105why, one step back105convert from reST to MySTdenuowebbefore adoption29eb946
lines 106–109why, one step back106@click.command()107def less():108 click.echo_via_pager(_generate_output())109```change extension to .mddenuowebbefore adoptionc002e08
line 110why, one step back110convert from reST to MySTdenuowebbefore adoption29eb946
line 111why, one step back111## Screen Clearingchange extension to .mddenuowebbefore adoptionc002e08
line 112why, one step back112convert from reST to MySTdenuowebbefore adoption29eb946
lines 113–114why, one step back113```{versionadded} 2.0114```change extension to .mddenuowebbefore adoptionc002e08
line 115why, one step back115add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e
line 116why, one step back116To clear the terminal screen, you can use the {func}`clear` function that is provided starting with Click 2.0. It doesconvert from reST to MySTdenuowebbefore adoption29eb946
line 117why, one step back117what the name suggests: it clears the entire visible screen in a platform-agnostic way:change extension to .mddenuowebbefore adoptionc002e08
line 118why, one step back118add python label in codeblockdenuowebbefore adoptionba20b21
line 119why, one step back119```pythonconvert from reST to MySTdenuowebbefore adoption29eb946
lines 120–122why, one step back120import click121click.clear()122```change extension to .mddenuowebbefore adoptionc002e08
line 123why, one step back123convert from reST to MySTdenuowebbefore adoption29eb946
line 124why, one step back124## Getting Characters from Terminalchange extension to .mddenuowebbefore adoptionc002e08
line 125why, one step back125convert from reST to MySTdenuowebbefore adoption29eb946
lines 126–127why, one step back126```{versionadded} 2.0127```change extension to .mddenuowebbefore adoptionc002e08
line 128why, one step back128convert from reST to MySTdenuowebbefore adoption29eb946
lines 129–131why, one step back129Normally, when reading input from the terminal, you would read from standard input. However, this is buffered input and130will 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 back132add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e
line 133why, one step back133For this, Click provides the {func}`getchar` function which reads a single character from the terminal buffer andconvert from reST to MySTdenuowebbefore adoption29eb946
line 134why, one step back134returns it as a Unicode character.change extension to .mddenuowebbefore adoptionc002e08
line 135why, one step back135convert from reST to MySTdenuowebbefore adoption29eb946
line 136why, one step back136Note 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 back137convert from reST to MySTdenuowebbefore adoption29eb946
line 138why, one step back138Example:change extension to .mddenuowebbefore adoptionc002e08
line 139why, one step back139convert from reST to MySTdenuowebbefore adoption29eb946
lines 140–141why, one step back140```python141import clickchange extension to .mddenuowebbefore adoptionc002e08
line 142why, one step back142convert from reST to MySTdenuowebbefore adoption29eb946
lines 143–152why, one step back143click.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 back153convert from reST to MySTdenuowebbefore adoption29eb946
lines 154–156why, one step back154Note that this reads raw input, which means that things like arrow keys will show up in the platform's native escape155format. 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 thatchange extension to .mddenuowebbefore adoptionc002e08
lines 157–158why, one step back157cannot be properly exited.158convert from reST to MySTdenuowebbefore adoption29eb946
line 159why, one step back159## Waiting for Key Presschange extension to .mddenuowebbefore adoptionc002e08
line 160why, one step back160convert from reST to MySTdenuowebbefore adoption29eb946
lines 161–162why, one step back161```{versionadded} 2.0162```change extension to .mddenuowebbefore adoptionc002e08
line 163why, one step back163convert from reST to MySTdenuowebbefore adoption29eb946
lines 164–165why, one step back164Sometimes, it's useful to pause until the user presses any key on the keyboard. This is especially useful on Windows165where `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 back166add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e
line 167why, one step back167In click, this can be accomplished with the {func}`pause` function. This function will print a quick message to theconvert from reST to MySTdenuowebbefore adoption29eb946
lines 168–169why, one step back168terminal (which can be customized) and wait for the user to press a key. In addition to that, it will also become a NOP169(no operation instruction) if the script is not run interactively.change extension to .mddenuowebbefore adoptionc002e08
line 170why, one step back170convert from reST to MySTdenuowebbefore adoption29eb946
line 171why, one step back171Example:change extension to .mddenuowebbefore adoptionc002e08
line 172why, one step back172convert from reST to MySTdenuowebbefore adoption29eb946
lines 173–176why, one step back173```python174import click175click.pause()176```change extension to .mddenuowebbefore adoptionc002e08
line 177why, one step back177convert from reST to MySTdenuowebbefore adoption29eb946
line 178why, one step back178## Launching Editorschange extension to .mddenuowebbefore adoptionc002e08
line 179why, one step back179convert from reST to MySTdenuowebbefore adoption29eb946
lines 180–181why, one step back180```{versionadded} 2.0181```change extension to .mddenuowebbefore adoptionc002e08
line 182why, one step back182add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e
line 183why, one step back183Click supports launching editors automatically through {func}`edit`. This is very useful for asking users for multi-lineconvert from reST to MySTdenuowebbefore adoption29eb946
lines 184–185why, one step back184input. It will automatically open the user's defined editor or fall back to a sensible default. If the user closes the185editor without saving, the return value will be `None`, otherwise the entered text.change extension to .mddenuowebbefore adoptionc002e08
line 186why, one step back186convert from reST to MySTdenuowebbefore adoption29eb946
line 187why, one step back187Example usage:change extension to .mddenuowebbefore adoptionc002e08
line 188why, one step back188convert from reST to MySTdenuowebbefore adoption29eb946
lines 189–190why, one step back189```python190import clickchange extension to .mddenuowebbefore adoptionc002e08
line 191why, one step back191convert from reST to MySTdenuowebbefore adoption29eb946
lines 192–197why, one step back192def 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 back198convert from reST to MySTdenuowebbefore adoption29eb946
lines 199–200why, one step back199Alternatively, the function can also be used to launch editors for files by a specific filename. In this case, the200return value is always `None`.change extension to .mddenuowebbefore adoptionc002e08
line 201why, one step back201convert from reST to MySTdenuowebbefore adoption29eb946
line 202why, one step back202Example usage:change extension to .mddenuowebbefore adoptionc002e08
line 203why, one step back203convert from reST to MySTdenuowebbefore adoption29eb946
lines 204–207why, one step back204```python205import click206click.edit(filename='/etc/passwd')207```change extension to .mddenuowebbefore adoptionc002e08
line 208why, one step back208convert from reST to MySTdenuowebbefore adoption29eb946
line 209why, one step back209## Launching Applicationschange extension to .mddenuowebbefore adoptionc002e08
line 210why, one step back210convert from reST to MySTdenuowebbefore adoption29eb946
lines 211–212why, one step back211```{versionadded} 2.0212```change extension to .mddenuowebbefore adoptionc002e08
line 213why, one step back213add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e
line 214why, one step back214Click supports launching applications through {func}`launch`. This can be used to open the default applicationconvert from reST to MySTdenuowebbefore adoption29eb946
lines 215–216why, one step back215associated with a URL or filetype. This can be used to launch web browsers or picture viewers, for instance. In addition216to this, it can also launch the file manager and automatically select the provided file.change extension to .mddenuowebbefore adoptionc002e08
line 217why, one step back217convert from reST to MySTdenuowebbefore adoption29eb946
line 218why, one step back218Example usage:change extension to .mddenuowebbefore adoptionc002e08
line 219why, one step back219convert from reST to MySTdenuowebbefore adoption29eb946
lines 220–223why, one step back220```python221click.launch("https://click.palletsprojects.com/")222click.launch("/my/downloaded/file.txt", locate=True)223```change extension to .mddenuowebbefore adoptionc002e08
line 224why, one step back224convert from reST to MySTdenuowebbefore adoption29eb946
line 225why, one step back225## Printing Filenameschange extension to .mddenuowebbefore adoptionc002e08
line 226why, one step back226convert from reST to MySTdenuowebbefore adoption29eb946
line 227why, one step back227Because filenames might not be Unicode, formatting them can be a bit tricky.change extension to .mddenuowebbefore adoptionc002e08
line 228why, one step back228add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e
line 229why, one step back229The way this works with click is through the {func}`format_filename` function. It does a best-effort conversion of theconvert from reST to MySTdenuowebbefore adoption29eb946
lines 230–231why, one step back230filename to Unicode and will never fail. This makes it possible to use these filenames in the context of a full Unicode231string.change extension to .mddenuowebbefore adoptionc002e08
line 232why, one step back232convert from reST to MySTdenuowebbefore adoption29eb946
line 233why, one step back233Example:change extension to .mddenuowebbefore adoptionc002e08
line 234why, one step back234convert from reST to MySTdenuowebbefore adoption29eb946
lines 235–237why, one step back235```python236click.echo(f"Path: {click.format_filename(b'foo.txt')}")237```change extension to .mddenuowebbefore adoptionc002e08
line 238why, one step back238convert from reST to MySTdenuowebbefore adoption29eb946
line 239why, one step back239## Standard Streamschange extension to .mddenuowebbefore adoptionc002e08
line 240why, one step back240convert from reST to MySTdenuowebbefore adoption29eb946
lines 241–243why, one step back241For command line utilities, it's very important to get access to input and output streams reliably. Python generally242provides 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 back244add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e
line 245why, one step back245Because of this, click provides the {func}`get_binary_stream` and {func}`get_text_stream` functions, which produceconvert from reST to MySTdenuowebbefore adoption29eb946
line 246why, one step back246consistent results with different Python versions and for a wide variety of terminal configurations.change extension to .mddenuowebbefore adoptionc002e08
line 247why, one step back247convert from reST to MySTdenuowebbefore adoption29eb946
lines 248–249why, one step back248The end result is that these functions will always return a functional stream object (except in very odd cases; see249{doc}`/unicode-support`).change extension to .mddenuowebbefore adoptionc002e08
line 250why, one step back250convert from reST to MySTdenuowebbefore adoption29eb946
line 251why, one step back251Example:change extension to .mddenuowebbefore adoptionc002e08
line 252why, one step back252convert from reST to MySTdenuowebbefore adoption29eb946
lines 253–254why, one step back253```python254import clickchange extension to .mddenuowebbefore adoptionc002e08
line 255why, one step back255convert from reST to MySTdenuowebbefore adoption29eb946
lines 256–258why, one step back256stdin_text = click.get_text_stream('stdin')257stdout_binary = click.get_binary_stream('stdout')258```change extension to .mddenuowebbefore adoptionc002e08
line 259why, one step back259convert from reST to MySTdenuowebbefore adoption29eb946
lines 260–261why, one step back260```{versionadded} 6.0261```change extension to .mddenuowebbefore adoptionc002e08
line 262why, one step back262convert from reST to MySTdenuowebbefore adoption29eb946
lines 263–264why, one step back263Click now emulates output streams on Windows to support unicode to the Windows console through separate APIs. For more264information see {doc}`wincmd`.change extension to .mddenuowebbefore adoptionc002e08
line 265why, one step back265convert from reST to MySTdenuowebbefore adoption29eb946
line 266why, one step back266## Intelligent File Openingchange extension to .mddenuowebbefore adoptionc002e08
line 267why, one step back267convert from reST to MySTdenuowebbefore adoption29eb946
lines 268–269why, one step back268```{versionadded} 3.0269```change extension to .mddenuowebbefore adoptionc002e08
line 270why, one step back270add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e
line 271why, one step back271Starting 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 back272function. It can intelligently open stdin/stdout as well as any other file.change extension to .mddenuowebbefore adoptionc002e08
line 273why, one step back273convert from reST to MySTdenuowebbefore adoption29eb946
line 274why, one step back274Example:change extension to .mddenuowebbefore adoptionc002e08
line 275why, one step back275convert from reST to MySTdenuowebbefore adoption29eb946
lines 276–277why, one step back276```python277import clickchange extension to .mddenuowebbefore adoptionc002e08
line 278why, one step back278convert from reST to MySTdenuowebbefore adoption29eb946
lines 279–281why, one step back279stdout = click.open_file('-', 'w')280test_file = click.open_file('test.txt', 'w')281```change extension to .mddenuowebbefore adoptionc002e08
line 282why, one step back282convert from reST to MySTdenuowebbefore adoption29eb946
lines 283–284why, one step back283If stdin or stdout are returned, the return value is wrapped in a special file where the context manager will prevent284the 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 back285convert from reST to MySTdenuowebbefore adoption29eb946
lines 286–289why, one step back286```python287with click.open_file(filename, 'w') as f:288 f.write('Hello World!\n')289```change extension to .mddenuowebbefore adoptionc002e08
line 290why, one step back290convert from reST to MySTdenuowebbefore adoption29eb946
line 291why, one step back291## Finding Application Folderschange extension to .mddenuowebbefore adoptionc002e08
line 292why, one step back292convert from reST to MySTdenuowebbefore adoption29eb946
lines 293–294why, one step back293```{versionadded} 2.0294```change extension to .mddenuowebbefore adoptionc002e08
line 295why, one step back295convert from reST to MySTdenuowebbefore adoption29eb946
lines 296–297why, one step back296Very often, you want to open a configuration file that belongs to your application. However, different operating systems297store these configuration files in different locations depending on their standards. Click provides aadd currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e
line 298why, one step back298{func}`get_app_dir` function which returns the most appropriate location for per-user config files for your applicationconvert from reST to MySTdenuowebbefore adoption29eb946
line 299why, one step back299depending on the OS.change extension to .mddenuowebbefore adoptionc002e08
line 300why, one step back300convert from reST to MySTdenuowebbefore adoption29eb946
line 301why, one step back301Example usage:change extension to .mddenuowebbefore adoptionc002e08
line 302why, one step back302convert from reST to MySTdenuowebbefore adoption29eb946
lines 303–306why, one step back303```python304import os305import click306import ConfigParserchange extension to .mddenuowebbefore adoptionc002e08
line 307why, one step back307convert from reST to MySTdenuowebbefore adoption29eb946
lines 308–318why, one step back308APP_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 back319convert from reST to MySTdenuowebbefore adoption29eb946
line 320why, one step back320## Showing Progress Barschange extension to .mddenuowebbefore adoptionc002e08
line 321why, one step back321convert from reST to MySTdenuowebbefore adoption29eb946
lines 322–323why, one step back322Sometimes, you have command line scripts that need to process a lot of data, but you want to quickly show the user some323progress about how long that will take. Click supports simple progress bar rendering for that through theadd currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e
line 324why, one step back324{func}`progressbar` function.change extension to .mddenuowebbefore adoptionc002e08
line 325why, one step back325move header to top ofJaron Rosenaubefore adoption8231bdf
line 326why, one step back326```{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 back327```change extension to .mddenuowebbefore adoptionc002e08
line 328why, one step back328convert from reST to MySTdenuowebbefore adoption29eb946
lines 329–330why, one step back329The basic usage is very simple: the idea is that you have an iterable that you want to operate on. For each item in the330iterable 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 back331convert from reST to MySTdenuowebbefore adoption29eb946
lines 332–335why, one step back332```python333for user in all_the_users_to_process:334 modify_the_user(user)335```change extension to .mddenuowebbefore adoptionc002e08
line 336why, one step back336convert from reST to MySTdenuowebbefore adoption29eb946
line 337why, one step back337To 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 back338convert from reST to MySTdenuowebbefore adoption29eb946
lines 339–340why, one step back339```python340import clickchange extension to .mddenuowebbefore adoptionc002e08
line 341why, one step back341convert from reST to MySTdenuowebbefore adoption29eb946
lines 342–345why, one step back342with 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 back346convert from reST to MySTdenuowebbefore adoption29eb946
lines 347–349why, one step back347Click will then automatically print a progress bar to the terminal and calculate the remaining time for you. The348calculation 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 back350convert from reST to MySTdenuowebbefore adoption29eb946
lines 351–356why, one step back351```python352with 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 back357add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e
line 358why, one step back358Note that {func}`progressbar` updates the bar *after* each iteration of the loop. So code like this will renderconvert from reST to MySTdenuowebbefore adoption29eb946
line 359why, one step back359correctly:change extension to .mddenuowebbefore adoptionc002e08
line 360why, one step back360convert from reST to MySTdenuowebbefore adoption29eb946
lines 361–362why, one step back361```python362import timechange extension to .mddenuowebbefore adoptionc002e08
line 363why, one step back363convert from reST to MySTdenuowebbefore adoption29eb946
lines 364–368why, one step back364with 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 back369convert from reST to MySTdenuowebbefore adoption29eb946
line 370why, one step back370Another 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 back371convert from reST to MySTdenuowebbefore adoption29eb946
lines 372–376why, one step back372```python373with 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 back377 modify_the_user(user)convert from reST to MySTdenuowebbefore adoption29eb946
line 378why, one step back378```change extension to .mddenuowebbefore adoptionc002e08
line 379why, one step back379convert from reST to MySTdenuowebbefore adoption29eb946
lines 380–382why, one step back380Sometimes, one may need to iterate over an external iterator, and advance the progress bar irregularly. To do so, you381need 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 back383convert from reST to MySTdenuowebbefore adoption29eb946
lines 384–390why, one step back384```python385with 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```