ai-lover's picture
Create app.py
ca6e954 verified
import streamlit as st
import requests
from datetime import datetime
# Fetch live exchange rates from an API (ExchangeRate-API)
def fetch_exchange_rates(base_currency="PKR"):
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="PKR 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("PKR Currency Converter")
# Sidebar - Description
st.sidebar.markdown("""
This app allows you to convert **Pakistani Rupees (PKR)** to any other currency.
Simply enter the amount in PKR, select the target currency, and click 'Convert'.
""")
# Sidebar - Currency list (converting from PKR to other popular currencies)
currency_list = ['USD', 'EUR', 'GBP', 'INR', 'AUD', 'CAD', 'JPY', 'CNY', 'CHF', 'MXN', 'ZAR', 'RUB', 'BRL', 'NZD']
# Main title
st.title("PKR Currency Conversion App")
# Set a light background color for the main app
st.markdown("""
<style>
.reportview-container {
background-color: #f0f8ff; /* Light background */
}
.sidebar .sidebar-content {
background-color: #f0f8ff; /* 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 in Pakistani Rupees (PKR)", min_value=0.0, format="%.2f", value=0.0)
# Select target currency
to_currency = st.selectbox("Convert to", currency_list)
# Fetch exchange rates for PKR
rates = fetch_exchange_rates("PKR")
# Show conversion button and output
if st.button("Convert"):
if amount > 0:
converted_amount = convert_currency(amount, "PKR", to_currency, rates)
st.success(f"{amount} PKR = {converted_amount:.2f} {to_currency}")
else:
st.warning("Please enter a valid amount in PKR.")
# Footer (optional)
st.markdown("""
---
<p style="font-size:14px; text-align:center;">Made with ❤️ using Streamlit</p>
""", unsafe_allow_html=True)