app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import streamlit as st
|
|
| 2 |
import requests
|
| 3 |
import pandas as pd
|
| 4 |
import plotly.graph_objs as go
|
| 5 |
-
import time
|
| 6 |
|
| 7 |
# Function to fetch historical price data from CoinGecko
|
| 8 |
def fetch_historical_data(coin_id, from_timestamp, to_timestamp):
|
|
@@ -14,7 +13,7 @@ def fetch_historical_data(coin_id, from_timestamp, to_timestamp):
|
|
| 14 |
prices = data.get('prices', [])
|
| 15 |
return prices
|
| 16 |
except requests.RequestException as e:
|
| 17 |
-
return f"Error fetching historical data
|
| 18 |
|
| 19 |
# Function to fetch current prices from CoinGecko
|
| 20 |
def fetch_current_price(coin_id):
|
|
@@ -25,7 +24,7 @@ def fetch_current_price(coin_id):
|
|
| 25 |
data = response.json()
|
| 26 |
return data.get(coin_id, {}).get('usd', 'N/A')
|
| 27 |
except requests.RequestException as e:
|
| 28 |
-
return f"Error fetching current price
|
| 29 |
|
| 30 |
# Function to convert dates to timestamps
|
| 31 |
def date_to_timestamp(date_str):
|
|
@@ -38,7 +37,7 @@ def plot_historical_prices(coin_name, from_date, to_date):
|
|
| 38 |
|
| 39 |
prices = fetch_historical_data(coin_name, from_timestamp, to_timestamp)
|
| 40 |
|
| 41 |
-
if isinstance(prices, str): #
|
| 42 |
return prices
|
| 43 |
|
| 44 |
try:
|
|
@@ -109,7 +108,7 @@ def fetch_real_time_trade_data(coin_id):
|
|
| 109 |
return prices[-1] # Latest price data
|
| 110 |
return "No real-time data available"
|
| 111 |
except requests.RequestException as e:
|
| 112 |
-
return f"Error fetching real-time trade data
|
| 113 |
|
| 114 |
# Streamlit App Layout
|
| 115 |
st.title("Cryptocurrency Dashboard")
|
|
@@ -132,7 +131,7 @@ if tab == "Price Chart":
|
|
| 132 |
|
| 133 |
if st.button("Plot Chart"):
|
| 134 |
fig = plot_historical_prices(selected_crypto, from_date, to_date)
|
| 135 |
-
if isinstance(fig, str): #
|
| 136 |
st.error(fig)
|
| 137 |
else:
|
| 138 |
st.plotly_chart(fig)
|
|
@@ -145,7 +144,7 @@ elif tab == "Current Price":
|
|
| 145 |
elif tab == "Real-Time Trade Data":
|
| 146 |
st.header("Real-Time Cryptocurrency Trade Data")
|
| 147 |
data = fetch_real_time_trade_data(selected_crypto)
|
| 148 |
-
if isinstance(data, str): #
|
| 149 |
st.error(data)
|
| 150 |
else:
|
| 151 |
st.write(f"The latest trading data for {selected_crypto.capitalize()} is: ${data[1]}")
|
|
|
|
| 2 |
import requests
|
| 3 |
import pandas as pd
|
| 4 |
import plotly.graph_objs as go
|
|
|
|
| 5 |
|
| 6 |
# Function to fetch historical price data from CoinGecko
|
| 7 |
def fetch_historical_data(coin_id, from_timestamp, to_timestamp):
|
|
|
|
| 13 |
prices = data.get('prices', [])
|
| 14 |
return prices
|
| 15 |
except requests.RequestException as e:
|
| 16 |
+
return f"Error fetching historical data: {e}"
|
| 17 |
|
| 18 |
# Function to fetch current prices from CoinGecko
|
| 19 |
def fetch_current_price(coin_id):
|
|
|
|
| 24 |
data = response.json()
|
| 25 |
return data.get(coin_id, {}).get('usd', 'N/A')
|
| 26 |
except requests.RequestException as e:
|
| 27 |
+
return f"Error fetching current price: {e}"
|
| 28 |
|
| 29 |
# Function to convert dates to timestamps
|
| 30 |
def date_to_timestamp(date_str):
|
|
|
|
| 37 |
|
| 38 |
prices = fetch_historical_data(coin_name, from_timestamp, to_timestamp)
|
| 39 |
|
| 40 |
+
if isinstance(prices, str): # Error fetching data
|
| 41 |
return prices
|
| 42 |
|
| 43 |
try:
|
|
|
|
| 108 |
return prices[-1] # Latest price data
|
| 109 |
return "No real-time data available"
|
| 110 |
except requests.RequestException as e:
|
| 111 |
+
return f"Error fetching real-time trade data: {e}"
|
| 112 |
|
| 113 |
# Streamlit App Layout
|
| 114 |
st.title("Cryptocurrency Dashboard")
|
|
|
|
| 131 |
|
| 132 |
if st.button("Plot Chart"):
|
| 133 |
fig = plot_historical_prices(selected_crypto, from_date, to_date)
|
| 134 |
+
if isinstance(fig, str): # Error plotting data
|
| 135 |
st.error(fig)
|
| 136 |
else:
|
| 137 |
st.plotly_chart(fig)
|
|
|
|
| 144 |
elif tab == "Real-Time Trade Data":
|
| 145 |
st.header("Real-Time Cryptocurrency Trade Data")
|
| 146 |
data = fetch_real_time_trade_data(selected_crypto)
|
| 147 |
+
if isinstance(data, str): # Error fetching real-time data
|
| 148 |
st.error(data)
|
| 149 |
else:
|
| 150 |
st.write(f"The latest trading data for {selected_crypto.capitalize()} is: ${data[1]}")
|