import streamlit as st import requests import time from datetime import datetime # Function to fetch real-time data from CoinGecko def fetch_real_time_data(coin_id): url = f"https://api.coingecko.com/api/v3/simple/price?ids={coin_id}&vs_currencies=usd" try: response = requests.get(url) response.raise_for_status() data = response.json() return data except requests.RequestException as e: return {"error": str(e)} # Streamlit app st.title("Real-Time Cryptocurrency Data") # Input for selecting cryptocurrency coin = st.selectbox("Select Cryptocurrency", ["bitcoin", "ethereum", "binancecoin"]) # Button to fetch data if st.button("Fetch Data"): data = fetch_real_time_data(coin) if "error" in data: st.error(f"Error: {data['error']}") else: st.write(f"The current price of {coin.capitalize()} is: ${data[coin]['usd']}") # Periodic updates st.write("Updating data every 5 minutes...") while True: data = fetch_real_time_data(coin) if "error" in data: st.error(f"Error: {data['error']}") else: st.write(f"Last updated at {datetime.now()}: The current price of {coin.capitalize()} is: ${data[coin]['usd']}") time.sleep(300) # Update every 5 minutes