import streamlit as st
import requests
# Set up your API key (Replace 'YOUR_API_KEY' with your actual API key)
API_KEY = "YOUR_API_KEY" # Get your free key from ExchangeRate-API or Open Exchange Rates
API_URL = "https://open.er-api.com/v6/latest/"
# Custom CSS for better styling
st.markdown(
"""
""",
unsafe_allow_html=True,
)
# App title
st.markdown('
💱 Currency Converter
', unsafe_allow_html=True)
st.markdown('Convert currencies easily with real-time exchange rates for 50+ currencies.
', unsafe_allow_html=True)
# Sidebar for user input
with st.sidebar:
st.header("Conversion Settings")
st.markdown("Select your desired options below.")
# Amount input
amount = st.number_input("Enter the amount to convert:", min_value=0.0, value=1.0, step=0.01)
# Currency selection
currencies = [
"USD", "EUR", "GBP", "INR", "JPY", "CNY", "AUD", "CAD", "CHF", "ZAR",
"SGD", "NZD", "MXN", "BRL", "RUB", "KRW", "HKD", "SEK", "NOK", "DKK",
"THB", "IDR", "TRY", "PLN", "MYR", "PHP", "ILS", "CZK", "HUF", "AED",
"SAR", "NGN", "EGP", "KZT", "CLP", "PKR", "BDT", "VND", "TWD", "COP",
"ARS", "PEN", "ISK", "BGN", "RON", "HRK", "LKR", "MAD", "OMR", "QAR"
]
base_currency = st.selectbox("From currency:", currencies)
target_currency = st.selectbox("To currency:", currencies)
# Layout for results
col1, col2 = st.columns(2)
# Fetch and display conversion results
if col1.button("Convert"):
try:
# Fetch exchange rates
response = requests.get(f"{API_URL}{base_currency}", params={"apiKey": API_KEY})
data = response.json()
# Check for valid response
if response.status_code == 200 and "rates" in data:
exchange_rate = data["rates"].get(target_currency)
if exchange_rate:
result = amount * exchange_rate
col2.success(f"{amount} {base_currency} = {result:.2f} {target_currency}")
else:
col2.error("The selected target currency is not supported.")
else:
col2.error("Failed to fetch exchange rates. Please try again later.")
except Exception as e:
col2.error(f"An error occurred: {e}")
# Footer
st.markdown(
"""
Powered by real-time exchange rates | © 2024 Currency Converter App
""",
unsafe_allow_html=True,
)