Engineer786 commited on
Commit
364b861
·
verified ·
1 Parent(s): 22954d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -20
app.py CHANGED
@@ -1,33 +1,34 @@
1
  import streamlit as st
2
- import requests # For making API requests
3
 
4
- # Replace with your preferred currency conversion API URL
5
  API_URL = "https://api.exchangerate.host/latest"
6
 
7
  def get_conversion_rate(from_currency, to_currency):
8
- """Fetches conversion rate from the API."""
9
- response = requests.get(API_URL, params={"base": from_currency, "symbols": to_currency})
10
- if response.status_code == 200:
11
- data = response.json()
12
- return data["rates"][to_currency]
13
- else:
14
- raise ValueError(f"API request failed with status code: {response.status_code}")
15
 
16
  st.title("Real-time Currency Converter")
17
 
18
- # Supported currencies based on the API
19
- supported_currencies = ["USD", "EUR", "JPY", "GBP", "AUD", "CNY"]
20
 
21
  # User input for amount and currencies
22
- amount = st.number_input("Enter amount:", min_value=0.01, step=0.01)
23
- from_currency = st.selectbox("From Currency:", options=supported_currencies)
24
- to_currency = st.selectbox("To Currency:", options=supported_currencies)
 
 
 
 
25
 
26
  # Button to trigger conversion
27
  if st.button("Convert"):
28
- try:
29
- conversion_rate = get_conversion_rate(from_currency, to_currency)
30
- converted_amount = amount * conversion_rate
31
- st.success(f"{amount} {from_currency} is equal to {converted_amount:.2f} {to_currency}")
32
- except ValueError as e:
33
- st.error(f"Error: {e}")
 
1
  import streamlit as st
2
+ import requests
3
 
4
+ # API URL for currency conversion
5
  API_URL = "https://api.exchangerate.host/latest"
6
 
7
  def get_conversion_rate(from_currency, to_currency):
8
+ response = requests.get(API_URL, params={"base": from_currency, "symbols": to_currency})
9
+ if response.status_code == 200:
10
+ data = response.json()
11
+ return data["rates"][to_currency]
12
+ else:
13
+ raise ValueError(f"API request failed with status code: {response.status_code}")
 
14
 
15
  st.title("Real-time Currency Converter")
16
 
17
+ # Supported currencies, including PKR
18
+ supported_currencies = ["USD", "EUR", "GBP", "JPY", "PKR"]
19
 
20
  # User input for amount and currencies
21
+ with st.container():
22
+ col1, col2 = st.columns(2)
23
+ with col1:
24
+ amount = st.number_input("Enter amount:", min_value=0.01, step=0.01)
25
+ with col2:
26
+ from_currency = st.selectbox("From Currency:", options=supported_currencies)
27
+ to_currency = st.selectbox("To Currency:", options=supported_currencies)
28
 
29
  # Button to trigger conversion
30
  if st.button("Convert"):
31
+ try:
32
+ conversion_rate = get_conversion_rate(from_currency, to_currency)
33
+ converted_amount = amount * conversion_rate
34
+ st.success(f"{amount} {from_currency} is equal to {converted_amount:.2f} {to