19

The below question is for people who use PyCharm. There are nested for loops and tqdm is used for progress bars corresponding to each for loop. The code is shown below.

from tqdm import tqdm
import time

for i in tqdm(range(5), desc="i", colour='green'):
    for j in tqdm(range(10), desc="j", colour='red'):
        time.sleep(0.5)

But the problem is, the progress bars for the inner loop appear in newline everytime there is an update in the bar as shown below.

i:   0%|          | 0/5 [00:00<?, ?it/s]
j:   0%|          | 0/10 [00:00<?, ?it/s]
j:  10%|█         | 1/10 [00:00<00:04,  1.94it/s]
j:  20%|██        | 2/10 [00:01<00:04,  1.94it/s]
j:  30%|███       | 3/10 [00:01<00:03,  1.96it/s]
j:  40%|████      | 4/10 [00:02<00:03,  1.96it/s]
j:  50%|█████     | 5/10 [00:02<00:02,  1.97it/s]
j:  60%|██████    | 6/10 [00:03<00:02,  1.97it/s]
j:  70%|███████   | 7/10 [00:03<00:01,  1.97it/s]
j:  80%|████████  | 8/10 [00:04<00:01,  1.98it/s]
j:  90%|█████████ | 9/10 [00:04<00:00,  1.98it/s]
j: 100%|██████████| 10/10 [00:05<00:00,  1.98it/s]
i:  20%|██        | 1/5 [00:05<00:20,  5.06s/it]
j:   0%|          | 0/10 [00:00<?, ?it/s]
j:  10%|█         | 1/10 [00:00<00:04,  2.00it/s]
j:  20%|██        | 2/10 [00:01<00:04,  1.99it/s]
j:  30%|███       | 3/10 [00:01<00:03,  1.99it/s]
j:  40%|████      | 4/10 [00:02<00:03,  1.99it/s]
j:  50%|█████     | 5/10 [00:02<00:02,  1.99it/s]
j:  60%|██████    | 6/10 [00:03<00:02,  1.99it/s]
j:  70%|███████   | 7/10 [00:03<00:01,  1.99it/s]
j:  80%|████████  | 8/10 [00:04<00:01,  1.99it/s]
j:  90%|█████████ | 9/10 [00:04<00:00,  1.99it/s]
j: 100%|██████████| 10/10 [00:05<00:00,  1.99it/s]
i:  40%|████      | 2/5 [00:10<00:15,  5.05s/it]

Setting the parameter 'position` for each loop also doesn't fix the issue.

from tqdm import tqdm
import time

for i in tqdm(range(5), desc="i", colour='green', position=0):
    for j in tqdm(range(10), desc="j", colour='red', position=1):
        time.sleep(0.5)

How does one get the progress bar to update in the same line?

learner
  • 3,168
  • 3
  • 18
  • 35

2 Answers2

27

The solution is two fold.

  1. Go to "Edit configurations". Click on the run/debug configuration that is being used. There should be an option "Emulate terminal in output console". Check that. Image added for reference. enter image description here

  2. Along with the position argument also set the leave argument. The code should look like this. I have added ncols so that the progress bar doesn't take up whole of the console.

from tqdm import tqdm
import time

for i in tqdm(range(5), position=0, desc="i", leave=False, colour='green', ncols=80):
    for j in tqdm(range(10), position=1, desc="j", leave=False, colour='red', ncols=80):
        time.sleep(0.5)

When the code is now run, the output of the console is as shown below.

i:  20%|████████▍                                 | 1/5 [00:05<00:20,  5.10s/it]
j:  60%|████████████████████████▌                | 6/10 [00:03<00:02,  1.95it/s]

The updates happen on the same line.

Dharman
  • 30,962
  • 25
  • 85
  • 135
learner
  • 3,168
  • 3
  • 18
  • 35
  • 4
    unfortunately, this checkbox does not exist when using a ssh interpreter (2020.3) – cr0 Jan 29 '21 at 09:47
  • 1
    @cr0 this issue has been raised [here](https://youtrack.jetbrains.com/issue/PY-29204). It is up to the developers to provide this option. – learner Mar 03 '21 at 09:09
  • @cr0 please see my answer – Nisba Jan 20 '22 at 01:06
  • none of any solutions is working in 2022 idea / pycharm plugin (option emulate terminal missing; tried all combinations of Leave, Position, tqdm and tqdm.auto) – stam Dec 07 '22 at 17:27
  • It works for me: using PyCharm 2022.3.3 (Community Edition): right click on the .py file that you want to run, select "Modify Run Configuration...": it opens a window, scroll down to the "Execution" section, and there you find "Emulate terminal in output console" – MarcoS Aug 09 '23 at 12:05
5

Here is a solution that also works for PyCharm SSH interpreters (I am using 2021.3.1 (Professional Edition) and I don't have the option "Emulate terminal in output console"):

from tqdm.auto import tqdm

for i in tqdm(range(10), position=0, leave=True):
    for index in tqdm(range(10), position=0, leave=True):
        pass
Nisba
  • 3,210
  • 2
  • 27
  • 46