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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -15
app.py CHANGED
@@ -1,37 +1,62 @@
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()
 
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()