File size: 2,124 Bytes
7a1b723
 
29e7b9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a1b723
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29e7b9f
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import streamlit as st

# Custom CSS for the UI
st.markdown("""
    <style>
        /* Change the primary color of the app */
        .stButton>button {
            background-color: #1E90FF; /* Primary color */
            color: white;
            font-size: 16px;
            font-weight: bold;
        }

        .stButton>button:hover {
            background-color: #4682B4; /* Darker shade on hover */
        }

        /* Change input field style */
        .stNumberInput>div>input {
            background-color: #f0f8ff;
            border-radius: 8px;
            padding: 10px;
            font-size: 16px;
        }

        /* Custom font for the title */
        h1 {
            font-family: 'Arial', sans-serif;
            color: #2E8B57;
        }

        /* Styling the select box */
        .stSelectbox>div>input {
            background-color: #f0f8ff;
            border-radius: 8px;
            padding: 10px;
            font-size: 16px;
        }
    </style>
""", unsafe_allow_html=True)

# Function to convert temperatures
def convert_temperature(value, from_unit, to_unit):
    if from_unit == to_unit:
        return value

    # Convert to Celsius
    if from_unit == "Fahrenheit":
        value = (value - 32) * 5 / 9
    elif from_unit == "Kelvin":
        value = value - 273.15

    # Convert from Celsius to target unit
    if to_unit == "Fahrenheit":
        return value * 9 / 5 + 32
    elif to_unit == "Kelvin":
        return value + 273.15
    else:
        return value

# Streamlit UI
st.title("🌡️ Temperature Converter")

st.write("Convert temperatures between Celsius, Fahrenheit, and Kelvin with ease.")

# Input temperature
temp_value = st.number_input("Enter the temperature:", value=0.0, step=0.1, format="%.2f")

# Units selection
units = ["Celsius", "Fahrenheit", "Kelvin"]
from_unit = st.selectbox("From Unit:", units)
to_unit = st.selectbox("To Unit:", units)

# Convert button
if st.button("Convert"):
    converted_temp = convert_temperature(temp_value, from_unit, to_unit)
    st.success(f"The converted temperature is: **{converted_temp:.2f} {to_unit}**")