Skip to content

progressbar label should not be shown when the output is not a tty #406

Closed

Description

@pombredanne

See #401 for details.
The behaviour is not as advertised.

import click

@click.command()
def mycli():
    with click.progressbar(range(10), label='xyz') as pb:
        for _ in pb:
           pass

if __name__ == '__main__':
    mycli()

will print the label twice:

python tst.py  > that
cat that 
xyz
xyz

The docstring states: "By default, this progress bar will not be rendered if the file is not a terminal."
However, the label is always displayed even when the output is not a tty.

@untitaker replied in #401

You make this seem like a bug when it's not. This is intended behavior -- the label contains information about the work that is currently done by the launched program. Since you can't output anything else while the progressbar is running (without creating glitches), this is the only option to show any information at all, besides the progressbar itself. Your fix would make any well-behaved Click app effectively silent in non-TTY mode.

But you can effectively issue a click.echo before displaying entering in a progressbar block and issue a click.echo after that block without problems. This is a more flexible way that printing a surprising fixed label twice.

Activity

  1. untitaker commented on Aug 11, 2015

    @untitaker
    Contributor

    But you can effectively issue a click.echo before displaying entering in a progressbar block and issue a click.echo after that block without problems.

    Not if you want to change the label during the progressbar (on a related note, perhaps we should also print the item info in non-TTY mode). Also it looks ugly in TTY mode.

    That the same label is printed more than once is stupid behavior.

  2. pombredanne commented on Aug 11, 2015

    @pombredanne
    ContributorAuthor

    ah... I had no idea you could change the label while the progressbar is iterated. This is an object after all. Though when you do that, the label you update is only shown in tty mode.
    The original label overwrites this (with a dual output) in non-tty mode.
    So, I really think that a progressbar should not display as stated anything when the output is not a tty.
    Displaying something like a start string and an end string ugliness is really a matter of taste.
    And if you really want something to be shown in a non-tty output it is IMHO best to let you control that rather that let the progressbar drive it.

    FWIW, having a start and end statement is something I do here https://github.com/nexB/scancode-toolkit/blob/develop/src/scancode/utils.py#L63

  3. untitaker commented on Aug 11, 2015

    @untitaker
    Contributor

    ah... I had no idea you could change the label while the progressbar is iterated. This is an object after all. Though when you do that, the label you update is only shown in tty mode.

    The new label will get used once the iterator advances. I'm not sure if that's intended usage, but I suspect a few people already rely on this.

  4. pombredanne commented on May 24, 2017

    @pombredanne
    ContributorAuthor

    I found out that this is fixed in head.... so I am closing

    import click
    
    @click.command()
    def mycli():
        with click.progressbar(range(10), label='xyz') as pb:
            for _ in pb:
               pass
    
    if __name__ == '__main__':
        mycli()
    

    will print the label only once as excepted:

    python tst.py  > that
    cat that 
    xyz
    
  5. locked as resolved and limited conversation to collaborators on Nov 13, 2020
  6. added
    help output
    on Aug 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help output

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions