File size: 1,552 Bytes
f0f1e02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st

# Page configuration
st.set_page_config(page_title="Temperature Converter", page_icon="🌡️", layout="centered")

# Styling
st.markdown(
    """
    <style>
    body {
        background-color: #f4f4f4;
    }
    .stApp {
        background: #ffffff;
        padding: 2rem;
        border-radius: 10px;
        box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.1);
        text-align: center;
    }
    </style>
    """,
    unsafe_allow_html=True
)

# Title
st.title("🌡️ Temperature Converter")

# Temperature conversion function
def convert_temperature(value, from_unit, to_unit):
    if from_unit == to_unit:
        return value
    elif from_unit == "Celsius":
        return value * 9/5 + 32 if to_unit == "Fahrenheit" else value + 273.15
    elif from_unit == "Fahrenheit":
        return (value - 32) * 5/9 if to_unit == "Celsius" else (value - 32) * 5/9 + 273.15
    elif from_unit == "Kelvin":
        return value - 273.15 if to_unit == "Celsius" else (value - 273.15) * 9/5 + 32

# User input
temperature = st.number_input("Enter Temperature:", value=0.0, format="%.2f")

# Unit selection
from_unit = st.selectbox("Convert from:", ["Celsius", "Fahrenheit", "Kelvin"])
to_unit = st.selectbox("Convert to:", ["Celsius", "Fahrenheit", "Kelvin"])

# Convert button
if st.button("Convert"):
    result = convert_temperature(temperature, from_unit, to_unit)
    st.success(f"Converted Temperature: {result:.2f} {to_unit}")

# Footer
st.markdown("<small>Created with ❤️ using Streamlit</small>", unsafe_allow_html=True)