1

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:\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)
smac89
  • 39,374
  • 15
  • 132
  • 179
  • 1
    Use a `while-loop`? – smac89 Sep 08 '22 at 16:34
  • Use a carriage return, `\r` – Mad Physicist Sep 08 '22 at 16:34
  • I tried the while-loop but it keeps printing the lines all over, I wannt print the lines once and update the values with the api – scarletspeedster Sep 08 '22 at 16:35
  • Did you try store previous result for each currency and do `if value_changed: print("new value")? – kosciej16 Sep 08 '22 at 16:39
  • @MadPhysicist I tried using \r but it's not helping, it prints 1 time, I wanna print the lines with the currency names once, and print the updated values with the \r and keep that running, but if I loop it, it prints the lines all over – scarletspeedster Sep 08 '22 at 16:40
  • @kosciej16 the API is updating the values in real time already, they have that stored, I'm just using json to import the real time data – scarletspeedster Sep 08 '22 at 16:41
  • How do you run that? What operating system? – kosciej16 Sep 08 '22 at 16:42
  • @kosciej16 I'm running just the back-end for me to test the api, but since I'm already here I wanna make it run for me in my terminal (windows) and keep me updating the currency values in real time – scarletspeedster Sep 08 '22 at 16:44
  • What "terminal" is this? I think what you are wanting to do is just run this code in a loop and clear the "terminal" each time? Like [this](https://stackoverflow.com/questions/2084508/clear-terminal-in-python). – JNevill Sep 08 '22 at 16:46
  • @JNevill I wanna run it a loop but just the currency values, the api already provides me that, but I wanna run the lines with the currency names just once – scarletspeedster Sep 08 '22 at 16:48
  • Right. So you write out what you have, then you clear the terminal and write it again with new values. To a user it will look like only the numbers are changing, not the currency. That's how this is done (or at least, how it's done the easy way). – JNevill Sep 08 '22 at 16:51

1 Answers1

1

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)
kosciej16
  • 6,294
  • 1
  • 18
  • 29