21

I'm using tqdm's progress bar, and I'd like to shorten the bar itself by using an argument to indicate how many progress ticks the bar should have

So instead of this

Training (16): 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▊| 983/984 [00:04<00:00, 242.42it/s, loss=0.0598]

I would get something like this

Training (16): 100%|█████████████| 983/984 [00:04<00:00, 242.42it/s, loss=0.0598]

I've explored the bar_format argument in tqdm's constructor, but couldn't figure out how to change it's size.

bluesummers
  • 11,365
  • 8
  • 72
  • 108
  • 1
    Check this link : [text-progress-bar-in-the-console](https://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console) – Anonymous Jan 25 '19 at 09:44

3 Answers3

27

The relevant formatting code is: {bar:10} -- if you want 10 characters of progress bar. In full, you would use it like this:

tqdm(iterator, bar_format='{l_bar}{bar:10}{r_bar}{bar:-10b}')

or

tqdm(iterator, bar_format='{desc:<5.5}{percentage:3.0f}%|{bar:10}{r_bar}')

See also: https://github.com/tqdm/tqdm/issues/585

Yaakov Belch
  • 4,692
  • 33
  • 39
21

You need to pass a value for ncols. This defaults to the width of the terminal so if you want it to be less you have to say so.

From https://github.com/tqdm/tqdm

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).

Dan D.
  • 73,243
  • 15
  • 104
  • 123
  • 7
    `ncols` is the width of the entire output message, I'm looking to determine the width of the progress bar itself – bluesummers Jan 25 '19 at 10:57
  • But what you ask for is what a smaller ncols would give you. The width of the progress bar is ncols minus the width of the other parts of the output. – Dan D. Jan 25 '19 at 11:03
  • Is it guaranteed never to cut the postfix / description? – bluesummers Jan 25 '19 at 11:07
2

This only applies to tqdm progress bar for the notebook

Because the tqdm progress bar in a jupyter notebook is a jupyter widget, we can modify the bar by changing the layout of the elements of the container. Consider this progress bar:

from tqdm.auto import tqdm
bar = tqdm(
    bar_format="Amount: {bar}{n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}{postfix}]",
)

Access the elements using bar.container.children, you get:

(HTML(value='Amount: '),
 FloatProgress(value=0.0, bar_style='info', layout=Layout(width='20px'), max=1.0),
 HTML(value='0/? [00:00&lt;?, ?it/s]'))

If you want to change the length of the actual bar, you can do so using:

bar.container.children[1].layout.width = "70%"

which will get make the progress bar's length 70% of the length of the container. The length of the container is the length of the cell.

You can also apply other properties, changing the length of the other 2 elements could change the indentation of the progress bar etc. The same properties can be applied to the entire container too, and not just it's children. To see what properties you can change, head over to the documentation.

atharva
  • 80
  • 1
  • 7