10

I like working in an IJulia notebook and would like to print the status of some process on the same line over and over again.

Taking the example given in the link below, we desire some output:

Downloading File FooFile.txt [47%]

and want to avoid something like this:

 Downloading File FooFile.txt [47%]
 Downloading File FooFile.txt [48%]
 Downloading File FooFile.txt [49%]

In case of Python I found an answer here.

What would be a solution?

Community
  • 1
  • 1
  • 3
    It is not obvious why this question attracted a down-vote, and this is a good example of why you should always consider leaving a comment explaining your down-vote. I'm up-voting back to 0. – Colin T Bowers Mar 15 '16 at 23:44
  • Maybe someone was having a bad day and felt like giving a downvote. I find that is great to have these beginner questions answered and available here, atleast I benefit a lot from them. – Willem Hekman Mar 16 '16 at 13:07

1 Answers1

12

The "magic" in the Python answer isn't unique to Python… it's just the \r character: it resets the cursor position to the beginning of the line (without creating a new line). Subsequent print instructions will just overwrite the previous text if your terminal supports such cursor movements.

In Julia:

print("Download progress: $(progress)%   \r")
flush(stdout)

You can also take a look at ProgressMeter.jl for fancier cursor movements and output.

mbauman
  • 30,958
  • 4
  • 88
  • 123
  • 1
    Seems to work, but it looks like notebooks clear while moving back, so the "\r" should probably be first in the new message to let the text remain between prints. – lossleader Mar 15 '16 at 23:06
  • Thank you! Such a minimal working example is great to have. I looked at the ProgressMeter before but got confused by all the bells and whistles. – Willem Hekman Mar 16 '16 at 13:05
  • With Julia 1.0 it is now `flush(stdout)`, so no capital letters. – Muqq Oct 22 '20 at 20:22