| import streamlit as st |
| import requests |
|
|
| st.set_page_config(page_title="Currency Converter", page_icon="π±") |
|
|
| st.title("π± Live Currency Converter") |
|
|
| st.write("Convert currencies in real-time using live exchange rates π") |
|
|
| |
| currencies = [ |
| "USD", "EUR", "GBP", "INR", "PKR", |
| "SAR", "AED", "CAD", "AUD", "JPY", "CNY" |
| ] |
|
|
| |
| from_currency = st.selectbox("From Currency", currencies) |
| to_currency = st.selectbox("To Currency", currencies) |
|
|
| amount = st.number_input("Enter Amount", min_value=0.0, value=1.0) |
|
|
| |
| if st.button("Convert π±"): |
|
|
| try: |
| |
| url = "https://open.er-api.com/v6/latest/USD" |
| response = requests.get(url) |
| data = response.json() |
|
|
| if data["result"] == "success": |
|
|
| rates = data["rates"] |
|
|
| if from_currency in rates and to_currency in rates: |
|
|
| usd_value = amount / rates[from_currency] |
| result = usd_value * rates[to_currency] |
|
|
| st.success(f"{amount} {from_currency} = {result:.2f} {to_currency}") |
|
|
| else: |
| st.error("Invalid currency selected!") |
|
|
| else: |
| st.error("API Error occurred") |
|
|
| except: |
| st.error("Something went wrong. Please try again.") |