Skip to content
Myr

Documented new functions in utils

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 12f50b4 · Browse files · 1 file+76 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+76 added−0 removed

  • other:+76 added−0 removed

1 file+76 added−0 removed

modified:docs/utils.rst+76 added
70 unmodified lines
71 import click7273 stdin_text = click.get_text_stream('stdin')74 stdout_binary = click.get_binary_stream('stdout')757677Finding Application Folders78---------------------------7980.. versionadded:: 2.08182Very often you want to open a config file that belongs to your83application. However different operationg systems store these config84files by their policy in different places. Click provides a85:func:`get_app_dir` function which returns the most appropriate location86for per user config files for your application depending on the OS.8788Example usage::8990 import os91 import click92 import ConfigParser9394 APP_NAME = 'My Application'9596 def read_config():97 cfg = os.path.join(click.get_app_dir(APP_NAME), 'config.ini')98 parser = ConfigParser.RawConfigParser()99 parser.read([cfg])100 rv = {}101 for section in parser.sections():102 for key, value in parser.items(section):103 rv['%s.%s' % (section, key)] = value104 return rv105106107Showing Progress Bars108---------------------109110.. versionadded:: 2.0111112Sometimes you have command line scripts that need to process a lot of data113but you want to quickly show the user some progress about how long that114will take. Click supports simple progress bar rendering for that through115the :func:`progressbar` function.116117The basic usage is very simple: the idea is that you have an iterable that118you want to operate on. For each item in the iterable it might take some119time to do processing. So say you have a loop like this::120121 for user in all_the_users_to_process:122 modify_the_user(user)123124To hook this up with an automatically updating progress bar all you need125to do is to change the code to this::126127 import click128129 with click.progressbar(all_the_users_to_process) as bar:130 for user in bar:131 modify_the_user(user)132133Click will then automatically print a progress bar to the terminal and134calculate the remaining time for you. The calculation of remaining time135requires that the iterable has a length. If it does not have a length,136but you know the length, you can explicitly provide it::137138 with click.progressbar(all_the_users_to_process,139 length=number_of_users) as bar:140 for user in bar:141 modify_the_user(user)142143Another useful feature is to associate a label with the progress bar which144will be shown before next to the progress bar::145146 with click.progressbar(all_the_users_to_process,147 label='Modifying user accounts',148 length=number_of_users) as bar:149 for user in bar:150 modify_the_user(user)