I'm trying to find a way of getting, along with other prints, the result/evolution of a progress bar in a pyqt application, for example in a QPlainTextEdit widget.
The problem I'm facing, is that progress bars can use some more advanced carriage return, or even more advanced cursor positionning that are mostly not supported by treams.
I've tried io.StringIO
, but the \r
is kept literal.
import io
from tqdm import tqdm
s = io.StringIO()
for i in tqdm(range(3), file=s):
sleep(.1)
output:
s.getvalue()
Out[24]: '\n\r 0%| | 0/3 [00:00<?, ?it/s]\x1b[A\n\r 33%|###3 | 1/3 [00:00<00:00, 9.99it/s]\x1b[A\n\r 67%|######6 | 2/3 [00:00<00:00, 9.98it/s]\x1b[A\n\r100%|##########| 3/3 [00:00<00:00, 9.98it/s]\x1b[A\n\x1b[A'
which translate into:
print(s.getvalue())
0%| | 0/3 [00:00<?, ?it/s]
33%|###3 | 1/3 [00:00<00:00, 9.99it/s]
67%|######6 | 2/3 [00:00<00:00, 9.98it/s]
100%|##########| 3/3 [00:00<00:00, 9.98it/s]
To be clear, in my output, I don't want one line per tqdm update, but just the current state, as it would be printed on the command line.
Any idea o how to do this ? Thanks!
The idea is to remove the previous line if there is a new text added, but you must also remove
\r
and verify that it is not an empty text. Also, for an object to receive the text oftqdm
, it must only have thewrite()
method, so implement a customQPlainTextEdit
. UseQMetaObject::invokeMethod()
to make it thread-safe