import streamlit as st
# Function to convert temperature
def convert_temperature(value, from_unit, to_unit):
if from_unit == "Celsius" and to_unit == "Fahrenheit":
return (value * 9/5) + 32
elif from_unit == "Celsius" and to_unit == "Kelvin":
return value + 273.15
elif from_unit == "Fahrenheit" and to_unit == "Celsius":
return (value - 32) * 5/9
elif from_unit == "Fahrenheit" and to_unit == "Kelvin":
return ((value - 32) * 5/9) + 273.15
elif from_unit == "Kelvin" and to_unit == "Celsius":
return value - 273.15
elif from_unit == "Kelvin" and to_unit == "Fahrenheit":
return ((value - 273.15) * 9/5) + 32
else:
return value
# Streamlit app UI
st.set_page_config(page_title="Temperature Conversion App", page_icon="🌡️")
# Heading with a custom font and color
st.title("🌡️ **Temperature Conversion App**")
st.markdown("
Convert temperature units with ease!
", unsafe_allow_html=True)
# Create columns for input and output
col1, col2 = st.columns(2)
with col1:
# Input section with a colorful header
st.markdown("Input Temperature
", unsafe_allow_html=True)
temperature = st.number_input("Enter the temperature value:", value=0.0, step=0.1, format="%.1f")
from_unit = st.selectbox("Select from unit", ["Celsius", "Fahrenheit", "Kelvin"])
with col2:
# Output section with a colorful header
st.markdown("Output Temperature
", unsafe_allow_html=True)
to_unit = st.selectbox("Select to unit", ["Celsius", "Fahrenheit", "Kelvin"])
# Add a colorful button for conversion
convert_button = st.button("🔄 Convert", key="convert_button", help="Click to convert the temperature", use_container_width=True)
# Display the result
if convert_button:
if from_unit == to_unit:
st.warning(f"The temperature is already in **{to_unit}**.")
else:
converted_value = convert_temperature(temperature, from_unit, to_unit)
st.success(f"{temperature} **{from_unit}** is equal to **{converted_value:.2f} {to_unit}**.")
# Footer with some styling
st.markdown("
Made with ❤️ by [Your Name]
", unsafe_allow_html=True)