File size: 1,486 Bytes
1f6c124
 
 
 
 
 
 
 
 
 
 
 
42e355f
1f6c124
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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=0.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}")