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

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

# Streamlit UI
st.set_page_config(page_title="Temperature Converter", page_icon="🌡️", layout="centered")
st.title("🌡️ Temperature Converter")
st.markdown("### Convert temperatures between Celsius, Fahrenheit, and Kelvin with ease!")

# Input fields
col1, col2 = st.columns(2)
with col1:
    temp_value = st.number_input("Enter Temperature Value:", min_value=-273.15, format="%.2f")
with col2:
    from_unit = st.selectbox("From Unit", ["Celsius", "Fahrenheit", "Kelvin"], index=0)
    to_unit = st.selectbox("To Unit", ["Celsius", "Fahrenheit", "Kelvin"], index=1)

if st.button("Convert"):
    converted_value = convert_temperature(temp_value, from_unit, to_unit)
    if converted_value is not None:
        st.success(f"Converted Temperature: {converted_value:.2f} {to_unit}")
    else:
        st.error("Invalid conversion. Please check your input.")

# Footer
st.markdown("---")
st.markdown("**Made with ❤️ using Streamlit**")