abdullahshoaib5616's picture
Create app.py
e27b572 verified
import streamlit as st
import requests
# Function to fetch exchange rates
def get_exchange_rates():
try:
response = requests.get('https://api.frankfurter.app/latest')
data = response.json()
return data['rates']
except Exception as e:
st.error(f"Error fetching exchange rates: {e}")
return None
# Dictionary of 50 currencies with symbols
currencies = {
'USD': 'US Dollar ($)', 'EUR': 'Euro (€)', 'GBP': 'British Pound (£)', 'JPY': 'Japanese Yen (¥)',
'AUD': 'Australian Dollar (A$)', 'CAD': 'Canadian Dollar (C$)', 'CHF': 'Swiss Franc (CHF)',
'CNY': 'Chinese Yuan (¥)', 'SEK': 'Swedish Krona (kr)', 'NZD': 'New Zealand Dollar (NZ$)',
'MXN': 'Mexican Peso (Mex$)', 'SGD': 'Singapore Dollar (S$)', 'HKD': 'Hong Kong Dollar (HK$)',
'NOK': 'Norwegian Krone (kr)', 'KRW': 'South Korean Won (₩)', 'TRY': 'Turkish Lira (₺)',
'INR': 'Indian Rupee (₹)', 'RUB': 'Russian Ruble (₽)', 'BRL': 'Brazilian Real (R$)',
'ZAR': 'South African Rand (R)', 'PLN': 'Polish Złoty (zł)', 'PHP': 'Philippine Peso (₱)',
'IDR': 'Indonesian Rupiah (Rp)', 'MYR': 'Malaysian Ringgit (RM)', 'THB': 'Thai Baht (฿)',
'HUF': 'Hungarian Forint (Ft)', 'CZK': 'Czech Koruna (Kč)', 'ILS': 'Israeli Shekel (₪)',
'PKR': 'Pakistani Rupee (Rs)', 'AED': 'UAE Dirham (د.إ)', 'SAR': 'Saudi Riyal (﷼)',
'EGP': 'Egyptian Pound (E£)', 'BDT': 'Bangladeshi Taka (৳)', 'LKR': 'Sri Lankan Rupee (Rs)',
'VND': 'Vietnamese Dong (₫)', 'COP': 'Colombian Peso (Col$)', 'ARS': 'Argentine Peso (AR$)',
'NGN': 'Nigerian Naira (₦)', 'KES': 'Kenyan Shilling (KSh)', 'GHS': 'Ghanaian Cedi (GH₵)',
'RON': 'Romanian Leu (lei)', 'DZD': 'Algerian Dinar (دج)', 'QAR': 'Qatari Riyal (ر.ق)',
'BHD': 'Bahraini Dinar (BD)', 'OMR': 'Omani Rial (ر.ع.)', 'KWD': 'Kuwaiti Dinar (KD)',
'JOD': 'Jordanian Dinar (JD)', 'MAD': 'Moroccan Dirham (د.م.)', 'TWD': 'New Taiwan Dollar (NT$)'
}
st.title("🌍 Currency Converter 💰")
# Get exchange rates
exchange_rates = get_exchange_rates()
if exchange_rates:
# Sidebar
st.sidebar.header("Currency Conversion Settings")
# Amount input
amount = st.sidebar.number_input("Enter amount:", min_value=0.01, value=1.0, format="%.2f")
# Select currencies
from_currency = st.sidebar.selectbox("From Currency:", list(currencies.keys()), format_func=lambda x: f"{x} - {currencies[x]}")
to_currency = st.sidebar.selectbox("To Currency:", list(currencies.keys()), format_func=lambda x: f"{x} - {currencies[x]}")
# Convert currency
if from_currency == "EUR":
base_amount = amount
else:
base_amount = amount / exchange_rates[from_currency]
converted_amount = base_amount * exchange_rates[to_currency]
# Display result
st.success(f"💱 {amount} {currencies[from_currency]} = {converted_amount:.2f} {currencies[to_currency]}")
else:
st.error("Unable to fetch exchange rates. Please try again later.")