nsidc / earthaccess

Python Library for NASA Earthdata APIs
https://earthaccess.readthedocs.io/
MIT License
403 stars 80 forks source link

Allow users to disable progress bars #612

Open simonhatcher opened 3 months ago

simonhatcher commented 3 months ago

Please add an optional argument to earthaccess.open to disable PQDM progress bars. The progress bars clog up my Jupyter notebook output very badly.

jhkennedy commented 3 months ago

@simonhatcher I agree it's a good idea to provide this control.

For now, since pqdm wraps tqdm, I believe you can set this environment variable to disable the progress bars. You can do this in a shell beforehand:

export TQDM_DISABLE=1

Or in Python ahead of any TQDM imports:

import os

os.environ["TQDM_DISABLE"] = "1"

The environment variable requires tqdm v4.66.0+, but you can also hack it for older versions ahead of any TQDM imports like:

from tqdm import tqdm
from functools import partialmethod

tqdm.__init__ = partialmethod(tqdm.__init__, disable=True)
simonhatcher commented 3 months ago

@jhkennedy Thank you! That does work, but the one challenge is that I'm using TQDM elsewhere and I don't want to globally disable it in the long run.

jhkennedy commented 3 months ago

That does work, but the one challenge is that I'm using TQDM elsewhere and I don't want to globally disable it in the long run.

yes, definitely exactly why we should provide a way to disable it for Earthaccess.

:thinking: I think you could write a context manager that would disable it temporarily... but that's getting to be a lot. Something like (I'm sure there are much cleaner ways to do this):

from tqdm import tqdm
from functools import partialmethod

class DisableTqdm(object):
    def __init__(self):
        self._tqdm_init = tqdm.__init__

    def __enter__(self):
        tqdm.__init__ = partialmethod(tqdm.__init__, disable=True)

    def __exit__(self, type, value, traceback):
        tqdm.__init__ = self._tqdm_init

with DisableTqdm():
    # Do stuff
    ...