PEC / app.py
IamZeeshan's picture
Update app.py
3b6bf55 verified
Raw History Blame Contribute Delete
2.83 kB
import streamlit as st
import requests
from datetime import datetime
# Set your API key and base URL
API_KEY = 'a601ff9a6cf145370a75b370' # Ensure you replace this with your actual API key
BASE_URL = "https://v6.exchangerate-api.com/v6/"
def get_exchange_rates(from_currency, to_currency):
"""Fetch the exchange rates from the API and calculate buy/sell rates."""
url = f"{BASE_URL}{API_KEY}/pair/{from_currency}/{to_currency}"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
if data['result'] == 'success':
base_rate = data['conversion_rate']
date = data.get('time_last_update_utc', 'No date available').split(' ')[0]
buy_rate = base_rate * 1.005
sell_rate = base_rate * 0.995
return base_rate, buy_rate, sell_rate, date
else:
st.error(f"API Error: {data.get('error-type', 'Unknown Error')}")
return None, None, None, None
else:
st.error(f"Failed to retrieve data: HTTP Status {response.status_code}")
return None, None, None, None
def load_css():
"""Load CSS to style the app."""
st.markdown("""
<style>
.big-font {
font-size:30px !important;
font-weight: bold;
}
</style>
""", unsafe_allow_html=True)
def main():
load_css()
st.title('Fircos Currency Checker')
st.markdown('![Alt Text](https://example.com/path_to_your_image.png)') # Replace URL with your image link
st.markdown('<p class="big-font">Currency Exchange Calculator</p>', unsafe_allow_html=True)
amount = st.number_input('Enter the amount you want to convert:', min_value=0.0, format='%.2f')
currencies = ["USD", "EUR", "GBP", "JPY", "AUD", "CAD", "PKR"]
from_currency = st.selectbox('From:', currencies)
to_currency = st.selectbox('To:', currencies)
if st.button('Convert'):
if from_currency != to_currency:
base_rate, buy_rate, sell_rate, rate_date = get_exchange_rates(from_currency, to_currency)
if base_rate is not None:
result_base = amount * base_rate
result_buy = amount * buy_rate
result_sell = amount * sell_rate
st.success(f"Base Rate: {amount} {from_currency} is equivalent to {result_base:.2f} {to_currency} as of {rate_date}")
st.info(f"Purchase Rate (Buy): {amount} {from_currency} buys {result_buy:.2f} {to_currency} as of {rate_date}")
st.info(f"Selling Rate (Sell): {amount} {from_currency} sells for {result_sell:.2f} {to_currency} as of {rate_date}")
else:
st.error("Failed to retrieve exchange rates.")
else:
st.error("Please select two different currencies.")
if __name__ == "__main__":
main()