Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,34 +2,74 @@ import streamlit as st
|
|
2 |
import requests
|
3 |
|
4 |
# Set up your API key (Replace 'YOUR_API_KEY' with your actual API key)
|
5 |
-
API_KEY = "YOUR_API_KEY" # Get
|
6 |
-
API_URL = "https://open.er-api.com/v6/latest/"
|
7 |
-
|
8 |
-
#
|
9 |
-
st.
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
#
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
#
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
try:
|
34 |
# Fetch exchange rates
|
35 |
response = requests.get(f"{API_URL}{base_currency}", params={"apiKey": API_KEY})
|
@@ -40,10 +80,21 @@ if st.button("Convert"):
|
|
40 |
exchange_rate = data["rates"].get(target_currency)
|
41 |
if exchange_rate:
|
42 |
result = amount * exchange_rate
|
43 |
-
|
44 |
else:
|
45 |
-
|
46 |
else:
|
47 |
-
|
48 |
except Exception as e:
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import requests
|
3 |
|
4 |
# Set up your API key (Replace 'YOUR_API_KEY' with your actual API key)
|
5 |
+
API_KEY = "YOUR_API_KEY" # Get your free key from ExchangeRate-API or Open Exchange Rates
|
6 |
+
API_URL = "https://open.er-api.com/v6/latest/"
|
7 |
+
|
8 |
+
# Custom CSS for better styling
|
9 |
+
st.markdown(
|
10 |
+
"""
|
11 |
+
<style>
|
12 |
+
.main {
|
13 |
+
background-color: #f7f9fc;
|
14 |
+
padding: 20px;
|
15 |
+
border-radius: 10px;
|
16 |
+
}
|
17 |
+
.title {
|
18 |
+
color: #4a90e2;
|
19 |
+
text-align: center;
|
20 |
+
font-size: 2.5rem;
|
21 |
+
font-weight: bold;
|
22 |
+
margin-bottom: 10px;
|
23 |
+
}
|
24 |
+
.instructions {
|
25 |
+
text-align: center;
|
26 |
+
font-size: 1.1rem;
|
27 |
+
color: #555;
|
28 |
+
}
|
29 |
+
.convert-btn {
|
30 |
+
background-color: #4a90e2;
|
31 |
+
color: white;
|
32 |
+
padding: 10px 20px;
|
33 |
+
border-radius: 5px;
|
34 |
+
border: none;
|
35 |
+
cursor: pointer;
|
36 |
+
}
|
37 |
+
.convert-btn:hover {
|
38 |
+
background-color: #357ABD;
|
39 |
+
}
|
40 |
+
</style>
|
41 |
+
""",
|
42 |
+
unsafe_allow_html=True,
|
43 |
+
)
|
44 |
+
|
45 |
+
# App title
|
46 |
+
st.markdown('<div class="title">💱 Currency Converter</div>', unsafe_allow_html=True)
|
47 |
+
st.markdown('<div class="instructions">Convert currencies easily with real-time exchange rates for 50+ currencies.</div>', unsafe_allow_html=True)
|
48 |
+
|
49 |
+
# Sidebar for user input
|
50 |
+
with st.sidebar:
|
51 |
+
st.header("Conversion Settings")
|
52 |
+
st.markdown("Select your desired options below.")
|
53 |
+
|
54 |
+
# Amount input
|
55 |
+
amount = st.number_input("Enter the amount to convert:", min_value=0.0, value=1.0, step=0.01)
|
56 |
+
|
57 |
+
# Currency selection
|
58 |
+
currencies = [
|
59 |
+
"USD", "EUR", "GBP", "INR", "JPY", "CNY", "AUD", "CAD", "CHF", "ZAR",
|
60 |
+
"SGD", "NZD", "MXN", "BRL", "RUB", "KRW", "HKD", "SEK", "NOK", "DKK",
|
61 |
+
"THB", "IDR", "TRY", "PLN", "MYR", "PHP", "ILS", "CZK", "HUF", "AED",
|
62 |
+
"SAR", "NGN", "EGP", "KZT", "CLP", "PKR", "BDT", "VND", "TWD", "COP",
|
63 |
+
"ARS", "PEN", "ISK", "BGN", "RON", "HRK", "LKR", "MAD", "OMR", "QAR"
|
64 |
+
]
|
65 |
+
base_currency = st.selectbox("From currency:", currencies)
|
66 |
+
target_currency = st.selectbox("To currency:", currencies)
|
67 |
+
|
68 |
+
# Layout for results
|
69 |
+
col1, col2 = st.columns(2)
|
70 |
+
|
71 |
+
# Fetch and display conversion results
|
72 |
+
if col1.button("Convert"):
|
73 |
try:
|
74 |
# Fetch exchange rates
|
75 |
response = requests.get(f"{API_URL}{base_currency}", params={"apiKey": API_KEY})
|
|
|
80 |
exchange_rate = data["rates"].get(target_currency)
|
81 |
if exchange_rate:
|
82 |
result = amount * exchange_rate
|
83 |
+
col2.success(f"{amount} {base_currency} = {result:.2f} {target_currency}")
|
84 |
else:
|
85 |
+
col2.error("The selected target currency is not supported.")
|
86 |
else:
|
87 |
+
col2.error("Failed to fetch exchange rates. Please try again later.")
|
88 |
except Exception as e:
|
89 |
+
col2.error(f"An error occurred: {e}")
|
90 |
+
|
91 |
+
# Footer
|
92 |
+
st.markdown(
|
93 |
+
"""
|
94 |
+
<div style="text-align: center; margin-top: 20px; color: #aaa;">
|
95 |
+
Powered by real-time exchange rates | © 2024 Currency Converter App
|
96 |
+
</div>
|
97 |
+
""",
|
98 |
+
unsafe_allow_html=True,
|
99 |
+
)
|
100 |
+
|