import streamlit as st from api import get_currencies from frankfurter import get_latest_rate, get_historical_rate from currency import format_conversion_result from datetime import datetime st.title("Currency Converter") # Fetch available currencies currencies = get_currencies() # Input elements amount = st.number_input("Enter the amount to be converted:", min_value=0.0, value=100.0) from_currency = st.selectbox("From currency:", currencies) to_currency = st.selectbox("To currency:", currencies) # Latest conversion rate button if st.button('Get Latest Rate'): try: rate = get_latest_rate(from_currency, to_currency) inverse_rate = 1 / rate if rate != 0 else 0 converted_amount = amount * rate result = format_conversion_result(datetime.today().strftime('%Y-%m-%d'), from_currency, to_currency, rate, amount, converted_amount, inverse_rate) st.write(result) except Exception as e: st.write(f"Error: {e}") # Historical conversion rate selected_date = st.date_input("Select a historical date:") if st.button('Conversion Rate'): try: rate = get_historical_rate(from_currency, to_currency, selected_date) inverse_rate = 1 / rate if rate != 0 else 0 converted_amount = amount * rate result = format_conversion_result(selected_date, from_currency, to_currency, rate, amount, converted_amount, inverse_rate) st.write(result) except Exception as e: st.write(f"Error: {e}")