Spaces:
Sleeping
Sleeping
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) | |