ai-lover's picture
Update app.py
f3e4ad3 verified
raw
history blame
3.15 kB
import streamlit as st
import requests
from datetime import datetime
# Fetch live exchange rates from an API
def fetch_exchange_rates(base_currency="USD"):
url = f"https://v6.exchangerate-api.com/v6/YOUR_API_KEY/latest/{base_currency}"
response = requests.get(url)
data = response.json()
if response.status_code == 200:
return data['conversion_rates']
else:
st.error("Error fetching data. Please try again later.")
return {}
# Function to convert currencies
def convert_currency(amount, from_currency, to_currency, rates):
if from_currency == to_currency:
return amount
amount_in_base_currency = amount / rates[from_currency]
converted_amount = amount_in_base_currency * rates[to_currency]
return converted_amount
# Set page config
st.set_page_config(page_title="Currency Converter", page_icon="💸", layout="wide")
# Sidebar - Display current date
now = datetime.now()
current_date = now.strftime("%Y-%m-%d")
st.sidebar.markdown(f"### Current Date: {current_date}")
# Sidebar - Title
st.sidebar.title("Currency Converter")
# Sidebar - Description
st.sidebar.markdown("""
This app allows you to convert between different currencies, including Pakistani Rupees (PKR).
Simply select the currencies, enter the amount, and click 'Convert'.
""")
# Sidebar - Currency list (includes popular and some less common currencies)
currency_list = ['USD', 'EUR', 'GBP', 'INR', 'AUD', 'CAD', 'JPY', 'CNY', 'CHF', 'MXN', 'PKR', 'ZAR', 'RUB', 'BRL', 'NZD']
# Main title
st.title("Currency Conversion App")
# Set a light background color for the main app
st.markdown("""
<style>
.reportview-container {
background-color: #f9f9f9; /* Light background */
}
.sidebar .sidebar-content {
background-color: #f9f9f9; /* Sidebar background color */
}
.stButton>button {
background-color: #4CAF50;
color: white;
font-size: 16px;
border-radius: 5px;
padding: 10px;
border: none;
}
.stButton>button:hover {
background-color: #45a049;
}
</style>
""", unsafe_allow_html=True)
# Input: Amount to convert
amount = st.number_input("Enter amount to convert", min_value=0.0, format="%.2f", value=0.0)
# Select currencies
from_currency = st.selectbox("Convert from", currency_list)
to_currency = st.selectbox("Convert to", currency_list)
# Fetch exchange rates for the selected base currency
rates = fetch_exchange_rates(from_currency)
# Show conversion button and output
if st.button("Convert"):
if amount > 0:
if from_currency != to_currency:
converted_amount = convert_currency(amount, from_currency, to_currency, rates)
st.success(f"{amount} {from_currency} = {converted_amount:.2f} {to_currency}")
else:
st.warning("Both currencies are the same. Please choose different currencies.")
else:
st.warning("Please enter a valid amount to convert.")
# Footer (optional)
st.markdown("""
---
<p style="font-size:14px; text-align:center;">Made with ❤️ using Streamlit</p>
""", unsafe_allow_html=True)