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

TqdmCallback: add support for custom tqdm subclasses #1497

Merged
merged 2 commits into from
Jan 29, 2024

Conversation

skshetry
Copy link
Contributor

@skshetry skshetry commented Jan 11, 2024

This will allow customizing the tqdm progressbar used for the callback, and extending (since all tqdm_kwargs is passed to the underlying implementation.)

E.g., in DVC, we have custom logic to align all the progress bars of tqdm, even if they have different description lengths.

This PR will enable us to use TqdmCallback directly in our code, without writing our own implementation.


from tqdm import tqdm

class TqdmExtraFormat(tqdm):
    """Provides a `total_time` format parameter"""
    @property
    def format_dict(self):
        d = super(TqdmExtraFormat, self).format_dict
        total_time = d["elapsed"] * (d["total"] or 0) / max(d["n"], 1)
        d.update(total_time=self.format_interval(total_time) + " in total")
        return d

for i in TqdmExtraFormat(
      range(9), ascii=" .oO0",
      bar_format="{total_time}: {percentage:.0f}%|{bar}{r_bar}"):
    if i == 4:
        break

For example, there is format_dict that allows you to define parameters which will be used in bar_format. In above example, you can show an addition info: total_time with the extended subclass.

@martindurant
Copy link
Member

Would it be appropriate to provide an example snippet of usage in the prose documentation where we already mention tqdm? Alternatively, you could include the exact class above in the code with docstrings (linked in the API page) explaining that this is a way to customise the progressbar functionality.

This will allow customizing the tqdm progressbar used for the callback,
and extending (since all `tqdm_kwargs` is passed to the underlying
implementation.)

Eg: in dvc, we have custom logic to align all the _progress_ _bars_
of tqdm, even if they have different description length.
@skshetry
Copy link
Contributor Author

I have updated the docstring with an example.

@martindurant martindurant merged commit f21699a into fsspec:master Jan 29, 2024
10 checks passed
@skshetry skshetry deleted the tqdm-callback-tqdm_cls branch January 29, 2024 15:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants