Spaces:
Sleeping
Sleeping
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 a free key from ExchangeRate-API or Open Exchange Rates | |
API_URL = "https://open.er-api.com/v6/latest/" # Example API endpoint | |
# App title and description | |
st.title("π± Currency Converter") | |
st.markdown(""" | |
This app allows you to convert between 50+ currencies in real-time. | |
Exchange rates are fetched live from the currency exchange API. | |
""") | |
# Input amount to convert | |
amount = st.number_input("Enter amount to convert:", min_value=0.0, value=1.0, step=0.01) | |
# List of currencies | |
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" | |
] | |
# Select base and target currencies | |
base_currency = st.selectbox("From currency:", currencies) | |
target_currency = st.selectbox("To currency:", currencies) | |
# Convert button | |
if st.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 | |
st.success(f"{amount} {base_currency} = {result:.2f} {target_currency}") | |
else: | |
st.error("The selected target currency is not supported.") | |
else: | |
st.error("Failed to fetch exchange rates. Please try again later.") | |
except Exception as e: | |
st.error(f"An error occurred: {e}") | |