File size: 1,411 Bytes
b26b110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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")