File size: 3,036 Bytes
6931cf5
 
 
 
18d905e
6931cf5
18d905e
6931cf5
 
 
18d905e
6931cf5
 
 
 
 
 
 
 
 
 
 
 
 
18d905e
6931cf5
 
 
 
 
 
 
 
 
 
 
18d905e
6931cf5
 
 
18d905e
 
6931cf5
 
 
 
 
 
 
 
18d905e
6931cf5
 
18d905e
6931cf5
 
18d905e
6931cf5
 
 
 
 
 
 
18d905e
6931cf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import streamlit as st
import requests
from datetime import datetime

# Fetch live exchange rates
def fetch_exchange_rates(base_currency="USD"):
    url = f"https://api.exchangerate-api.com/v4/latest/{base_currency}"
    response = requests.get(url)
    data = response.json()
    if response.status_code == 200:
        return data['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.
    Simply select the currencies, enter the amount, and click 'Convert'.
""")

# Sidebar - Currency list (use the most popular currencies)
currency_list = ['USD', 'EUR', 'GBP', 'INR', 'AUD', 'CAD', 'JPY', 'CNY', 'CHF', 'MXN']

# Main title
st.title("Currency Conversion App")

# Set a light background color for the main app
st.markdown("""
    <style>
    .reportview-container {
        background-color: #f0f8ff;  /* Light Blue background */
    }
    .sidebar .sidebar-content {
        background-color: #f0f8ff;  /* Light Blue for sidebar */
    }
    .stButton>button {
        background-color: #008CBA;
        color: white;
        font-size: 16px;
        border-radius: 5px;
        padding: 10px;
        border: none;
    }
    .stButton>button:hover {
        background-color: #005f6a;
    }
    </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)