Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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}")
|