2

I am trying to clear a line for a loading element, I have the printing-on-one-line part of it, I just need this

I have consulted some websites(mainly this one) and I cannot find an answer, I am using https://trinket.io as my coder, I also have python from https://python.org, can you find code that works?

Here is the code I have so far:

import functools

printf = functools.partial(print, end="")

loading():
    printf("loading")
    printf(".")
    time.sleep(.3)
    printf(".")
    time.sleep(.3)
    printf(".")
    time.sleep(.3)
    printf(".")
    time.sleep(.3)
    printf(".")

Which gives me this output(final stage):

loading.....

I want to delete the line above so I can use the loading sequence multiple times.

braX
  • 11,506
  • 5
  • 20
  • 33
  • 1
    Does this answer your question? [How to overwrite the previous print to stdout in python?](https://stackoverflow.com/questions/5419389/how-to-overwrite-the-previous-print-to-stdout-in-python) – Carcigenicate Nov 07 '19 at 15:22

1 Answers1

0

I believe these adjustments will get what you want:

import functools
import time

printf = functools.partial(print, end="", flush=True)

def loading():
    printf("\r{:12}\r".format(''))
    printf("loading")
    printf(".")
    time.sleep(.3)
    printf(".")
    time.sleep(.3)
    printf(".")
    time.sleep(.3)
    printf(".")
    time.sleep(.3)
    printf(".")

loading()
time.sleep(2)
loading()

Two changes:

  1. Setting flush=True in print makes sure that each dot prints between sleep statements, otherwise they'll all be displayed at once.
  2. printf("\r{:12}\r".format('')) clears the line. '\r' is carriage return, moving cursor to beginning, then it prints some blank spaces to "clear" (overwrite) the loading text (which is 12 characters), and then does carriage return again to print a new loading message.
ufoxDan
  • 609
  • 4
  • 13
  • Thank you, @usfoxDan, that might have worked, but it gave me this output: `loading..... loading.....` –  Nov 07 '19 at 15:51
  • I've tested this through a linux shell and through windows powershell and I only get one line that is overwritten. What system are you running this on? Are there any `\n` or `print` rather than `printf` in your code? – ufoxDan Nov 07 '19 at 15:55
  • Or perhaps you need a space in format, can you try: `printf("\r{:12}\r".format(' '))`? – ufoxDan Nov 07 '19 at 15:57
  • Still gives me the same output, @ufoxDan, sorry If this helps I am using this: [https://apps.apple.com/us/app/python3ide/id1357215444](https://apps.apple.com/us/app/python3ide/id1357215444) It is a python IDE since I can only use my iPad most of the day –  Nov 07 '19 at 16:49
  • The console in this app probably does not support the `\r` character then. I don't have any iOS devices so I can't play around with it, but if there are other apps they may work, and if you plan on running this script on a computer it should work! – ufoxDan Nov 07 '19 at 16:52
  • I just ran it in trinket and came across the same problem. I did find https://repl.it/languages/python3 and it worked there, so it looks like it really just varies by app/website. – ufoxDan Nov 07 '19 at 17:05
  • thank you! I will use that website and the IDLE on my computer. –  Nov 07 '19 at 18:04
  • I just ran it through Python 3.6.3 Shell for Windows and I got this answer `loading server... loading server...connected!` which worked in [repl.it](https://repl.it) but didn't in Python Shell, I'll show you the output from [repl](https://repl.it): first: `loading server...` and last: `connected!` BTW, the server connection is fake... –  Nov 08 '19 at 01:53
  • It seems that IDLE just doesn't support the carriage return. I would suggest for things outside of just learning and testing some quick code, to use a shell or IDE instead of IDLE. Powershell is easy to use and as long as you selected to have your Python installation added to your PATH on Windows it should run right away through Powershell. VSCode also supports running with an extension and there are other environments you can use too. – ufoxDan Nov 08 '19 at 05:54