question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support capturing interactive progress bar

See original GitHub issue

I am using tqdm which interactively update the progress bar in the terminal. A sacred observer captures stdout like this:

[train]    loss =   0.0000    acc =   1.0000:   2%|▏         | 29/1500 [00:01<11:18,  2.17it/s]
[train]    loss =   0.0002    acc =   1.0000:   2%|▏         | 29/1500 [00:01<11:18,  2.17it/s]
[train]    loss =   0.0002    acc =   1.0000:   3%|▎         | 45/1500 [00:01<07:52,  3.08it/s]
[train]    loss =   0.0000    acc =   1.0000:   3%|▎         | 45/1500 [00:01<07:52,  3.08it/s]
[train]    loss =   0.0000    acc =   1.0000:   3%|▎         | 45/1500 [00:01<07:52,  3.08it/s]
[train]    loss =   0.0041    acc =   1.0000:   3%|▎         | 45/1500 [00:01<07:52,  3.08it/s]
[train]    loss =   0.0000    acc =   1.0000:   3%|▎         | 45/1500 [00:01<07:52,  3.08it/s]
[train]    loss =   0.0000    acc =   1.0000:   3%|▎         | 45/1500 [00:01<07:52,  3.08it/s]
[train]    loss =   0.0000    acc =   1.0000:   3%|▎         | 45/1500 [00:01<07:52,  3.08it/s]
[train]    loss =   0.0000    acc =   1.0000:   3%|▎         | 45/1500 [00:01<07:52,  3.08it/s]
[train]    loss =   0.0000    acc =   1.0000:   3%|▎         | 45/1500 [00:01<07:52,  3.08it/s]
[train]    loss =   0.0000    acc =   1.0000:   3%|▎         | 45/1500 [00:01<07:52,  3.08it/s]
[train]    loss =   0.0002    acc =   1.0000:   3%|▎         | 45/1500 [00:01<07:52,  3.08it/s]
[train]    loss =   0.0045    acc =   1.0000:   3%|▎         | 45/1500 [00:01<07:52,  3.08it/s]
[train]    loss =   0.0000    acc =   1.0000:   3%|▎         | 45/1500 [00:01<07:52,  3.08it/s]
[train]    loss =   0.0003    acc =   1.0000:   3%|▎         | 45/1500 [00:01<07:52,  3.08it/s]
[train]    loss =   0.0003    acc =   1.0000:   4%|▍         | 58/1500 [00:01<05:31,  4.35it/s]
[train]    loss =   0.0000    acc =   1.0000:   4%|▍         | 58/1500 [00:01<05:31,  4.35it/s]

Applying ex.captured_out_filter = apply_backspaces_and_linefeeds has no effect. If I have a nested tqdm (two for-loops),

[train]    loss =   0.0000    acc =   1.0000:   2%|▏         | 30/1500 [00:01<10:54,  2.25it/s]

[train]    loss =   0.0000    acc =   1.0000:   3%|▎         | 45/1500 [00:01<07:36,  3.19it/s]

[train]    loss =   0.0000    acc =   1.0000:   3%|▎         | 45/1500 [00:01<07:36,  3.19it/s]

[train]    loss =   0.0000    acc =   1.0000:   3%|▎         | 45/1500 [00:01<07:36,  3.19it/s]

[train]    loss =   0.0001    acc =   1.0000:   3%|▎         | 45/1500 [00:01<07:36,  3.19it/s]

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:3
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
gabrieldemarmiessecommented, May 15, 2019

I managed to fix the TQDM issue with multiple lines, but not the color issue. To fix, use:

from sacred import SETTINGS
from sacred.utils import apply_backspaces_and_linefeeds


SETTINGS.CAPTURE_MODE = 'sys'

...
ex.captured_out_filter = apply_backspaces_and_linefeeds

From what I understand: If you don’t do this, tqdm will detect that you are using a terminal (SETTINGS.CAPTURE_MODE = 'fd' which is the default in linux) and that it’s possible to move the cursor inside this terminal. So tqdm will use \n and move the cursor. By using SETTINGS.CAPTURE_MODE = 'sys', tqdm understands that it has to write inside a text stream and that it can’t control the cursor. It then uses \r to come back at the start of the line. This use case is then handled correctly by apply_backspaces_and_linefeeds. Given how the capture works, I believe it’s impossible to make it work with nested progress bars in TQDM. Colored output is another problem which is unrelated to this fix.

Feel free to correct me. This comes from my limited knowledge of linux, shells and streams.

1reaction
amtsengcommented, Sep 26, 2019

If you’re using Python 3, this issue may be because the captured output has all of its carriage returns (\r) converted to new lines (\n). I’ve submitted this bug report (above).

This quick fix has worked for me:

Try altering line 119 in stdout_capturing.py to be with NamedTemporaryFile(mode="w+", newline='') as target:. You’ll still need to apply ex.captured_out_filter = apply_backspaces_and_linefeeds , but you won’t need to change SETTINGS.CAPTURE_MODE.

Read more comments on GitHub >

github_iconTop Results From Across the Web

10 inspiring progress bars that delight users
Progress bars set expectations, give an impression of activity and can calm users. Here are 10 that do it right.
Read more >
Applying or Capturing Image No Progress Bar Movement
During the applying image or capturing image step the progress bar remains stuck at 0% for about 15 minutes then bounces all the...
Read more >
Using a Progress Bar (UI) in SaaS-Types and Examples
A progress bar (UI) is a tool to boost engagement and reduce friction in SaaS. Here's how you should use them, and how...
Read more >
How to create a Cross-browser Compatible HTML ...
Learn about HTML5 Progress Bar, its elements, the best way to style it using CSS, and solutions for cross-browser compatibility issues.
Read more >
ProgressBar
Progress bar supports two modes to represent progress: determinate, ... Automatically determine whether a view is important for content capture.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found