Talha1786 commited on
Commit
38c3e60
·
verified ·
1 Parent(s): ff9a826

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -32
app.py CHANGED
@@ -2,34 +2,74 @@ import streamlit as st
2
  import requests
3
 
4
  # Set up your API key (Replace 'YOUR_API_KEY' with your actual API key)
5
- API_KEY = "YOUR_API_KEY" # Get a free key from ExchangeRate-API or Open Exchange Rates
6
- API_URL = "https://open.er-api.com/v6/latest/" # Example API endpoint
7
-
8
- # App title and description
9
- st.title("💱 Currency Converter")
10
- st.markdown("""
11
- This app allows you to convert between 50+ currencies in real-time.
12
- Exchange rates are fetched live from the currency exchange API.
13
- """)
14
-
15
- # Input amount to convert
16
- amount = st.number_input("Enter amount to convert:", min_value=0.0, value=1.0, step=0.01)
17
-
18
- # List of currencies
19
- currencies = [
20
- "USD", "EUR", "GBP", "INR", "JPY", "CNY", "AUD", "CAD", "CHF", "ZAR",
21
- "SGD", "NZD", "MXN", "BRL", "RUB", "KRW", "HKD", "SEK", "NOK", "DKK",
22
- "THB", "IDR", "TRY", "PLN", "MYR", "PHP", "ILS", "CZK", "HUF", "AED",
23
- "SAR", "NGN", "EGP", "KZT", "CLP", "PKR", "BDT", "VND", "TWD", "COP",
24
- "ARS", "PEN", "ISK", "BGN", "RON", "HRK", "LKR", "MAD", "OMR", "QAR"
25
- ]
26
-
27
- # Select base and target currencies
28
- base_currency = st.selectbox("From currency:", currencies)
29
- target_currency = st.selectbox("To currency:", currencies)
30
-
31
- # Convert button
32
- if st.button("Convert"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  try:
34
  # Fetch exchange rates
35
  response = requests.get(f"{API_URL}{base_currency}", params={"apiKey": API_KEY})
@@ -40,10 +80,21 @@ if st.button("Convert"):
40
  exchange_rate = data["rates"].get(target_currency)
41
  if exchange_rate:
42
  result = amount * exchange_rate
43
- st.success(f"{amount} {base_currency} = {result:.2f} {target_currency}")
44
  else:
45
- st.error("The selected target currency is not supported.")
46
  else:
47
- st.error("Failed to fetch exchange rates. Please try again later.")
48
  except Exception as e:
49
- st.error(f"An error occurred: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
2
  import requests
3
 
4
  # Set up your API key (Replace 'YOUR_API_KEY' with your actual API key)
5
+ API_KEY = "YOUR_API_KEY" # Get your free key from ExchangeRate-API or Open Exchange Rates
6
+ API_URL = "https://open.er-api.com/v6/latest/"
7
+
8
+ # Custom CSS for better styling
9
+ st.markdown(
10
+ """
11
+ <style>
12
+ .main {
13
+ background-color: #f7f9fc;
14
+ padding: 20px;
15
+ border-radius: 10px;
16
+ }
17
+ .title {
18
+ color: #4a90e2;
19
+ text-align: center;
20
+ font-size: 2.5rem;
21
+ font-weight: bold;
22
+ margin-bottom: 10px;
23
+ }
24
+ .instructions {
25
+ text-align: center;
26
+ font-size: 1.1rem;
27
+ color: #555;
28
+ }
29
+ .convert-btn {
30
+ background-color: #4a90e2;
31
+ color: white;
32
+ padding: 10px 20px;
33
+ border-radius: 5px;
34
+ border: none;
35
+ cursor: pointer;
36
+ }
37
+ .convert-btn:hover {
38
+ background-color: #357ABD;
39
+ }
40
+ </style>
41
+ """,
42
+ unsafe_allow_html=True,
43
+ )
44
+
45
+ # App title
46
+ st.markdown('<div class="title">💱 Currency Converter</div>', unsafe_allow_html=True)
47
+ st.markdown('<div class="instructions">Convert currencies easily with real-time exchange rates for 50+ currencies.</div>', unsafe_allow_html=True)
48
+
49
+ # Sidebar for user input
50
+ with st.sidebar:
51
+ st.header("Conversion Settings")
52
+ st.markdown("Select your desired options below.")
53
+
54
+ # Amount input
55
+ amount = st.number_input("Enter the amount to convert:", min_value=0.0, value=1.0, step=0.01)
56
+
57
+ # Currency selection
58
+ currencies = [
59
+ "USD", "EUR", "GBP", "INR", "JPY", "CNY", "AUD", "CAD", "CHF", "ZAR",
60
+ "SGD", "NZD", "MXN", "BRL", "RUB", "KRW", "HKD", "SEK", "NOK", "DKK",
61
+ "THB", "IDR", "TRY", "PLN", "MYR", "PHP", "ILS", "CZK", "HUF", "AED",
62
+ "SAR", "NGN", "EGP", "KZT", "CLP", "PKR", "BDT", "VND", "TWD", "COP",
63
+ "ARS", "PEN", "ISK", "BGN", "RON", "HRK", "LKR", "MAD", "OMR", "QAR"
64
+ ]
65
+ base_currency = st.selectbox("From currency:", currencies)
66
+ target_currency = st.selectbox("To currency:", currencies)
67
+
68
+ # Layout for results
69
+ col1, col2 = st.columns(2)
70
+
71
+ # Fetch and display conversion results
72
+ if col1.button("Convert"):
73
  try:
74
  # Fetch exchange rates
75
  response = requests.get(f"{API_URL}{base_currency}", params={"apiKey": API_KEY})
 
80
  exchange_rate = data["rates"].get(target_currency)
81
  if exchange_rate:
82
  result = amount * exchange_rate
83
+ col2.success(f"{amount} {base_currency} = {result:.2f} {target_currency}")
84
  else:
85
+ col2.error("The selected target currency is not supported.")
86
  else:
87
+ col2.error("Failed to fetch exchange rates. Please try again later.")
88
  except Exception as e:
89
+ col2.error(f"An error occurred: {e}")
90
+
91
+ # Footer
92
+ st.markdown(
93
+ """
94
+ <div style="text-align: center; margin-top: 20px; color: #aaa;">
95
+ Powered by real-time exchange rates | © 2024 Currency Converter App
96
+ </div>
97
+ """,
98
+ unsafe_allow_html=True,
99
+ )
100
+