Update app.py
Browse files
app.py
CHANGED
@@ -10,26 +10,28 @@ def get_available_cryptos():
|
|
10 |
response = requests.get(url)
|
11 |
response.raise_for_status()
|
12 |
data = response.json()
|
13 |
-
# Create a dictionary to map
|
14 |
-
cryptos = {coin["
|
15 |
return cryptos
|
16 |
except requests.exceptions.RequestException as e:
|
17 |
st.error(f"Error fetching cryptocurrency list: {e}")
|
18 |
return {}
|
19 |
|
20 |
-
# Function to get current price of the selected cryptocurrency in USD
|
21 |
def get_crypto_price(crypto_id):
|
22 |
url = f"https://api.coingecko.com/api/v3/simple/price?ids={crypto_id}&vs_currencies=usd"
|
23 |
try:
|
24 |
response = requests.get(url)
|
25 |
response.raise_for_status()
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
27 |
except requests.exceptions.RequestException as e:
|
28 |
st.error(f"Error fetching data: {e}")
|
29 |
return None
|
30 |
-
except KeyError:
|
31 |
-
st.error(f"Invalid cryptocurrency symbol: {crypto_id}")
|
32 |
-
return None
|
33 |
|
34 |
# Streamlit app layout
|
35 |
st.title("Crypto to USD Converter")
|
|
|
10 |
response = requests.get(url)
|
11 |
response.raise_for_status()
|
12 |
data = response.json()
|
13 |
+
# Create a dictionary to map lowercase IDs to their uppercase symbols
|
14 |
+
cryptos = {coin["id"]: coin["symbol"].upper() for coin in data}
|
15 |
return cryptos
|
16 |
except requests.exceptions.RequestException as e:
|
17 |
st.error(f"Error fetching cryptocurrency list: {e}")
|
18 |
return {}
|
19 |
|
20 |
+
# Function to get the current price of the selected cryptocurrency in USD
|
21 |
def get_crypto_price(crypto_id):
|
22 |
url = f"https://api.coingecko.com/api/v3/simple/price?ids={crypto_id}&vs_currencies=usd"
|
23 |
try:
|
24 |
response = requests.get(url)
|
25 |
response.raise_for_status()
|
26 |
+
price_data = response.json()
|
27 |
+
if crypto_id in price_data:
|
28 |
+
return price_data[crypto_id]['usd']
|
29 |
+
else:
|
30 |
+
st.error(f"Invalid cryptocurrency ID: {crypto_id}")
|
31 |
+
return None
|
32 |
except requests.exceptions.RequestException as e:
|
33 |
st.error(f"Error fetching data: {e}")
|
34 |
return None
|
|
|
|
|
|
|
35 |
|
36 |
# Streamlit app layout
|
37 |
st.title("Crypto to USD Converter")
|