Terminal Running
I'm making a Python code to get me the real timevalues on some currencies to test the API, how do I keep it runnning and printing just the updated value, but without print the lines all over again, basically printing the currency names one time and keep updating the values without closing the terminal.
Here's my code:
import requests
import json
import colored
from colored import fg
print("Valor atualizado:\n\n")
cotacoes = requests.get("https://economia.awesomeapi.com.br/last/USD-BRL,EUR-BRL,BTC-BRL,ETH-BRL")
cotacoes = cotacoes.json()
dolar = cotacoes['USDBRL']["bid"]
euro = cotacoes['EURBRL']["bid"]
bitcoin = cotacoes['BTCBRL']["bid"]
eth = cotacoes['ETHBRL']["bid"]
red = fg('red')
blue = fg('blue')
yellow = fg('yellow')
green = fg('green')
white = fg('white')
print(green "Dólar:", dolar,"reais")
print(red "Euro:", euro, "reais")
print(yellow "Bitcoin:", bitcoin, "reais")
print(blue "Etherium:", eth,"reais")
print(white)
CodePudding user response:
Generally to override already written text you need to do some cursor positioning, but the simplest way is to clear the terminal in every iteration.
import os
import time
while True:
os.system('cls' if os.name == 'nt' else 'clear') # clears terminal
<get needed values>
<print everything>
time.sleep(1)