Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Live Updates on Python

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading