module documentation

Undocumented

Constant V Undocumented
Function ​_build​_prompt Undocumented
Function ​_format​_default Undocumented
Function ​_interpret​_color Undocumented
Function clear Clears the terminal screen. This will have the effect of clearing the whole visible space of the terminal and moving the cursor to the top left. This does not do anything if not connected to a terminal.
Function confirm Prompts for confirmation (yes/no question).
Function echo​_via​_pager This function takes a text and shows it via an environment specific pager on stdout.
Function edit No summary
Function get​_terminal​_size Returns the current size of the terminal as tuple in the form (width, height) in columns and rows.
Function getchar No summary
Function hidden​_prompt​_func Undocumented
Function launch No summary
Function pause No summary
Function progressbar No summary
Function prompt Prompts a user for input. This is a convenience function that can be used to prompt a user for input later.
Function raw​_terminal Undocumented
Function secho This function combines echo and style into one call. As such the following two calls are the same:
Function style No summary
Function unstyle Removes ANSI styling information from a string. Usually it's not necessary to use this function as Click's echo function will automatically remove styling if necessary.
Variable ​_ansi​_colors Undocumented
Variable ​_ansi​_reset​_all Undocumented
Variable ​_getchar Undocumented
V =

Undocumented

Value
t.TypeVar('V')
def _build_prompt(text, suffix, show_default=False, default=None, show_choices=True, type=None):

Undocumented

Parameters
text:strUndocumented
suffix:strUndocumented
show​_default:boolUndocumented
default:t.Optional[t.Any]Undocumented
show​_choices:boolUndocumented
type:t.Optional[ParamType]Undocumented
Returns
strUndocumented
def _format_default(default):

Undocumented

Parameters
default:t.AnyUndocumented
Returns
t.AnyUndocumented
def _interpret_color(color, offset=0):

Undocumented

Parameters
color:t.Union[int, t.Tuple[int, int, int], str]Undocumented
offset:intUndocumented
Returns
strUndocumented
def clear():

Clears the terminal screen. This will have the effect of clearing the whole visible space of the terminal and moving the cursor to the top left. This does not do anything if not connected to a terminal.

New in version 2.0.
def confirm(text, default=False, abort=False, prompt_suffix=': ', show_default=True, err=False):

Prompts for confirmation (yes/no question).

If the user aborts the input by sending a interrupt signal this function will catch it and raise a Abort exception.

Changed in version 8.0: Repeat until input is given if default is None.
New in version 4.0: Added the err parameter.
Parameters
text:strthe question to ask.
default:t.Optional[bool]The default value to use when no input is given. If None, repeat until input is given.
abort:boolif this is set to True a negative answer aborts the exception by raising Abort.
prompt​_suffix:stra suffix that should be added to the prompt.
show​_default:boolshows or hides the default value in the prompt.
err:boolif set to true the file defaults to stderr instead of stdout, the same as with echo.
Returns
boolUndocumented
def echo_via_pager(text_or_generator, color=None):

This function takes a text and shows it via an environment specific pager on stdout.

Changed in version 3.0: Added the color flag.
Parameters
text​_or​_generator:t.Union[t.Iterable[str], t.Callable[[], t.Iterable[str]], str]the text to page, or alternatively, a generator emitting the text to page.
color:t.Optional[bool]controls if the pager supports ANSI colors or not. The default is autodetection.
def edit(text=None, editor=None, env=None, require_save=True, extension='.txt', filename=None):

Edits the given text in the defined editor. If an editor is given (should be the full path to the executable but the regular operating system search path is used for finding the executable) it overrides the detected editor. Optionally, some environment variables can be used. If the editor is closed without changes, None is returned. In case a file is edited directly the return value is always None and require_save and extension are ignored.

If the editor cannot be opened a UsageError is raised.

Note for Windows: to simplify cross-platform usage, the newlines are automatically converted from POSIX to Windows and vice versa. As such, the message here will have \n as newline markers.

Parameters
text:t.Optional[t.AnyStr]the text to edit.
editor:t.Optional[str]optionally the editor to use. Defaults to automatic detection.
env:t.Optional[t.Mapping[str, str]]environment variables to forward to the editor.
require​_save:boolif this is true, then not saving in the editor will make the return value become None.
extension:strthe extension to tell the editor about. This defaults to .txt but changing this might change syntax highlighting.
filename:t.Optional[str]if provided it will edit this file instead of the provided text contents. It will not use a temporary file as an indirection in that case.
Returns
t.Optional[t.AnyStr]Undocumented
def get_terminal_size():

Returns the current size of the terminal as tuple in the form (width, height) in columns and rows.

Deprecated since version 8.0: Will be removed in Click 8.1. Use shutil.get_terminal_size instead.
Returns
os.terminal_sizeUndocumented
def getchar(echo=False):

Fetches a single character from the terminal and returns it. This will always return a unicode character and under certain rare circumstances this might return more than one character. The situations which more than one character is returned is when for whatever reason multiple characters end up in the terminal buffer or standard input was not actually a terminal.

Note that this will always read from the terminal, even if something is piped into the standard input.

Note for Windows: in rare cases when typing non-ASCII characters, this function might wait for a second character and then return both at once. This is because certain Unicode characters look like special-key markers.

New in version 2.0.
Parameters
echo:boolif set to True, the character read will also show up on the terminal. The default is to not show it.
Returns
strUndocumented
def hidden_prompt_func(prompt):

Undocumented

Parameters
prompt:strUndocumented
Returns
strUndocumented
def launch(url, wait=False, locate=False):

This function launches the given URL (or filename) in the default viewer application for this file type. If this is an executable, it might launch the executable in a new session. The return value is the exit code of the launched application. Usually, 0 indicates success.

Examples:

click.launch('https://click.palletsprojects.com/')
click.launch('/my/downloaded/file', locate=True)
New in version 2.0.
Parameters
url:strURL or filename of the thing to launch.
wait:boolWait for the program to exit before returning. This only works if the launched program blocks. In particular, xdg-open on Linux does not block.
locate:boolif this is set to True then instead of launching the application associated with the URL it will attempt to launch a file manager with the file located. This might have weird effects if the URL does not point to the filesystem.
Returns
intUndocumented
def pause(info=None, err=False):

This command stops execution and waits for the user to press any key to continue. This is similar to the Windows batch "pause" command. If the program is not run through a terminal, this command will instead do nothing.

New in version 2.0.
New in version 4.0: Added the err parameter.
Parameters
info:t.Optional[str]The message to print before pausing. Defaults to "Press any key to continue...".
err:boolif set to message goes to stderr instead of stdout, the same as with echo.
def progressbar(iterable=None, length=None, label=None, show_eta=True, show_percent=None, show_pos=False, item_show_func=None, fill_char='#', empty_char='-', bar_template='%(label)s [%(bar)s] %(info)s', info_sep=' ', width=36, file=None, color=None, update_min_steps=1):

This function creates an iterable context manager that can be used to iterate over something while showing a progress bar. It will either iterate over the iterable or length items (that are counted up). While iteration happens, this function will print a rendered progress bar to the given file (defaults to stdout) and will attempt to calculate remaining time and more. By default, this progress bar will not be rendered if the file is not a terminal.

The context manager creates the progress bar. When the context manager is entered the progress bar is already created. With every iteration over the progress bar, the iterable passed to the bar is advanced and the bar is updated. When the context manager exits, a newline is printed and the progress bar is finalized on screen.

Note: The progress bar is currently designed for use cases where the total progress can be expected to take at least several seconds. Because of this, the ProgressBar class object won't display progress that is considered too fast, and progress where the time between steps is less than a second.

No printing must happen or the progress bar will be unintentionally destroyed.

Example usage:

with progressbar(items) as bar:
    for item in bar:
        do_something_with(item)

Alternatively, if no iterable is specified, one can manually update the progress bar through the update() method instead of directly iterating over the progress bar. The update method accepts the number of steps to increment the bar with:

with progressbar(length=chunks.total_bytes) as bar:
    for chunk in chunks:
        process_chunk(chunk)
        bar.update(chunks.bytes)

The update() method also takes an optional value specifying the current_item at the new position. This is useful when used together with item_show_func to customize the output for each manual step:

with click.progressbar(
    length=total_size,
    label='Unzipping archive',
    item_show_func=lambda a: a.filename
) as bar:
    for archive in zip_file:
        archive.extract()
        bar.update(archive.size, archive)
Changed in version 8.0: Output is shown even if execution time is less than 0.5 seconds.
Changed in version 8.0: item_show_func shows the current item, not the previous one.
Changed in version 8.0: Labels are echoed if the output is not a TTY. Reverts a change in 7.0 that removed all output.
New in version 8.0: Added the update_min_steps parameter.
Changed in version 4.0: Added the color parameter. Added the update method to the object.
New in version 2.0.
Parameters
iterable:t.Optional[t.Iterable[V]]an iterable to iterate over. If not provided the length is required.
length:t.Optional[int]the number of items to iterate over. By default the progressbar will attempt to ask the iterator about its length, which might or might not work. If an iterable is also provided this parameter can be used to override the length. If an iterable is not provided the progress bar will iterate over a range of that length.
label:t.Optional[str]the label to show next to the progress bar.
show​_eta:boolenables or disables the estimated time display. This is automatically disabled if the length cannot be determined.
show​_percent:t.Optional[bool]enables or disables the percentage display. The default is True if the iterable has a length or False if not.
show​_pos:boolenables or disables the absolute position display. The default is False.
item​_show​_func:t.Optional[t.Callable[[t.Optional[V]], t.Optional[str]]]A function called with the current item which can return a string to show next to the progress bar. If the function returns None nothing is shown. The current item can be None, such as when entering and exiting the bar.
fill​_char:strthe character to use to show the filled part of the progress bar.
empty​_char:strthe character to use to show the non-filled part of the progress bar.
bar​_template:strthe format string to use as template for the bar. The parameters in it are label for the label, bar for the progress bar and info for the info section.
info​_sep:strthe separator between multiple info items (eta etc.)
width:intthe width of the progress bar in characters, 0 means full terminal width
file:t.Optional[t.TextIO]The file to write to. If this is not a terminal then only the label is printed.
color:t.Optional[bool]controls if the terminal supports ANSI colors or not. The default is autodetection. This is only needed if ANSI codes are included anywhere in the progress bar output which is not the case by default.
update​_min​_steps:intRender only when this many updates have completed. This allows tuning for very fast iterators.
Returns
ProgressBar[V]Undocumented
def prompt(text, default=None, hide_input=False, confirmation_prompt=False, type=None, value_proc=None, prompt_suffix=': ', show_default=True, err=False, show_choices=True):

Prompts a user for input. This is a convenience function that can be used to prompt a user for input later.

If the user aborts the input by sending a interrupt signal, this function will catch it and raise a Abort exception.

New in version 8.0: confirmation_prompt can be a custom string.
New in version 7.0: Added the show_choices parameter.
New in version 6.0: Added unicode support for cmd.exe on Windows.
New in version 4.0: Added the err parameter.
Parameters
text:strthe text to show for the prompt.
default:t.Optional[t.Any]the default value to use if no input happens. If this is not given it will prompt until it's aborted.
hide​_input:boolif this is set to true then the input value will be hidden.
confirmation​_prompt:t.Union[bool, str]Prompt a second time to confirm the value. Can be set to a string instead of True to customize the message.
type:t.Optional[t.Union[ParamType, t.Any]]the type to use to check the value against.
value​_proc:t.Optional[t.Callable[[str], t.Any]]if this parameter is provided it's a function that is invoked instead of the type conversion to convert a value.
prompt​_suffix:stra suffix that should be added to the prompt.
show​_default:boolshows or hides the default value in the prompt.
err:boolif set to true the file defaults to stderr instead of stdout, the same as with echo.
show​_choices:boolShow or hide choices if the passed type is a Choice. For example if type is a Choice of either day or week, show_choices is true and text is "Group by" then the prompt will be "Group by (day, week): ".
Returns
t.AnyUndocumented
def raw_terminal():

Undocumented

Returns
t.ContextManager[int]Undocumented
def secho(message=None, file=None, nl=True, err=False, color=None, **styles):

This function combines echo and style into one call. As such the following two calls are the same:

click.secho('Hello World!', fg='green')
click.echo(click.style('Hello World!', fg='green'))

All keyword arguments are forwarded to the underlying functions depending on which one they go with.

Non-string types will be converted to str. However, bytes are passed directly to echo without applying style. If you want to style bytes that represent text, call bytes.decode first.

Changed in version 8.0: A non-string message is converted to a string. Bytes are passed through without style applied.
New in version 2.0.
Parameters
message:t.Optional[t.Any]Undocumented
file:t.Optional[t.IO]Undocumented
nl:boolUndocumented
err:boolUndocumented
color:t.Optional[bool]Undocumented
**styles:t.AnyUndocumented
def style(text, fg=None, bg=None, bold=None, dim=None, underline=None, overline=None, italic=None, blink=None, reverse=None, strikethrough=None, reset=True):

Styles a text with ANSI styles and returns the new string. By default the styling is self contained which means that at the end of the string a reset code is issued. This can be prevented by passing reset=False.

Examples:

click.echo(click.style('Hello World!', fg='green'))
click.echo(click.style('ATTENTION!', blink=True))
click.echo(click.style('Some things', reverse=True, fg='cyan'))
click.echo(click.style('More colors', fg=(255, 12, 128), bg=117))

Supported color names:

  • black (might be a gray)
  • red
  • green
  • yellow (might be an orange)
  • blue
  • magenta
  • cyan
  • white (might be light gray)
  • bright_black
  • bright_red
  • bright_green
  • bright_yellow
  • bright_blue
  • bright_magenta
  • bright_cyan
  • bright_white
  • reset (reset the color code only)

If the terminal supports it, color may also be specified as:

  • An integer in the interval [0, 255]. The terminal must support 8-bit/256-color mode.
  • An RGB tuple of three integers in [0, 255]. The terminal must support 24-bit/true-color mode.

See https://en.wikipedia.org/wiki/ANSI_color and https://gist.github.com/XVilka/8346728 for more information.

Changed in version 8.0: A non-string message is converted to a string.
Changed in version 8.0: Added support for 256 and RGB color codes.
Changed in version 8.0: Added the strikethrough, italic, and overline parameters.
Changed in version 7.0: Added support for bright colors.
New in version 2.0.
Parameters
text:t.Anythe string to style with ansi codes.
fg:t.Optional[t.Union[int, t.Tuple[int, int, int], str]]if provided this will become the foreground color.
bg:t.Optional[t.Union[int, t.Tuple[int, int, int], str]]if provided this will become the background color.
bold:t.Optional[bool]if provided this will enable or disable bold mode.
dim:t.Optional[bool]if provided this will enable or disable dim mode. This is badly supported.
underline:t.Optional[bool]if provided this will enable or disable underline.
overline:t.Optional[bool]if provided this will enable or disable overline.
italic:t.Optional[bool]if provided this will enable or disable italic.
blink:t.Optional[bool]if provided this will enable or disable blinking.
reverse:t.Optional[bool]if provided this will enable or disable inverse rendering (foreground becomes background and the other way round).
strikethrough:t.Optional[bool]if provided this will enable or disable striking through text.
reset:boolby default a reset-all code is added at the end of the string which means that styles do not carry over. This can be disabled to compose styles.
Returns
strUndocumented
def unstyle(text):

Removes ANSI styling information from a string. Usually it's not necessary to use this function as Click's echo function will automatically remove styling if necessary.

New in version 2.0.
Parameters
text:strthe text to remove style information from.
Returns
strUndocumented
_ansi_colors: dict[str, int] =

Undocumented

_ansi_reset_all: str =

Undocumented

_getchar: t.Optional[t.Callable[[bool], str]] =

Undocumented