Skip to content
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

Turn off tqdm from application code #619

Closed
3 tasks done
stellarhoof opened this issue Sep 27, 2018 · 9 comments · Fixed by #1491
Closed
3 tasks done

Turn off tqdm from application code #619

stellarhoof opened this issue Sep 27, 2018 · 9 comments · Fixed by #1491
Assignees
Labels
duplicate 🗐 Seen it before p3-enhancement 🔥 Much new such feature question/docs ‽ Documentation clarification candidate
Projects

Comments

@stellarhoof
Copy link

stellarhoof commented Sep 27, 2018

  • I have visited the [source website], and in particular
    read the [known issues]
  • I have searched through the [issue tracker] for duplicates
  • I have mentioned version numbers, operating system and
    environment, where applicable:
4.26.0 3.6.6 (default, Aug 21 2018, 23:42:39) 
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)] darwin

I'm using a package that uses tqdm internally and I'd like to disable tqdm from within application code.

#612 would've been ideal. It was closed because the solution solved the problem for the author of the issue but not for us since we cannot modify library code. Thanks.

@casperdcl
Copy link
Sponsor Member

before importing the offending package you could do something like:

import tqdm

def nop(it, *a, **k):
    return it

tqdm.tqdm = nop

@casperdcl casperdcl self-assigned this Sep 27, 2018
@casperdcl casperdcl added p3-enhancement 🔥 Much new such feature question/docs ‽ Documentation clarification candidate labels Sep 27, 2018
@stellarhoof
Copy link
Author

stellarhoof commented Sep 27, 2018

That would work but it's not very robust since it depends on the order of imports. It'd be fine if I was doing a one-off script for example where I have absolute control of how I import things but if I monkey patch tqdm like you suggested somewhere else to be reused it's going to cause very confusing behavior. Ex:

import {helper module that monkey patches tqdm like in suggestion}
import {lib that uses tqdm}

Above would work, but if a user switches the imports it won't work

An alternative to an env var is to have a module-level variable that we can toggle from application code like

# tqdm land
enabled = True
def disable():
   global enabled
   enabled = False

# user land
import tqdm
tqdm.disable()

@stellarhoof stellarhoof changed the title Re: Turn off tqdm via environment variables Re: Turn off tqdm from application code Sep 27, 2018
@stellarhoof stellarhoof changed the title Re: Turn off tqdm from application code Turn off tqdm from application code Sep 27, 2018
@thakkarparth007
Copy link

Any updates on this? Seems very useful when I use this in Jupyter notebooks and want to sometimes disable progress bars globally.

@casperdcl
Copy link
Sponsor Member

I think the main discussion is #614

@casperdcl
Copy link
Sponsor Member

Closing this in favour of #614

@casperdcl casperdcl added the duplicate 🗐 Seen it before label Mar 10, 2020
facebook-github-bot pushed a commit to facebookresearch/beanmachine that referenced this issue Apr 9, 2020
Summary:
This allows tqdm to be turned off, by passing a flag through `infer()` to `_infer()`.

I chose this approach of passing through the hierarchy because it should fare well in the future growth of BeanMachine's UX, where the `bool` should probably be replaced with several levels/layers of `verbose`, which might be set/modified at various layers of the inheritance hierarchy.

Conversely, this design might be too clunky/bloated because it passes through several layers of wrappers. Please let me know! An alternative approach could be to use some global variable. These are viable suggestions for this approach:
tqdm/tqdm#619, tqdm/tqdm#612

Reviewed By: nazanint

Differential Revision: D20741860

fbshipit-source-id: 73fbb5fa0543cf8331f6417f175442c482db9246
@kaivalyar
Copy link

kaivalyar commented Apr 26, 2020

For those still stumbling upon this problem, perhaps this might be useful, as an improvement of @casperdcl's solutions:

import tqdm
class _TQDM(tqdm.tqdm):
    def __init__(self, *argv, **kwargs):
        kwargs['disable'] = True
        if kwargs.get('disable_override', 'def') != 'def':
            kwargs['disable'] = kwargs['disable_override']
        super().__init__(*argv, **kwargs)
tqdm.tqdm = _TQDM

This has almost the identical effect as the nop in @casperdcl's solution, and can be used in exactly the same manner. However, this has the added benefit of allowing the use of tqdm in the application code itself. So effectively the behaviour of tqdm when used by library code is to always set disable to True, but the application code can use tqdm using the new parameter disable_override.

This can also be further customised to not override the library parameter settings, but only change tqdm's default behaviour, by replacing the line kwargs['disable'] = True with kwargs['disable'] = kwargs.get('disable', True), if desired.

@avenkatraman31
Copy link

Thank you very much! This is exactly what I needed.

@ndvbd
Copy link

ndvbd commented May 4, 2023

@kaivalyar I added these lines to my code - nothing happened - tqdm still shows progress bars.

casperdcl added a commit that referenced this issue Aug 5, 2023
- fixes #370, fixes #612, fixes #619, fixes #1318
- closes #950, closes #1061
casperdcl added a commit that referenced this issue Aug 8, 2023
- fixes #370, fixes #612, fixes #619, fixes #1318
- closes #950, closes #1061
casperdcl added a commit that referenced this issue Aug 8, 2023
- fixes #370, fixes #612, fixes #619, fixes #1318
- closes #950, closes #1061
@casperdcl
Copy link
Sponsor Member

Thanks everyone for your patience! Just released tqdm==4.66.0 which includes support for env var overrides. I'm using TQDM_MININTERVAL=5 in most of my CI jobs now :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate 🗐 Seen it before p3-enhancement 🔥 Much new such feature question/docs ‽ Documentation clarification candidate
Projects
Casper
  
Done
Development

Successfully merging a pull request may close this issue.

6 participants