tqdm/tqdm

Unexpected behavior when combining `initial` and `range` as iterator.

twall opened this issue · 5 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)

4.66.2 3.10.9 (main, Jan 11 2023, 09:18:20) [Clang 14.0.6 ] darwin

from tqdm import tqdm 

for level in tqdm(list(range(1, 16)), initial=10):
  print(f"Level={level}")

Output:

 67%|████████████████████████           | 10/15 [00:00<?, ?it/s]
Level 1
Level 2
Level 3
Level 4
Level 5
Level 6
Level 7
Level 8
Level 9
Level 10
Level 11
Level 12
Level 13
Level 14
Level 15

In this case, the initial argument is not completely ignored, since the status seems to stop at 10. I would expect either an exception or warning about the argument being ignored, or in the case of an indexable iterable actually use it to start at the correct index.

My workaround is to ignore the first initial iterations, but it was unexpected to have the full set of iterations produced by tqdm when initial was specified.

Hi @twall,

I have try to reproduce the issue here. The output is following

 67%|████████████████████████████▋              | 10/15 [00:00<?, ?it/s]Level=1
Level=2
Level=3
Level=4
Level=5
Level=6
Level=7
Level=8
Level=9
Level=10
Level=11
Level=12
Level=13
Level=14
Level=15
25it [00:00, 205603.14it/s]

I am not sure if the last line is missed in your post.
Based on document and the response(#215 (comment)) to a similar issue, the initial only sets the initial offset for iteration display.
And I think the following code is what you are looking for.

from tqdm import tqdm 

for level in tqdm(list(range(1, 16)), initial=10, total=10+15):
  print(f"Level={level}")

I am expecting the iterations to start at 10 and finish at 15, for a total of 5 iterations, with the progress bar showing 15/15 when complete.

tqdm(list(range(10, 16)), initial=10, total=16)

almost works, except the final status is 16/16 instead of 15/15. Using total=15 results in an static progress bar stuck at 10/15.

Change it to tqdm(list(10, 16), initial=9, total=15) will meet your needs.
Based on my understanding, you want to iterate through [1..15], which leaves total=15, originally.
And you want to keep on iterate though [10..15], after you have iterated through [1..9].
Since you have iterated through [1..9], the initial counter should be 9, or initial=9 in the argument.

Yes, that works. I was mistakenly interpreting initial to mean the first iterable value, rather than the number of iterations completed. initial really has nothing at all to do with the iterable itself. It is a little odd having the status bar start at 9/15, but that's consistent with 0/N being the initial status of any other N count.