0o7Hunk's picture
Update app.py
9ac64bd verified
import streamlit as st
import requests
# Page setings
st.set_page_config(page_title="Currency Converter", page_icon="💱")
# Title
st.title("💰 Currency Converter")
# Function to fetch exchange rates
@st.cache_data
def get_rates(base="USD"):
url = f"https://api.exchangerate-api.com/v4/latest/{base}"
response = requests.get(url)
if response.status_code != 200:
return None
data = response.json()
return data["rates"]
# UI Inputs
amount = st.number_input("Enter Amount", min_value=0.0, value=1.0)
from_currency = st.text_input("From Currency (e.g., USD)", value="USD").upper()
to_currency = st.text_input("To Currency (e.g., PKR)", value="PKR").upper()
# Convert Button
if st.button("Convert"):
rates = get_rates(from_currency)
if rates is None:
st.error("Failed to fetch exchange rates.")
elif to_currency not in rates:
st.error("Invalid currency code.")
else:
result = amount * rates[to_currency]
st.success(f"{amount} {from_currency} = {round(result, 2)} {to_currency}")