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