-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tqdm.write(string) doesn't work well when string has \n in it #564
Comments
related: #520 ( I think the problem here is you don't clear the nested bar or print a newline after it. You need: def test():
for x in tqdm(range(30), leave=False):
sleep(0.05) A minimal reproduction of your problem: from tqdm import trange
for i in trange(10):
for j in trange(10):
pass
# print("\n")
print("hello") # prints on top of last nested bar. |
Thanks for looking. from tqdm import trange
for i in trange(3):
tqdm.write("task " + str(i) + " started")
for j in trange(4):
pass works. But with tqdm.write("task " + str(i) + " done\nand well started") doesn't. However I may have misunderstood your answer. If so I'm sorry. |
The issue in your new example is the same - the inner |
Actually, putting leave=false delete the inner progress bar, which is a different behavior. tqdm.write("task " + str(i) + " done\nand well done") would behave in the same way that : tqdm.write("task " + str(i) + " done")
tqdm.write("and well done") My guess is that in the second snippet, the code knows that it has to climb up two lines, in the first one, it thinks only one is enough. In a second time, but I think it's a clearly different, more complex subject, it would be nice to be able to keep all the inner progress bar (for example to keep the stats) while inserting text between them. This issue is linked I think to #230 : outer: 78%|███████████ | 2/3 [00:00<00:00, 6189.33it/s] |
if you want all nested bars to remain, then you can manually from tqdm import tqdm, trange
from time import sleep
def test(position=None, bar=None):
if bar is None:
bar = trange(30, position=position)
for x in bar:
sleep(0.05)
N = 10 then either: for x in trange(N):
test(position=x + 1)
print('\n' * (N - 1)) or pre-allocate: bars = [trange(30, position=i +1, desc=str(i + 1)) for i in range(N)]
for x in trange(N):
#tqdm.write("test")
#tqdm.write("test"); tqdm.write("foo")
#tqdm.write("test\nfoo")
test(bar=bars[x])
print('\n' * (N - 1)) However in both cases |
Here's a solution: from tqdm import trange
import time
def line_messages(messages):
for i, m in enumerate(messages, 1):
trange(1, desc=str(m), position=i, bar_format='{desc}')
n_messages = 2
for i in trange(3):
line_messages(['iter: %s' % i, 'half: %s' % (i/2)])
time.sleep(1)
for _ in range(n_messages): print() Preview:
You can switch the bar to appear below the messages by changing the position parameters of the two trange declarations (i.e. outer one should be length of messages, inner should start at zero, print n_messages+1 at the end). The downside is having to print newlines after the overall loop to keep the cursor in the correct place afterwards. Also, haven't tested on windows. |
Hi,
I'am using tqdm 4.23.4 on python 3.6.3.
I noticed what I think might be an issue when I use tqdm.write with a multiline string.
The two snippets below don't work the same way (the correct behavior I think is the second one) :
Also, but that may be an other issue, I haven't find a way to have nested progress bars with print between bar that would remain plotted (and not print above all the bars).
The text was updated successfully, but these errors were encountered: