File size: 1,264 Bytes
d56614c
 
b42a644
ab7eb67
c16abe6
b42a644
 
d56614c
 
b42a644
d56614c
 
b42a644
d56614c
b42a644
d56614c
b42a644
 
d56614c
b42a644
 
d56614c
b42a644
 
 
 
 
d56614c
b42a644
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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