-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
progressbar(hide=True)
option for hiding the progress bar
#2609
Comments
Here's one workaround I've used for this, which feels pretty messy: class NullProgressBar:
def __init__(self, *args):
self.args = args
def __iter__(self):
yield from self.args[0]
def update(self, value):
pass
@contextlib.contextmanager
def progressbar(*args, **kwargs):
silent = kwargs.pop("silent")
if silent:
yield NullProgressBar(*args)
else:
with click.progressbar(*args, **kwargs) as bar:
yield bar Then: with progressbar(
length=self.count, silent=not show_progress, label="1: Evaluating"
) as bar: |
It looks like there's already a property that could be used for this: click/src/click/_termui_impl.py Line 107 in ca5e1c3
So the implementation may be as straight-forward as adding a self.is_hidden: bool = not hide and not isatty(self.file) |
I tried this: with click.progressbar(
ids, label="Calculating similarities", show_percent=True
) as bar:
if hide:
bar.is_hidden = True But it still shows the bar once at the start of the loop, because of this: click/src/click/_termui_impl.py Lines 110 to 113 in ca5e1c3
Which calls click/src/click/_termui_impl.py Lines 231 to 239 in ca5e1c3
|
I'm wary of changing our progress bar at this point. I generally recommend that people use tqdm or rich if they need features beyond what ours provides. Both of them have a way to hide a progress bar. In this case, it does seem relatively straightforward to modify our existing hide behavior with a parameter, so I think I'm ok with this. |
Co-authored-by: Andreas Backx <[email protected]>
Often when I use the Click
progressbar
I find myself wanting to conditionally hide it. Sometimes it's because there's another option in play which means I have output to display in place of it - others it's because I added a--silent
option (as seen incurl
) for disabling it entirely.It's actually a bit tricky doing this at the moment, due to its use as a context manager.
What I'd really like to be able to do is this:
The text was updated successfully, but these errors were encountered: