Live Updates on Python

Question:

Terminal Running
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:nn")

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)
Asked By: scarletspeedster

||

Answers:

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)
Answered By: kosciej16
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.