In my python project I'm using tqdm
module to display a progress bar. I want to print a persistent message on the line before the progress bar.
The set_description
method prints the message on the same line, whilte tqdm.write
creates a new line.
Using set_description
$ python pbar.py {Task_1 message} 3%|████ ]
Is is possible to achieve this
$ python pbar.py {Task_1 message} 3%|████ ]
Edit:
from tqdm import tqdm
pbar = tqdm(m_list)
for item in m_list:
# Do work
pbar.update(1)
pbar.close()
Simple example with a loop:
import tqdm,time
for i in tqdm.tqdm(range(9),desc="{Task_1 message}"):
time.sleep(0.1)
final output:
{Task_1 message}: 100%|##############################################| 9/9 [00:00<00:00, 9.99it/s]
If you add \\n
to the description, it will trash your output like this:
{Task_1 message}
{Task_1 message} | 0/9 [00:00<?, ?it/s]
{Task_1 message} | 1/9 [00:00<00:00, 9.99it/s]
{Task_1 message}# | 2/9 [00:00<00:00, 9.99it/s]
{Task_1 message}###### | 3/9 [00:00<00:00, 9.99it/s]
{Task_1 message}########### | 4/9 [00:00<00:00, 9.99it/s]
{Task_1 message}################ | 5/9 [00:00<00:00, 9.99it/s]
{Task_1 message}##################### | 6/9 [00:00<00:00, 9.96it/s]
{Task_1 message}########################## | 7/9 [00:00<00:00, 9.97it/s]
{Task_1 message}############################### | 8/9 [00:00<00:00, 9.98it/s]
: 100%|#############################################| 9/9 [00:00<00:00, 9.98it/s]
The only thing you can do is to print the description first, and run the task without description, as you can only delete the current line in the terminal.
import tqdm,time
print("{Task_1 message}")
for i in tqdm.tqdm(range(9)):
time.sleep(0.1)
As previously said, you can only delete the current line in the terminal (using \\r
), there are numerous topics about that limitation on SO.
The alternative is using curses, but that's a completely different approach.
这是一个可能的解决方法,对我有用:
print("Your message", flush=True)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.