Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,45 @@
|
|
1 |
import streamlit as st
|
2 |
import requests
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
-
"AED": "United Arab Emirates Dirham",
|
7 |
-
"SAR": "Saudi Riyal",
|
8 |
-
"KWD": "Kuwaiti Dinar",
|
9 |
-
"TRY": "Turkish Lira"
|
10 |
-
}
|
11 |
-
|
12 |
-
def get_exchange_rate(base_currency, target_currency):
|
13 |
-
"""
|
14 |
-
Fetch the exchange rate from an API.
|
15 |
-
"""
|
16 |
try:
|
17 |
-
url =
|
18 |
response = requests.get(url)
|
19 |
response.raise_for_status()
|
20 |
data = response.json()
|
21 |
-
return data["rates"]
|
22 |
except Exception as e:
|
23 |
-
st.error(f"Error fetching exchange
|
24 |
-
return
|
25 |
|
26 |
def main():
|
27 |
-
st.title("Currency Converter
|
28 |
-
st.write("Convert
|
29 |
|
30 |
# Input Section
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
predefined_target = st.radio(
|
35 |
-
"Choose a predefined target currency (for PKR):",
|
36 |
-
list(PREDEFINED_RATES.keys()) + ["Custom"],
|
37 |
-
horizontal=True
|
38 |
-
)
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
else:
|
43 |
-
target_currency = predefined_target
|
44 |
|
45 |
# Convert Button
|
46 |
-
if st.button("Convert"):
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
else:
|
50 |
-
|
51 |
-
if rate:
|
52 |
-
converted_amount = amount * rate
|
53 |
-
st.success(
|
54 |
-
f"{amount} {base_currency} is equal to {converted_amount:.2f} {target_currency}."
|
55 |
-
)
|
56 |
-
if target_currency in PREDEFINED_RATES:
|
57 |
-
st.write(f"Target currency: {PREDEFINED_RATES[target_currency]}")
|
58 |
-
else:
|
59 |
-
st.error(f"Exchange rate not found for {base_currency} to {target_currency}.")
|
60 |
|
61 |
if __name__ == "__main__":
|
62 |
main()
|
|
|
1 |
import streamlit as st
|
2 |
import requests
|
3 |
|
4 |
+
# Function to fetch the exchange rate for multiple currencies to PKR
|
5 |
+
def get_exchange_rates_to_pkr():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
try:
|
7 |
+
url = "https://api.exchangerate-api.com/v4/latest/PKR"
|
8 |
response = requests.get(url)
|
9 |
response.raise_for_status()
|
10 |
data = response.json()
|
11 |
+
return data["rates"]
|
12 |
except Exception as e:
|
13 |
+
st.error(f"Error fetching exchange rates: {e}")
|
14 |
+
return {}
|
15 |
|
16 |
def main():
|
17 |
+
st.title("Currency to PKR Converter")
|
18 |
+
st.write("Convert amounts in multiple currencies to Pakistani Rupees (PKR).")
|
19 |
|
20 |
# Input Section
|
21 |
+
available_currencies = ["USD", "EUR", "GBP", "AED", "SAR", "KWD", "TRY"]
|
22 |
+
selected_currencies = st.multiselect("Select the currencies:", available_currencies)
|
23 |
+
amounts = {}
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
for currency in selected_currencies:
|
26 |
+
amounts[currency] = st.number_input(f"Enter the amount in {currency}:", min_value=0.0, format="%.2f")
|
|
|
|
|
27 |
|
28 |
# Convert Button
|
29 |
+
if st.button("Convert to PKR"):
|
30 |
+
rates = get_exchange_rates_to_pkr()
|
31 |
+
if rates:
|
32 |
+
total_pkr = 0
|
33 |
+
for currency, amount in amounts.items():
|
34 |
+
if currency in rates:
|
35 |
+
converted = amount / rates[currency]
|
36 |
+
total_pkr += converted
|
37 |
+
st.success(f"{amount:.2f} {currency} = {converted:.2f} PKR")
|
38 |
+
else:
|
39 |
+
st.warning(f"Exchange rate for {currency} is not available.")
|
40 |
+
st.write(f"### Total in PKR: {total_pkr:.2f}")
|
41 |
else:
|
42 |
+
st.error("Failed to fetch exchange rates. Please try again later.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
if __name__ == "__main__":
|
45 |
main()
|