KubraBashir commited on
Commit
4fe526b
·
verified ·
1 Parent(s): ff0a9d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -43
app.py CHANGED
@@ -1,62 +1,45 @@
1
  import streamlit as st
2
  import requests
3
 
4
- # Predefined currency mappings for PKR conversions
5
- PREDEFINED_RATES = {
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 = f"https://api.exchangerate-api.com/v4/latest/{base_currency}"
18
  response = requests.get(url)
19
  response.raise_for_status()
20
  data = response.json()
21
- return data["rates"].get(target_currency, None)
22
  except Exception as e:
23
- st.error(f"Error fetching exchange rate: {e}")
24
- return None
25
 
26
  def main():
27
- st.title("Currency Converter App")
28
- st.write("Convert PKR to predefined currencies or any currency pair globally.")
29
 
30
  # Input Section
31
- amount = st.number_input("Enter the amount to convert:", min_value=0.0, format="%.2f")
32
- base_currency = st.text_input("Base Currency (e.g., PKR, USD):").upper()
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
- if predefined_target == "Custom":
41
- target_currency = st.text_input("Target Currency (e.g., USD, EUR):").upper()
42
- else:
43
- target_currency = predefined_target
44
 
45
  # Convert Button
46
- if st.button("Convert"):
47
- if not base_currency or not target_currency:
48
- st.warning("Please enter both the base and target currencies.")
 
 
 
 
 
 
 
 
 
49
  else:
50
- rate = get_exchange_rate(base_currency, target_currency)
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()