# Println and ProgressMeter in the same loop

**URL:** <https://discourse.julialang.org/t/println-and-progressmeter-in-the-same-loop/32140>\
**Category:** General Usage\
**Created:** [December 11, 2019, 9:29am UTC](https://discourse.julialang.org/t/println-and-progressmeter-in-the-same-loop/32140 "2019-12-11T09:29:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![benzwick](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@benzwick](https://discourse.julialang.org/u/benzwick)\
**Post date:** [December 11, 2019, 9:29am UTC](https://discourse.julialang.org/t/println-and-progressmeter-in-the-same-loop/32140/1 "2019-12-11T09:29:02Z")

</div>

I am trying to combine `println` (or `@info` for logging) and a `ProgressMeter` progress bar in a loop like this:

```julia
using ProgressMeter
prog = Progress(10)
for i in 1:10
    println("value: ", i^2)
    sleep(0.1)
    next!(prog)
end

```

But the output is not as nice as it could be:

```julia
value: 1
Progress: 10%|████▏ | ETA: 0:00:01value: 4
Progress: 20%|████████▎ | ETA: 0:00:01value: 9
Progress: 30%|████████████▎ | ETA: 0:00:01value: 16
Progress: 40%|████████████████▍ | ETA: 0:00:01value: 25
Progress: 50%|████████████████████▌ | ETA: 0:00:01value: 36
Progress: 60%|████████████████████████▋ | ETA: 0:00:00value: 49
Progress: 70%|████████████████████████████▊ | ETA: 0:00:00value: 64
Progress: 80%|████████████████████████████████▊ | ETA: 0:00:00value: 81
Progress: 90%|████████████████████████████████████▉ | ETA: 0:00:00value: 100
Progress: 100%|█████████████████████████████████████████| Time: 0:00:01

```

Is it possible to get the output to look as follows (with a single progress bar always at the bottom of the screen)?

```julia
value: 1
value: 4
value: 9
value: 16
Progress: 40%|████████████████▍ | ETA: 0:00:01

```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [December 11, 2019, 9:30am UTC](https://discourse.julialang.org/t/println-and-progressmeter-in-the-same-loop/32140/2 "2019-12-11T09:30:38Z")

</div>

Why don’t you use ProgressMeter’s ability to add extra info, it’s made for this:  
[https://github.com/timholy/ProgressMeter.jl#printing-additional-information](https://github.com/timholy/ProgressMeter.jl#printing-additional-information)

---

<div class="post-metadata">

**Author:** ![benzwick](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@benzwick](https://discourse.julialang.org/u/benzwick)\
**Post date:** [December 11, 2019, 9:36am UTC](https://discourse.julialang.org/t/println-and-progressmeter-in-the-same-loop/32140/3 "2019-12-11T09:36:10Z")

</div>

I considered this but the information is not printed on every iteration (it depends on some conditionals elsewhere in the loop and there is more than one message). Also, I would like the printed output to go to a log file and for the message to remain on screen instead of being replaced when the progress bar is updated.

---

<div class="post-metadata">

**Author:** ![benzwick](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@benzwick](https://discourse.julialang.org/u/benzwick)\
**Post date:** [December 11, 2019, 9:40am UTC](https://discourse.julialang.org/t/println-and-progressmeter-in-the-same-loop/32140/4 "2019-12-11T09:40:44Z")

</div>

The actual use case is more like this:

```julia
prog = Progress(10)
for i in 1:10
    if i == 5
        println("value: ", i^2, " this is a message")
    end
    sleep(0.1)
    next!(prog)
end

```

and this is the curent output:

```julia
Progress: 40%|████████████████▍ | ETA: 0:00:01value: 25 this is a message
Progress: 100%|█████████████████████████████████████████| Time: 0:00:01

```

but I would prefer this:

```julia
value: 25 this is a message
Progress: 40%|████████████████▍ | ETA: 0:00:01

```

---

<div class="post-metadata">

**Author:** ![anowacki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/anowacki/32/17375_2.png) [@anowacki](https://discourse.julialang.org/u/anowacki)\
**Post date:** [December 11, 2019, 9:55am UTC](https://discourse.julialang.org/t/println-and-progressmeter-in-the-same-loop/32140/5 "2019-12-11T09:55:14Z")

</div>

A bit of carriage-return trickery and filling your terminal with spaces via `rpad()` is probably the easiest way of doing this yourself. Using `displaysize()` to get the width of your terminal is needed unless there is another way to query how wide the progress meter is. (I haven’t used ProgressMeter.jl myself.)

```julia
prog = Progress(10)
for i in 1:10
    if i == 5
        println(rpad("\rvalue: $(i^2) this is a message",
                displaysize(stdout)[2]))
    end
    sleep(0.1)
    next!(prog)
end

```

I guess if this output is a common desire, submitting a feature request issue to ProgressMeter.jl or even submitting a PR would be welcome.

---

<div class="post-metadata">

**Author:** ![benzwick](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@benzwick](https://discourse.julialang.org/u/benzwick)\
**Post date:** [December 11, 2019, 1:04pm UTC](https://discourse.julialang.org/t/println-and-progressmeter-in-the-same-loop/32140/7 "2019-12-11T13:04:44Z")

</div>

This works quite well except that the carriage return `\r` is written to the log file. Also I needed to make the progress bar slightly narrower because the logging prefix requires some space (I’m using the Memento package).

I wrapped the logging macro in a function to make it easier to use in multiple places:

```julia
rpadinfo(s) = @info rpad("\r"*s, displaysize(stdout)[2])

```

---

<div class="post-metadata">

**Author:** ![Lightup1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lightup1/32/35410_2.png) [@Lightup1](https://discourse.julialang.org/u/Lightup1)\
**Post date:** [July 13, 2022, 2:14pm UTC](https://discourse.julialang.org/t/println-and-progressmeter-in-the-same-loop/32140/8 "2022-07-13T14:14:16Z")

</div>

In my Ubuntu:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/6/06e398195da27a3fd34aa45d8147c75cc749cf72.png)  
What happened here?
