Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
|
4 |
+
def get_exchange_rate(from_currency, to_currency):
|
5 |
+
try:
|
6 |
+
url = f"https://api.exchangerate-api.com/v4/latest/{from_currency}"
|
7 |
+
response = requests.get(url)
|
8 |
+
data = response.json()
|
9 |
+
rate = data["rates"].get(to_currency, None)
|
10 |
+
return rate
|
11 |
+
except Exception as e:
|
12 |
+
st.error(f"An error occurred: {e}")
|
13 |
+
return None
|
14 |
+
|
15 |
+
def main():
|
16 |
+
st.title("Currency Converter App")
|
17 |
+
st.write("Convert amounts between different currencies using real-time exchange rates.")
|
18 |
+
|
19 |
+
# Input Section
|
20 |
+
amount = st.number_input("Enter the amount:", min_value=0.0, format="%.2f")
|
21 |
+
from_currency = st.text_input("From Currency (e.g., USD, EUR):").upper()
|
22 |
+
to_currency = st.text_input("To Currency (e.g., USD, EUR):").upper()
|
23 |
+
|
24 |
+
# Convert Button
|
25 |
+
if st.button("Convert"):
|
26 |
+
if not from_currency or not to_currency:
|
27 |
+
st.warning("Please enter both the source and target currencies.")
|
28 |
+
else:
|
29 |
+
rate = get_exchange_rate(from_currency, to_currency)
|
30 |
+
if rate:
|
31 |
+
converted_amount = amount * rate
|
32 |
+
st.success(f"{amount} {from_currency} is equal to {converted_amount:.2f} {to_currency}")
|
33 |
+
else:
|
34 |
+
st.error(f"Conversion rate not available for {from_currency} to {to_currency}.")
|
35 |
+
|
36 |
+
if __name__ == "__main__":
|
37 |
+
main()
|