Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,34 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import requests
|
| 3 |
|
| 4 |
-
#
|
| 5 |
API_URL = "https://api.exchangerate.host/latest"
|
| 6 |
|
| 7 |
def get_conversion_rate(from_currency, to_currency):
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
raise ValueError(f"API request failed with status code: {response.status_code}")
|
| 15 |
|
| 16 |
st.title("Real-time Currency Converter")
|
| 17 |
|
| 18 |
-
# Supported currencies
|
| 19 |
-
supported_currencies = ["USD", "EUR", "
|
| 20 |
|
| 21 |
# User input for amount and currencies
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
# Button to trigger conversion
|
| 27 |
if st.button("Convert"):
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
except ValueError as e:
|
| 33 |
-
st.error(f"Error: {e}")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
|
| 4 |
+
# API URL for currency conversion
|
| 5 |
API_URL = "https://api.exchangerate.host/latest"
|
| 6 |
|
| 7 |
def get_conversion_rate(from_currency, to_currency):
|
| 8 |
+
response = requests.get(API_URL, params={"base": from_currency, "symbols": to_currency})
|
| 9 |
+
if response.status_code == 200:
|
| 10 |
+
data = response.json()
|
| 11 |
+
return data["rates"][to_currency]
|
| 12 |
+
else:
|
| 13 |
+
raise ValueError(f"API request failed with status code: {response.status_code}")
|
|
|
|
| 14 |
|
| 15 |
st.title("Real-time Currency Converter")
|
| 16 |
|
| 17 |
+
# Supported currencies, including PKR
|
| 18 |
+
supported_currencies = ["USD", "EUR", "GBP", "JPY", "PKR"]
|
| 19 |
|
| 20 |
# User input for amount and currencies
|
| 21 |
+
with st.container():
|
| 22 |
+
col1, col2 = st.columns(2)
|
| 23 |
+
with col1:
|
| 24 |
+
amount = st.number_input("Enter amount:", min_value=0.01, step=0.01)
|
| 25 |
+
with col2:
|
| 26 |
+
from_currency = st.selectbox("From Currency:", options=supported_currencies)
|
| 27 |
+
to_currency = st.selectbox("To Currency:", options=supported_currencies)
|
| 28 |
|
| 29 |
# Button to trigger conversion
|
| 30 |
if st.button("Convert"):
|
| 31 |
+
try:
|
| 32 |
+
conversion_rate = get_conversion_rate(from_currency, to_currency)
|
| 33 |
+
converted_amount = amount * conversion_rate
|
| 34 |
+
st.success(f"{amount} {from_currency} is equal to {converted_amount:.2f} {to
|
|
|
|
|
|