tqdm / tqdm

:zap: A Fast, Extensible Progress Bar for Python and CLI

Home Page:https://tqdm.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Set `ncols='100%'` raise `TypeError` in jupyter lab

purpleskyfall opened this issue · comments

  • I have marked all applicable categories:
    • exception-raising bug
    • visual output bug
  • 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:
    import tqdm, sys
    print(tqdm.__version__, sys.version, sys.platform)

I'm using tqdm in Jupyter lab, when set ncols='100%' in tqdm.notebook.tqdm or tqdm.notebook.tqdm_notebook function, I got a TypeError:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\IPython\core\formatters.py:974, in MimeBundleFormatter.__call__(self, obj, include, exclude)
    971     method = get_real_method(obj, self.print_method)
    973     if method is not None:
--> 974         return method(include=include, exclude=exclude)
    975     return None
    976 else:

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\ipywidgets\widgets\widget.py:803, in Widget._repr_mimebundle_(self, **kwargs)
    802 def _repr_mimebundle_(self, **kwargs):
--> 803     plaintext = repr(self)
    804     if len(plaintext) > 110:
    805         plaintext = plaintext[:110] + '…'

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\tqdm\notebook.py:84, in TqdmHBox.__repr__(self, pretty)
     82 if pbar is None:
     83     return super(TqdmHBox, self).__repr__()
---> 84 return pbar.format_meter(**self._json_(pretty))

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\tqdm\std.py:640, in tqdm.format_meter(n, total, elapsed, ncols, prefix, ascii, unit, unit_scale, rate, bar_format, postfix, unit_divisor, initial, colour, **extra_kwargs)
    636     return nobar  # no `{bar}`; nothing else to do
    638 # Formatting progress bar space available for bar's display
    639 full_bar = Bar(frac,
--> 640                max(1, ncols - disp_len(nobar)) if ncols else 10,
    641                charset=Bar.ASCII if ascii is True else ascii or Bar.UTF,
    642                colour=colour)
    643 if not _is_ascii(full_bar.charset) and _is_ascii(bar_format):
    644     bar_format = str(bar_format)

TypeError: unsupported operand type(s) for -: 'str' and 'int'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\IPython\core\formatters.py:708, in PlainTextFormatter.__call__(self, obj)
    701 stream = StringIO()
    702 printer = pretty.RepresentationPrinter(stream, self.verbose,
    703     self.max_width, self.newline,
    704     max_seq_length=self.max_seq_length,
    705     singleton_pprinters=self.singleton_printers,
    706     type_pprinters=self.type_printers,
    707     deferred_pprinters=self.deferred_printers)
--> 708 printer.pretty(obj)
    709 printer.flush()
    710 return stream.getvalue()

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\IPython\lib\pretty.py:407, in RepresentationPrinter.pretty(self, obj)
    405     meth = cls._repr_pretty_
    406     if callable(meth):
--> 407         return meth(obj, self, cycle)
    408 if cls is not object \
    409         and callable(cls.__dict__.get('__repr__')):
    410     return _repr_pprint(obj, self, cycle)

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\tqdm\notebook.py:87, in TqdmHBox._repr_pretty_(self, pp, *_, **__)
     86 def _repr_pretty_(self, pp, *_, **__):
---> 87     pp.text(self.__repr__(True))

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\tqdm\notebook.py:84, in TqdmHBox.__repr__(self, pretty)
     82 if pbar is None:
     83     return super(TqdmHBox, self).__repr__()
---> 84 return pbar.format_meter(**self._json_(pretty))

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\tqdm\std.py:640, in tqdm.format_meter(n, total, elapsed, ncols, prefix, ascii, unit, unit_scale, rate, bar_format, postfix, unit_divisor, initial, colour, **extra_kwargs)
    636     return nobar  # no `{bar}`; nothing else to do
    638 # Formatting progress bar space available for bar's display
    639 full_bar = Bar(frac,
--> 640                max(1, ncols - disp_len(nobar)) if ncols else 10,
    641                charset=Bar.ASCII if ascii is True else ascii or Bar.UTF,
    642                colour=colour)
    643 if not _is_ascii(full_bar.charset) and _is_ascii(bar_format):
    644     bar_format = str(bar_format)

TypeError: unsupported operand type(s) for -: 'str' and 'int'

You may reproduce this bug by:

import time
from tqdm.notebook import tqdm

for i in tqdm(range(500), ncols='100%'):
    time.sleep(0.01)

My environment version:

  • os: Windows 11 23H2 22631.2506
  • python: 3.11.5
  • ipywidgets: 8.1.1
  • jupyter_client: 8.3.1
  • jupyter_core: 5.3.1
  • jupyterlab: 4.0.5
  • tqdm: 4.66.1

According to the documentation, ncols must be integer.

To quote the documentation directly:

 |  ncols  : int, optional
 |      The width of the entire output message. If specified,
 |      dynamically resizes the progressbar to stay within this bound.
 |      If unspecified, attempts to use environment width. The
 |      fallback is a meter width of 10 and no limit for the counter and
 |      statistics. If 0, will not print any meter (only stats).

But in documentation of IPython/Jupyter Integration section, it says:

The notebook version supports percentage or pixels for overall width (e.g.: ncols='100%' or ncols='480px').

@CopperEagle