File size: 1,957 Bytes
263323e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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}")