3

I have looked around at some of the questions already asked but none of them seem to fit my situation. So I have a very basic program the shows me the current value of bitcoin:

import requests
import os
import sys
import time
while True:
    main_api = ('https://api.coindesk.com/v1/bpi/currentprice.json')
    json_data = requests.get(main_api).json()
    json_updated = json_data['time']['updated']
    json_value = json_data['bpi']['USD']['rate']
    time.sleep(1)
    print('\n' 'Last Updated: ' + json_updated)
    print('\n' "Bitcoin price: " + json_value + " USD")

and it works well for the most part. But there is a small problem, every time the code executes itself (every second) it will create more text in the terminal displaying the currency and the last time it was updated. But It will not remove the previous text which makes it look ugly and a bit confusing so I aim to make it look like, in the terminal, that there is only once instance of the text that updates itself, giving a much more cleaner look.

I have seen some solutions like:

for x in range(10):
    print(x, end='\r')
print()

from How to overwrite the previous print to stdout in python?

and

import time
for x in range (0,5):  
    b = "Loading" + "." * x
    print (b, end="\r")
    time.sleep(1)

from Remove and Replace Printed items

But I honestly have no idea how or IF I can incorporate those solutions into my own code since my program is much more different from the ones used in the solutions or I am just I noob, probably the latter lol.

Thanks.

888
  • 53
  • 5
  • The easiest way to overwrite a **single** line `print` statement, is using the `\r` carriage at the start and **no** `\n` (newline) at the end. For **multi** line, you can take a look at [curses](https://docs.python.org/3/howto/curses.html) or [this](https://stackoverflow.com/questions/6840420/rewrite-multiple-lines-in-the-console) post. – Thymen Jan 02 '21 at 11:30
  • @Thymen I just tried this with a single line: `print('\r' 'Last Updated: ' + json_updated)` but when I ran it nothing changed at all? Maybe I misunderstood what you meant by "using the `\r` carriage at the start" I am not sure. – 888 Jan 02 '21 at 11:49
  • Does this answer your question? [Rewrite multiple lines in the console](https://stackoverflow.com/questions/6840420/rewrite-multiple-lines-in-the-console) – outis Jan 02 '21 at 12:23

2 Answers2

2

This is a parcial solution, because there is no simple way of including a newline.

import sys
import time
import requests
while True:
    main_api = ('https://api.coindesk.com/v1/bpi/currentprice.json')
    json_data = requests.get(main_api).json()
    json_updated = json_data['time']['updated']
    json_value = json_data['bpi']['USD']['rate']
    b ="Bitcoin price: " + json_value + " USD"
    sys.stdout.write('\r' + b)
    time.sleep(1)
Josh Merrian
  • 291
  • 3
  • 11
  • thanks for this but I aim to also have the time updated too. If I don't have a full solution soon, I will mark yours as solved. Thanks again. – 888 Jan 02 '21 at 11:58
2

Here is a solution that updates both time, and currency. It's by simply clearing the terminal window before each time you get updated data.

import requests
import os
import sys
import time
def clear():
    if sys.platform=="win32":
        os.system("cls") # cmd clear command for Windows systems
    elif sys.platform in ["linux", "darwin"]: 
        os.system("clear") # terminal clear command for Linux and Mac OS
    else:
        raise OSError("Uncompatible Operating-System.")
while True:
    main_api = ('https://api.coindesk.com/v1/bpi/currentprice.json')
    json_data = requests.get(main_api).json()
    json_updated = json_data['time']['updated']
    json_value = json_data['bpi']['USD']['rate']
    time.sleep(1)
    clear()
    print('\n' 'Last Updated: ' + json_updated)
    print('\n' "Bitcoin price: " + json_value + " USD")

You can also add a line for the current hour if it looks freezed.

import requests
import os
import sys
import time
from datetime import datetime
def clear():
    if sys.platform=="win32":
        os.system("cls") # cmd clear command for Windows systems
    elif sys.platform in ["linux", "darwin"]: 
        os.system("clear") # terminal clear command for Linux and Mac OS
    else:
        raise OSError("Uncompatible Operating-System.")
while True:
    main_api = ('https://api.coindesk.com/v1/bpi/currentprice.json')
    json_data = requests.get(main_api).json()
    json_updated = json_data['time']['updated']
    json_value = json_data['bpi']['USD']['rate']
    time.sleep(1)
    clear()
    print('\n' "Current date and time :", str(datetime.now())[:-7])
    print('\n' 'Last Updated: ' + json_updated)
    print('\n' "Bitcoin price: " + json_value + " USD")

It should help, unless you don't want your screen cleared.