Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,38 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
to_currency = st.
|
| 14 |
|
| 15 |
-
#
|
| 16 |
if st.button("Convert"):
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
st.
|
| 23 |
else:
|
| 24 |
-
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
|
| 4 |
+
# Page settings
|
| 5 |
+
st.set_page_config(page_title="Currency Converter", page_icon="💱")
|
| 6 |
|
| 7 |
+
# Title
|
| 8 |
+
st.title("💰 Currency Converter")
|
| 9 |
|
| 10 |
+
# Function to fetch exchange rates
|
| 11 |
+
@st.cache_data
|
| 12 |
+
def get_rates(base="USD"):
|
| 13 |
+
url = f"https://api.exchangerate-api.com/v4/latest/{base}"
|
| 14 |
+
response = requests.get(url)
|
| 15 |
+
|
| 16 |
+
if response.status_code != 200:
|
| 17 |
+
return None
|
| 18 |
+
|
| 19 |
+
data = response.json()
|
| 20 |
+
return data["rates"]
|
| 21 |
+
|
| 22 |
+
# UI Inputs
|
| 23 |
+
amount = st.number_input("Enter Amount", min_value=0.0, value=1.0)
|
| 24 |
|
| 25 |
+
from_currency = st.text_input("From Currency (e.g., USD)", value="USD").upper()
|
| 26 |
+
to_currency = st.text_input("To Currency (e.g., PKR)", value="PKR").upper()
|
| 27 |
|
| 28 |
+
# Convert Button
|
| 29 |
if st.button("Convert"):
|
| 30 |
+
rates = get_rates(from_currency)
|
| 31 |
+
|
| 32 |
+
if rates is None:
|
| 33 |
+
st.error("Failed to fetch exchange rates.")
|
| 34 |
+
elif to_currency not in rates:
|
| 35 |
+
st.error("Invalid currency code.")
|
| 36 |
else:
|
| 37 |
+
result = amount * rates[to_currency]
|
| 38 |
+
st.success(f"{amount} {from_currency} = {round(result, 2)} {to_currency}")
|