Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
|
4 |
+
def get_crypto_price(crypto_symbol):
|
5 |
+
url = f"https://api.binance.com/api/v3/ticker/price?symbol={crypto_symbol}USDT"
|
6 |
+
try:
|
7 |
+
response = requests.get(url)
|
8 |
+
response.raise_for_status()
|
9 |
+
data = response.json()
|
10 |
+
return float(data['price'])
|
11 |
+
except requests.exceptions.RequestException as e:
|
12 |
+
st.error(f"Error fetching data: {e}")
|
13 |
+
return None
|
14 |
+
except KeyError:
|
15 |
+
st.error(f"Invalid cryptocurrency symbol: {crypto_symbol}")
|
16 |
+
return None
|
17 |
+
|
18 |
+
def get_available_cryptos():
|
19 |
+
url = "https://api.binance.com/api/v3/exchangeInfo"
|
20 |
+
try:
|
21 |
+
response = requests.get(url)
|
22 |
+
response.raise_for_status()
|
23 |
+
data = response.json()
|
24 |
+
cryptos = [symbol['baseAsset'] for symbol in data['symbols'] if symbol['quoteAsset'] == 'USDT']
|
25 |
+
return sorted(cryptos)
|
26 |
+
except requests.exceptions.RequestException as e:
|
27 |
+
st.error(f"Error fetching cryptocurrency list: {e}")
|
28 |
+
return []
|
29 |
+
|
30 |
+
st.set_page_config(page_title="Crypto to USDT Converter", page_icon="💰")
|
31 |
+
|
32 |
+
st.title("💰 Crypto to USDT Converter")
|
33 |
+
st.write("Get real-time cryptocurrency prices from Binance.")
|
34 |
+
|
35 |
+
available_cryptos = get_available_cryptos()
|
36 |
+
|
37 |
+
if available_cryptos:
|
38 |
+
selected_crypto = st.selectbox("Select Cryptocurrency:", available_cryptos)
|
39 |
+
amount = st.number_input(f"Enter amount of {selected_crypto} to convert:", min_value=0.0, step=0.01)
|
40 |
+
|
41 |
+
if st.button("Convert to USDT"):
|
42 |
+
with st.spinner("Fetching price..."):
|
43 |
+
crypto_price = get_crypto_price(selected_crypto)
|
44 |
+
if crypto_price is not None:
|
45 |
+
total_usdt = crypto_price * amount
|
46 |
+
st.success(f"Current price of {selected_crypto}: {crypto_price:.2f} USDT")
|
47 |
+
st.success(f"{amount} {selected_crypto} is equivalent to {total_usdt:.2f} USDT")
|
48 |
+
else:
|
49 |
+
st.warning("Unable to fetch the price at this time. Please try again later.")
|
50 |
+
else:
|
51 |
+
st.warning("No cryptocurrencies available for conversion at this time.")
|
52 |
+
|
53 |
+
st.markdown("#### 📌 Note:")
|
54 |
+
st.markdown("Prices are fetched in real-time from the Binance API.")
|