-1

I want to update a print result in a first line and update a progressbar in a second line. I made a python code, but my script prints a text line by line, but not update it in a line.

How can I fix it?

from tqdm import *
import time

total_num = 100
bar = tqdm(total=total_num)
bar.set_description('Count Up')

for i in range(total_num):
    bar.update()
    print(f'\r-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ {i} -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+')
    time.sleep(1)
Ahmed Mamdouh
  • 696
  • 5
  • 12

2 Answers2

-1

You can clear the screen by using python's builtin subprocess module.

from subprocess import run
from sys import platform
def clear():
    run("cls" if platform in {'win32', 'cygwin'} else "clear")
theX
  • 1,008
  • 7
  • 22
-1

your line is not updading because the print function prints a newline character so what you want to do is

from tqdm import *
import time
import sys

total_num = 100
bar = tqdm(total=total_num)
bar.set_description('Count Up')

for i in range(total_num):
    bar.update()
    sys.stdout.write(f'\r{i}')
    time.sleep(0.2)

but that messes up that output, what i suggest is updating the bar description (this is also on an example on the tqdm github page)

from tqdm import *
import time

total_num = 100
bar = tqdm(total=total_num)

for i in range(total_num):
    bar.set_description(f"{i} Count Up")
    bar.update()
    time.sleep(0.2)
ObebO
  • 16
  • Hello, it's not a solution to update the bar description. The purpose of this ask is to update a long line of text and progressbar at the same time. – Ahmed Mamdouh Sep 03 '20 at 22:50