Spaces:
Runtime error
Runtime error
bikas
commited on
Commit
·
1f6c124
1
Parent(s):
89d0f61
Currency Converter First Deployment
Browse files- api.py +15 -0
- app.py +38 -0
- currency.py +6 -0
- frankfurter.py +40 -0
- requirements.txt +2 -0
api.py
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
|
3 |
+
BASE_URL = "https://api.frankfurter.app"
|
4 |
+
|
5 |
+
def get_currencies():
|
6 |
+
"""
|
7 |
+
Fetch the list of available currencies from Frankfurter API.
|
8 |
+
Returns a list of currency codes.
|
9 |
+
"""
|
10 |
+
response = requests.get(f"{BASE_URL}/currencies")
|
11 |
+
if response.status_code == 200:
|
12 |
+
currencies = response.json()
|
13 |
+
return list(currencies.keys())
|
14 |
+
else:
|
15 |
+
raise Exception("Failed to fetch currencies")
|
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from api import get_currencies
|
3 |
+
from frankfurter import get_latest_rate, get_historical_rate
|
4 |
+
from currency import format_conversion_result
|
5 |
+
from datetime import datetime
|
6 |
+
|
7 |
+
st.title("Currency Converter")
|
8 |
+
|
9 |
+
# Fetch available currencies
|
10 |
+
currencies = get_currencies()
|
11 |
+
|
12 |
+
# Input elements
|
13 |
+
amount = st.number_input("Enter the amount to be converted:", min_value=0.0, value=100.0)
|
14 |
+
from_currency = st.selectbox("From currency:", currencies)
|
15 |
+
to_currency = st.selectbox("To currency:", currencies)
|
16 |
+
|
17 |
+
# Latest conversion rate button
|
18 |
+
if st.button('Get Latest Rate'):
|
19 |
+
try:
|
20 |
+
rate = get_latest_rate(from_currency, to_currency)
|
21 |
+
inverse_rate = 1 / rate if rate != 0 else 0
|
22 |
+
converted_amount = amount * rate
|
23 |
+
result = format_conversion_result(datetime.today().strftime('%Y-%m-%d'), from_currency, to_currency, rate, amount, converted_amount, inverse_rate)
|
24 |
+
st.write(result)
|
25 |
+
except Exception as e:
|
26 |
+
st.write(f"Error: {e}")
|
27 |
+
|
28 |
+
# Historical conversion rate
|
29 |
+
selected_date = st.date_input("Select a historical date:")
|
30 |
+
if st.button('Conversion Rate'):
|
31 |
+
try:
|
32 |
+
rate = get_historical_rate(from_currency, to_currency, selected_date)
|
33 |
+
inverse_rate = 1 / rate if rate != 0 else 0
|
34 |
+
converted_amount = amount * rate
|
35 |
+
result = format_conversion_result(selected_date, from_currency, to_currency, rate, amount, converted_amount, inverse_rate)
|
36 |
+
st.write(result)
|
37 |
+
except Exception as e:
|
38 |
+
st.write(f"Error: {e}")
|
currency.py
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def format_conversion_result(date, from_currency, to_currency, rate, from_amount, to_amount, inverse_rate):
|
2 |
+
return f"""
|
3 |
+
The conversion rate on {date} from {from_currency} to {to_currency} was {rate:.5f}.
|
4 |
+
So {from_amount:.1f} in {from_currency} corresponds to {to_amount:.2f} in {to_currency}.
|
5 |
+
The inverse rate was {inverse_rate:.4f}.
|
6 |
+
"""
|
frankfurter.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
|
3 |
+
BASE_URL = "https://api.frankfurter.app"
|
4 |
+
|
5 |
+
def get_latest_rate(from_currency, to_currency):
|
6 |
+
"""
|
7 |
+
Fetch the latest conversion rate from Frankfurter API.
|
8 |
+
|
9 |
+
Args:
|
10 |
+
from_currency: Currency code to convert from.
|
11 |
+
to_currency: Currency code to convert to.
|
12 |
+
|
13 |
+
Returns:
|
14 |
+
The latest conversion rate.
|
15 |
+
"""
|
16 |
+
response = requests.get(f"{BASE_URL}/latest?from={from_currency}&to={to_currency}")
|
17 |
+
if response.status_code == 200:
|
18 |
+
data = response.json()
|
19 |
+
return data['rates'][to_currency]
|
20 |
+
else:
|
21 |
+
raise Exception("Failed to fetch the latest rate")
|
22 |
+
|
23 |
+
def get_historical_rate(from_currency, to_currency, date):
|
24 |
+
"""
|
25 |
+
Fetch the historical conversion rate for a specific date from Frankfurter API.
|
26 |
+
|
27 |
+
Args:
|
28 |
+
from_currency: Currency code to convert from.
|
29 |
+
to_currency: Currency code to convert to.
|
30 |
+
date: Date for which to fetch the rate (in YYYY-MM-DD format).
|
31 |
+
|
32 |
+
Returns:
|
33 |
+
The historical conversion rate.
|
34 |
+
"""
|
35 |
+
response = requests.get(f"{BASE_URL}/{date}?from={from_currency}&to={to_currency}")
|
36 |
+
if response.status_code == 200:
|
37 |
+
data = response.json()
|
38 |
+
return data['rates'][to_currency]
|
39 |
+
else:
|
40 |
+
raise Exception("Failed to fetch historical rate")
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
requests
|