0o7Hunk commited on
Commit
e2584e9
·
verified ·
1 Parent(s): 80b2109

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -15
app.py CHANGED
@@ -1,24 +1,38 @@
1
  import streamlit as st
2
  import requests
3
 
4
- st.title("💱 Simple Currency Converter")
 
5
 
6
- # Input amount
7
- amount = st.number_input("Enter amount", value=1.0)
8
 
9
- # From currency
10
- from_currency = st.selectbox("From currency", ["USD", "EUR", "GBP", "JPY", "INR"])
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- # To currency
13
- to_currency = st.selectbox("To currency", ["USD", "EUR", "GBP", "JPY", "INR"])
14
 
15
- # Fetch exchange rate
16
  if st.button("Convert"):
17
- url = f"https://api.exchangerate.host/convert?from={from_currency}&to={to_currency}&amount={amount}"
18
- response = requests.get(url)
19
- data = response.json()
20
- if data.get("success"):
21
- converted_amount = data["result"]
22
- st.success(f"{amount} {from_currency} = {converted_amount:.2f} {to_currency}")
23
  else:
24
- st.error("Failed to retrieve data. Try again later.")
 
 
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}")