Spaces:
Sleeping
Sleeping
import streamlit as st | |
import requests | |
# Function to fetch exchange rates for multiple currencies to PKR | |
def get_exchange_rates_to_pkr(): | |
try: | |
url = "https://api.exchangerate-api.com/v4/latest/PKR" | |
response = requests.get(url) | |
response.raise_for_status() | |
data = response.json() | |
return data["rates"] | |
except Exception as e: | |
st.error(f"Error fetching exchange rates: {e}") | |
return {} | |
def main(): | |
# App title and icon | |
st.set_page_config(page_title="Currency to PKR Converter", page_icon="π°") | |
# Background styling | |
st.markdown( | |
""" | |
<style> | |
body { | |
background-color: #f5f5f5; | |
font-family: Arial, sans-serif; | |
} | |
.stApp { | |
background-color: #e0f7fa; | |
border-radius: 10px; | |
padding: 20px; | |
} | |
</style> | |
""", | |
unsafe_allow_html=True, | |
) | |
st.title("π° Currency to PKR Converter") | |
st.write("Convert amounts from multiple currencies to Pakistani Rupees (PKR).") | |
# Currencies with country names | |
currencies = { | |
"USD": "United States Dollar", | |
"EUR": "Euro (European Union)", | |
"GBP": "British Pound Sterling", | |
"AED": "United Arab Emirates Dirham", | |
"SAR": "Saudi Riyal", | |
"KWD": "Kuwaiti Dinar", | |
"TRY": "Turkish Lira", | |
"JPY": "Japanese Yen", | |
"AUD": "Australian Dollar", | |
"CAD": "Canadian Dollar", | |
"CNY": "Chinese Yuan", | |
"INR": "Indian Rupee", | |
"CHF": "Swiss Franc", | |
"SEK": "Swedish Krona", | |
} | |
# Fetch exchange rates | |
rates = get_exchange_rates_to_pkr() | |
if rates: | |
# Input fields for each currency | |
for code, name in currencies.items(): | |
amount = st.number_input(f"Enter the amount in {name} ({code}):", min_value=0.0, format="%.2f") | |
if amount > 0: | |
if code in rates: | |
converted = amount / rates[code] | |
st.success(f"{amount:.2f} {code} ({name}) = {converted:.2f} PKR") | |
else: | |
st.warning(f"Exchange rate for {code} ({name}) is not available.") | |
else: | |
st.error("Failed to load exchange rates. Please refresh or try again later.") | |
if __name__ == "__main__": | |
main() | |