ValueError Raised When Resizing Window While Progress Bar Is Being Initially Rendered

by ADMIN 86 views

===========================================================

Describe the Bug

When resizing the window while the progress bar is being initially rendered, a ValueError is raised. This issue occurs due to the way the Enlighten library handles progress bars and window resizing.

To Reproduce

The issue can be reproduced by resizing the window while the progress bar is being rendered. The function processing_pbar is used to create the progress bar, which is shown below:

def processing_pbar(
    *, manager: Manager, total_len: int, pbar_name: str, unit: str = "Examples"
) -> ProcessingPbar:
    """Processing bar that contains counters for in progress, completed, and failed."""
    terminal = manager.term
    PBAR_FORMAT = (
        "{desc}{desc_pad}{percentage_00:3.0f}%|{bar}| "
        f"P:{terminal.mediumpurple('{count_0:{len_total}d}')} "
        f"S:{terminal.green3('{count_1:{len_total}d}')} "
        f"F:{terminal.firebrick1('{count_2:{len_total}d}')} "
        "[{elapsed}<{eta}, {rate:.2f}{unit_pad}{unit}/s]"
    )
    in_progress: Counter = manager.counter(
        total=total_len,
        desc=pbar_name,
        unit=unit,
        color="mediumpurple",
        bar_format=PBAR_FORMAT,
    )
    completed = in_progress.add_subcounter("white")
    failed = in_progress.add_subcounter("firebrick1")
    return ProcessingPbar(in_progress, completed, failed)

This issue does not occur at the very start of the script because the total_len value is obtained by downloading some datasets first.

Environment

The issue is observed in the following environment:

  • Enlighten Version: v1.14.1
  • OS and version: MacOS, 15.1.1 (24B2091)
  • Console application: iterm2
  • Special Conditions: N/A

Additional Context

The stacktrace of the error is as follows:

line 33, in processing_pbar
    in_progress: Counter = manager.counter(
  File "/usr/local/lib/python3.10/dist-packages/enlighten/_basemanager.py", line 154, in counter
    return self._add_counter(self.counter_class, position=position, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/enlighten/_basemanager.py", line 278, in _add_counter
    ctr.clear(flush=False)
  File "/usr/local/lib/python3.10/dist-packages/enlighten/_basecounter.py", line 254, in clear 
    self.manager.write(flush=flush, counter=self)
  File "/usr/local/lib/python3.10/dist-packages/enlighten/_manager.py", line 316, in write
    self._resize_handler()
  File "/usr/local/lib/python3.10/dist-packages/enlighten/_manager.py", line 150, in _resize_handler
    counter.refresh(flush=False)
  File "/usr/local/lib/python3.10/dist-packages/enlighten/_basecounter.py", line 299, in refresh
    self.manager.write(output=self.format, flush=flush, counter=self, elapsedelapsed)
  File "/usr/local/lib/python3.10/dist-packages/enlighten/_manager.py", line 327, in write
    output = output(**kwargs)
  File "/usr/local/lib/python3.10/dist-packages/enlighten/_counter.py", line 613, in format
    return self._format_bar(fields, iterations, width, elapsed, force_float)
  File "/usr/local/lib/python3.10/dist-packages/enlighten/_counter.py", line 712, in _format_bar
    raise_from_none(ValueError(self._get_format_error(e.args[0])))
  File "<string>", line 2, in raise_from_none
ValueError: Reserve field 'percentage_00' specified in format, but no subcounters are configured

Solution

The issue can be solved by removing the percentage_00 field from the PBAR_FORMAT string. This field is not necessary and is causing the error.

PBAR_FORMAT = (
    "{desc}{desc_pad}{percentage:3.0f}%|{bar}| "
    f"P:{terminal.mediumpurple('{count_0:{len_total}d}')} "
    f"S:{terminal.green3('{count_1:{len_total}d}')} "
    f"F:{terminal.firebrick1('{count_2:{len_total}d}')} "
    "[{elapsed}<{eta}, {rate:.2f}{unit_pad}{unit}/s]"
)

By removing the percentage_00 field, the progress bar will still be displayed correctly, and the error will be resolved.

Q: What is the cause of the ValueError raised when resizing the window while the progress bar is being initially rendered?

A: The cause of the ValueError is due to the way the Enlighten library handles progress bars and window resizing. Specifically, the issue occurs when the percentage_00 field is specified in the PBAR_FORMAT string, but no subcounters are configured.

Q: What is the PBAR_FORMAT string, and why is it causing the error?

A: The PBAR_FORMAT string is a format string used to display the progress bar. It contains placeholders for various fields, such as the percentage, bar, and counters. The percentage_00 field is a placeholder for the percentage value, but it is not necessary and is causing the error.

Q: How can I fix the error?

A: To fix the error, you can remove the percentage_00 field from the PBAR_FORMAT string. This will prevent the error from occurring and allow the progress bar to be displayed correctly.

Q: What are the benefits of removing the percentage_00 field?

A: By removing the percentage_00 field, you can:

  • Prevent the ValueError from occurring
  • Improve the performance of the progress bar rendering
  • Simplify the code and reduce the risk of errors

Q: Are there any other ways to fix the error?

A: Yes, there are other ways to fix the error. You can:

  • Configure the subcounters to include the percentage_00 field
  • Use a different format string that does not include the percentage_00 field
  • Upgrade to a newer version of the Enlighten library that fixes the issue

Q: How can I prevent the error from occurring in the future?

A: To prevent the error from occurring in the future, you can:

  • Always check the documentation for the Enlighten library to ensure that you are using the correct format string
  • Test your code thoroughly to catch any errors before they occur
  • Keep your code up to date with the latest version of the Enlighten library

Q: What are some best practices for working with progress bars in Enlighten?

A: Some best practices for working with progress bars in Enlighten include:

  • Always use the correct format string for the progress bar
  • Configure the subcounters to include the necessary fields
  • Test your code thoroughly to catch any errors before they occur
  • Keep your code up to date with the latest version of the Enlighten library

Q: Where can I find more information about working with progress bars in Enlighten?

A: You can find more information about working with progress bars in Enlighten by:

  • Checking the documentation for the Enlighten library
  • Searching online for tutorials and examples
  • Joining online communities and forums for Enlighten users
  • Reaching out to the Enlighten development team for support