CoderHassan's picture
Create app.py
b26b110 verified
import streamlit as st
import requests
# Set the title of the app
st.title("Currency Converter")
# API URL for getting the latest exchange rates (using a free API)
API_URL = "https://api.exchangerate-api.com/v4/latest/USD"
# Function to get exchange rates from the API
def get_exchange_rates():
response = requests.get(API_URL)
if response.status_code == 200:
data = response.json()
return data['rates']
else:
st.error("Failed to fetch exchange rates")
return {}
# Load exchange rates
exchange_rates = get_exchange_rates()
# Check if rates are available
if exchange_rates:
# List of available currencies
currencies = list(exchange_rates.keys())
# Create selectboxes for currency input
from_currency = st.selectbox("Select the currency you want to convert from", currencies)
to_currency = st.selectbox("Select the currency you want to convert to", currencies)
# Input field for amount to convert
amount = st.number_input(f"Amount in {from_currency}", min_value=0.01, step=0.01)
if amount > 0:
# Perform the conversion
conversion_rate = exchange_rates[to_currency] / exchange_rates[from_currency]
converted_amount = amount * conversion_rate
# Display the result
st.write(f"{amount} {from_currency} = {converted_amount:.2f} {to_currency}")
else:
st.error("No exchange rates available")