Ask Your Question
1

tqdm's "total" parameter bug

asked 2019-04-30 10:57:03 +0100

PepijnWissing gravatar image

updated 2019-04-30 13:09:25 +0100

Due to something in the Sage 8.7 kernel (currently running in CoCalc), the tqdm progress bar crashes when I try to specify its total parameter. According to the documentation, my syntax should be correct. Specifically,

from tqdm import tqdm_notebook as tqdm
for i in tqdm(range(50),total=75):
    print(i)

yields

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-1-e4e61f3c450b> in <module>()
      1 from tqdm import tqdm_notebook as tqdm
----> 2 for i in tqdm(range(Integer(50)),total=Integer(75)):
      3     print(i)
/ext/sage/sage-8.7_1804/local/lib/python2.7/site-packages/tqdm/__init__.pyc in tqdm_notebook(*args, **kwargs)
     23     """See tqdm._tqdm_notebook.tqdm_notebook for full documentation"""
     24     from ._tqdm_notebook import tqdm_notebook as _tqdm_notebook
---> 25     return _tqdm_notebook(*args, **kwargs)
     26 
     27 
/ext/sage/sage-8.7_1804/local/lib/python2.7/site-packages/tqdm/_tqdm_notebook.pyc in __init__(self, *args, **kwargs)
    215         # Print initial bar state
    216         if not self.disable:
--> 217             self.sp(self.__repr__())  # same as self.refresh without clearing
    218 
    219     def __iter__(self, *args, **kwargs):
/ext/sage/sage-8.7_1804/local/lib/python2.7/site-packages/tqdm/_tqdm.pyc in __repr__(self)
    982 
    983     def __repr__(self):
--> 984         return self.format_meter(**self.format_dict)
    985 
    986     @property
/ext/sage/sage-8.7_1804/local/lib/python2.7/site-packages/tqdm/_tqdm.pyc in format_meter(n, total, elapsed, ncols, prefix, ascii, unit, unit_scale, rate, bar_format, postfix, unit_divisor, **extra_kwargs)
    395             percentage = frac * 100
    396 
--> 397             l_bar += '{0:3.0f}%|'.format(percentage)
    398 
    399             if ncols == 0:
ValueError: Unknown format code 'f' for object of type 'str'

Especially since a similar implementation of tqdm does work in a python 3 sheet, this feels like a minor bug to me. Or am I making some silly mistake?

-- edit: replaced example with a more straightforward one

edit retag flag offensive close merge delete

Comments

Try with "10" instead of str(10)

FrédéricC gravatar imageFrédéricC ( 2019-04-30 11:37:45 +0100 )edit

Thanks for your answer, but the "10" isn't the problem; graphs.nauty_geng takes a string argument and returns an Iterable. The issue is related to the other number, which somehow becomes a string in tqdm.

I have replaced my example in response to your suggestion, to make the question more clear.

PepijnWissing gravatar imagePepijnWissing ( 2019-04-30 13:04:09 +0100 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2019-04-30 16:05:41 +0100

slelievre gravatar image

updated 2019-04-30 16:25:27 +0100

Sage uses a preparser, which transforms the "75" in your input into the Sage integer 75; quotients with this "75" as a numerator or denominator thereafter become rationals instead of floats.

The workaround for you is to input "75r" which stands for the raw Python integer 75, not to be preparsed by the Sage preparser.

So this will work (after installing tqdm for the Python of the Sage you are using):

from tqdm import tqdm_notebook as tqdm
for i in tqdm(range(50), total=75r):
    print(i)

Note: if you want to use a Python3-based Sage in CoCalc, you can select the kernel "SageMath (development, Py3)" from the list of kernels in CoCalc's Jupyter notebook.

To install extra pip packages for that Sage, do the following in a CoCalc terminal:

Select the development version of Sage:

~$ sage_select develop
Making default sage /ext/bin/sage-develop by creating ~/bin/sage symbolic link:
Restart your project server!

Then restart your project server (go to the project settings, and click "Restart Project" halfway down the right column).

Reopen the CoCalc terminal and check you have the right version of Sage:

~$ sage --version
SageMath version 8.8.beta1, Release Date: 2019-04-07

and that it is Python3-based:

~$ sage --python --version
Python 3.7.3

Then you can install extra Python packages for it:

~$ sage --pip install --user tqdm
Collecting tqdm
  Downloading https://files.pythonhosted.org/packages/6c/4b/c38b5144cf167c4f52288517436ccafefe9dc01b8d1c190e18a6b154cd4a/tqdm-4.31.1-py2.py3-none-any.whl (48kB)
    100% |████████████████████████████████| 51kB 5.6MB/s 
Installing collected packages: tqdm
Successfully installed tqdm-4.31.1

Then in Jupyter worksheets using the "SageMath (development, Py3)" kernel you can import tqdm and use it.

edit flag offensive delete link more

Comments

Thanks, that'll do it!

PepijnWissing gravatar imagePepijnWissing ( 2019-05-01 09:22:08 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2019-04-30 10:57:03 +0100

Seen: 899 times

Last updated: Apr 30 '19