File size: 3,025 Bytes
4e2873f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import streamlit as st
import math

# App title and configuration
st.set_page_config(
    page_title="Temperature Converter",
    page_icon="🌡️",
    layout="centered",
    initial_sidebar_state="expanded"
)

# Custom CSS for a fancy look
st.markdown(
    """
    <style>
        .title {
            font-size: 2.5em;
            font-weight: bold;
            color: #4CAF50;
            text-align: center;
        }
        .subtitle {
            font-size: 1.5em;
            color: #2196F3;
            text-align: center;
        }
        .math-box {
            background-color: #f9f9f9;
            border: 1px solid #ddd;
            padding: 10px;
            border-radius: 5px;
        }
    </style>
    """,
    unsafe_allow_html=True
)

# Title and subtitle
st.markdown('<div class="title">🌡️ Temperature Converter 🌟</div>', unsafe_allow_html=True)
st.markdown('<div class="subtitle">Convert temperatures between Celsius, Fahrenheit, and Kelvin</div>', unsafe_allow_html=True)

# Sidebar for user input
st.sidebar.header("Conversion Settings")
input_unit = st.sidebar.selectbox("Select Input Unit:", ["Celsius (°C)", "Fahrenheit (°F)", "Kelvin (K)"])
output_unit = st.sidebar.selectbox("Select Output Unit:", ["Celsius (°C)", "Fahrenheit (°F)", "Kelvin (K)"])
input_value = st.sidebar.number_input("Enter Temperature Value:", value=0.0)

# Conversion logic
def convert_temperature(value, from_unit, to_unit):
    if from_unit == to_unit:
        return value
    
    # Convert input to Celsius first
    if from_unit == "Celsius (°C)":
        celsius = value
    elif from_unit == "Fahrenheit (°F)":
        celsius = (value - 32) * 5 / 9
    elif from_unit == "Kelvin (K)":
        celsius = value - 273.15
    
    # Convert from Celsius to target unit
    if to_unit == "Celsius (°C)":
        return celsius
    elif to_unit == "Fahrenheit (°F)":
        return celsius * 9 / 5 + 32
    elif to_unit == "Kelvin (K)":
        return celsius + 273.15

# Perform conversion
result = convert_temperature(input_value, input_unit, output_unit)

# Display result
st.markdown(f"### 🌟 Converted Temperature: **{result:.2f} {output_unit}**")

# Display math formula
st.markdown("#### 📚 Mathematical Formula")
if input_unit == "Celsius (°C)" and output_unit == "Fahrenheit (°F)":
    st.markdown('**F = (C × 9/5) + 32**')
elif input_unit == "Fahrenheit (°F)" and output_unit == "Celsius (°C)":
    st.markdown('**C = (F - 32) × 5/9**')
elif input_unit == "Celsius (°C)" and output_unit == "Kelvin (K)":
    st.markdown('**K = C + 273.15**')
elif input_unit == "Kelvin (K)" and output_unit == "Celsius (°C)":
    st.markdown('**C = K - 273.15**')
elif input_unit == "Fahrenheit (°F)" and output_unit == "Kelvin (K)":
    st.markdown('**K = (F - 32) × 5/9 + 273.15**')
elif input_unit == "Kelvin (K)" and output_unit == "Fahrenheit (°F)":
    st.markdown('**F = (K - 273.15) × 9/5 + 32**')

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