mrlazy1708 / tqdm

Python tqdm in Rust.

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allow re-use of bars and/or nesting multiple bars

jkorinth opened this issue · comments

Nested tqdm iterators like this

for &i in tqdm(a.iter()) {
  for &j in tqdm(b.iter()) {
    ...
  }
}

currently produce a new bar and a newline on each iteration of the outer loop. It would be nice to be able to re-use the inner bar (reset count and state), e.g.,

let mut inner_bar = tqdm::bar();
for &i in tqdm(a.iter()) {
  for &j in inner_bar.reset(b.iter()) {
    ...
  }
}

Another approach may be to have a multibar environment that automatically keeps track of the named bars and re-uses them, e.g.,

let mut mb = tqdm::Multibar::new();
for &i in tqdm(a.iter()).desc(Some("outer")) {
  for &j in tqdm(b.iter()).desc(Some("inner")) {
    ...
  }
}

Hi @jkorinth, I got what you mean and thanks for bringing this up. The problem here seems to be the behavior of line breaking after termination. Currently I have line breaks anyway, so the outer bar would move forward every iteration.

Regarding the fixes you mentioned, I think both are doable for this purpose, despite the feeling that both are a little bit anti-Rusty. I checked out py tqdm and found there's a leave argument in its param list. I guess this could be the easiest way to go. In py it looks like:

for a in tqdm([1, 2, 3, 4, 5, 6, 7]):
    for b in tqdm([1, 2, 3], leave=False):
        for c in tqdm([1, 2, 3], leave=False):

            from time import sleep
            sleep(0.1)

If that also works in your case I'll add leave to the builder

Does 1aa14ae fix your case? Now we have

for _ in tqdm(0..3) {
    for _ in tqdm(0..4).clear(true) {
        for _ in tqdm(0..5).clear(true) {
            ...
        }
    }
}

While another bug I noticed is that after switching to hash map 97dab28, the order of bars is messed up - will fix it soon fixed by aaf0273

@mrlazy1708 Yes, that is looking very good, thanks!